3

Trying to implement a TabView with PageTabView style in SwiftUI, where navigation is only done programmatically, and all swipe gestures are disabled.

This solution only partially works - if you tap the screen as the selection is changing, it still interferes with the transition and causes weird effects. Also, if you scroll with two fingers the gesture still registers. I need a solution that fully disables the swipe gesture.

Code:

struct PageViewTest: View {
    @State var selection: Int = 1
    
    var body: some View {
        ZStack {
            Color.green.ignoresSafeArea()
            
            TabView(selection: $selection) {
                Color.red
                    .tag(1)
                    .gesture(DragGesture())
                
                Color.blue
                    .tag(2)
                    .gesture(DragGesture())
                
                Color.yellow
                    .tag(3)
                    .gesture(DragGesture())
            }
            .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
            .animation(.linear, value: selection)
            
            VStack {
                Spacer()
                Button(action: {
                    selection = selection == 3 ? 1 : selection + 1
                }) {
                    Text("next")
                        .foregroundColor(.white)
                        .font(.title)
                }
            }
        }
    }
}

setting .disabled(true) to the TabView solves it, but then all of the subviews are no longer interactive.

Dhia Djobbi
  • 1,176
  • 2
  • 15
  • 35
bze12
  • 727
  • 8
  • 20
  • If you do NOT want swipe gestures on your TabView, why you are given **DragGesture()** to each? and what is **DragGesture()**? any code for that? – ios coder Mar 10 '21 at 22:34
  • DragGesture() attaches an empty gesture (does nothing) to that view, which I assume overrides the default drag behavior. – bze12 Mar 11 '21 at 02:42
  • I tested your code, the tabs do not swipe, so what you wanted is already there! what I am missing? – ios coder Mar 11 '21 at 03:15
  • try scrolling with two fingers – bze12 Mar 11 '21 at 03:25
  • I think I can help you, but I need to know what would be that I solve the issue with not using TabView, but you could change Pages of App also like before? – ios coder Mar 11 '21 at 17:14

2 Answers2

0

This answer allows the creation of an overlay with a custom gesture recognizer in SwiftUI. Then all we need is to create a delegate that won't allow the gesture to start.

So the code would be:

import SwiftUI
import UIKit


struct ContentView: View {
    @State var selection: Int = 1
    let numTabs = 3
    let minDragTranslationForSwipe: CGFloat = 5000

    var body: some View {
        ZStack {
            Color.green.ignoresSafeArea()
            
            TabView(selection: $selection) {
                Color.red
                    .tag(1)
                Color.blue
                    .tag(2)
                Color.yellow
                    .tag(3)
            }
            .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
            .animation(.linear, value: selection)
                .overlay(TouchesHandler())
            
            VStack {
                Spacer()
                Button(action: {
                    selection = selection == 3 ? 1 : selection + 1
                }) {
                    Text("next")
                        .foregroundColor(.white)
                        .font(.title)
                }
            }
        }

    }
}

//just a dummy
class MySwipeGesture: UISwipeGestureRecognizer {

    @objc func noop() {}

    init(target: Any?) {
        super.init(target: target, action: #selector(noop))
    }
}

//this delegate effectively disables the gesure
class MySwipeGestureDelegate: NSObject, UIGestureRecognizerDelegate {
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        false
    }
}

//and the overlay inspired by the answer from the link above
struct TouchesHandler: UIViewRepresentable {

    func makeUIView(context: UIViewRepresentableContext<TouchesHandler>) -> UIView {
        let view = UIView(frame: .zero)
        view.isUserInteractionEnabled = true
        view.addGestureRecognizer(context.coordinator.makeGesture())
        return view;
    }

    func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<TouchesHandler>) {
    }

    func makeCoordinator() -> Coordinator {
        return Coordinator()
    }

    class Coordinator {
        var delegate: UIGestureRecognizerDelegate = MySwipeGestureDelegate()
        func makeGesture() -> MySwipeGesture {
            delegate = MySwipeGestureDelegate()
            let gr = MySwipeGesture(target: self)
            gr.delegate = delegate
            return gr
        }
    }
    typealias UIViewType = UIView
}
Dan O
  • 36
  • 4
0

This is to help others, after some hours of research I believe the following is a solid simple solution using SwiftUI-Introspec Library on github. If you simply remove the gestures on the first view. It makes your problems go away. :]. You can also build your custom PagerView to avoid this problem altogether using Majid's solution as mentioned here previously.

TabView(selection: $selection) {
    Color.red
        .tag(0)
        .introspectScrollView { scrollView in
            scrollView.gestureRecognizers?.removeAll()
        }
    Color.blue
        .tag(1)
    
    Color.yellow
        .tag(2)
}
Wael
  • 489
  • 6
  • 19