swiftgestureuipangesturerecognizer

How do I disable swipe animation when user directly tap on tab bar item?


In my project, I enable a coacopods called 'SwipeableTabBarController'. These allow my tab bar view controller detect pan gestures and switch between tabs. And I also write some code to detect swipe gesture, which allows users to hide the tab bar. Problem: My app will have a slide animation even when the user directly tap on the bar item. Any way to solve this? I appreciated any help!

Tried to disable swipe and pan gestures when a tap is detected. But the pan gestures are not in my gesture array.


Solution

  • Use isSwipeEnabled = false to disable the swipe feature. By default it is set to true in SwipeableTabBarController

    UPDATE:

    Since you are looking for a solution without the animation that SwipeableTabBarController library provides, but still want the swipe feature. Here is how you can do this with the default UITabBarController.

    Step 1: Create a default UITabBarController and 2 View Controllers, lets call them ViewController_1 & ViewController_2

    Step 2: Create a class for each ViewController and in the ViewDidLoad() method of both ViewController_1 & ViewController_2 add these lines.

    override func viewDidLoad() {
        super.viewDidLoad()
    
        let swipeRight = UISwipeGestureRecognizer(target: self, action:  #selector(swiped))
        swipeRight.direction = UISwipeGestureRecognizer.Direction.right
        self.view.addGestureRecognizer(swipeRight)
    
        let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
        swipeLeft.direction = UISwipeGestureRecognizer.Direction.left
        self.view.addGestureRecognizer(swipeLeft)
    
    }
    

    And then add this function for every time a swipe is detected in both the classes.

    @objc  func swiped(_ gesture: UISwipeGestureRecognizer) {
        if gesture.direction == .left {
            if (self.tabBarController?.selectedIndex)! < 2
            {
                self.tabBarController?.selectedIndex += 1
            }
        } else if gesture.direction == .right {
            if (self.tabBarController?.selectedIndex)! > 0 {
                self.tabBarController?.selectedIndex -= 1
            }
        }
    }
    

    This will give you the ability to swipe and navigate to different ViewControllers and also navigate using the Tabbar buttons.

    Hope this helps.