首页 > 解决方案 > 当用户直接点击标签栏项目时,如何禁用滑动动画?

问题描述

在我的项目中,我启用了一个名为“SwipeableTabBarController”的 coacopods。这些允许我的标签栏视图控制器检测平移手势并在标签之间切换。而且我还编写了一些代码来检测滑动手势,它允许用户隐藏标签栏。问题:即使用户直接点击栏项目,我的应用也会有幻灯片动画。有什么办法可以解决这个问题?我感谢任何帮助!

尝试在检测到点击时禁用滑动和平移手势。但是平移手势不在我的手势数组中。

标签: swiftgestureuipangesturerecognizer

解决方案


用于isSwipeEnabled = false禁用滑动功能。默认情况下,它设置为 trueSwipeableTabBarController

更新:

由于您正在寻找没有SwipeableTabBarController库提供的动画的解决方案,但仍需要滑动功能。这是使用默认值执行此操作的方法UITabBarController

第 1 步: 创建一个默认UITabBarController和 2 个视图控制器,让我们调用它们ViewController_1&ViewController_2

第2步: 为每个创建一个类,ViewController并在ViewDidLoad()两者的方法中ViewController_1添加ViewController_2这些行。

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)

}

然后在两个类中每次检测到滑动时添加此函数。

@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
        }
    }
}

这将使您能够滑动和导航到不同的 ViewController 并使用Tabbar按钮进行导航。

希望这可以帮助。


推荐阅读