swift - 当用户直接点击标签栏项目时,如何禁用滑动动画?
问题描述
在我的项目中,我启用了一个名为“SwipeableTabBarController”的 coacopods。这些允许我的标签栏视图控制器检测平移手势并在标签之间切换。而且我还编写了一些代码来检测滑动手势,它允许用户隐藏标签栏。问题:即使用户直接点击栏项目,我的应用也会有幻灯片动画。有什么办法可以解决这个问题?我感谢任何帮助!
尝试在检测到点击时禁用滑动和平移手势。但是平移手势不在我的手势数组中。
解决方案
用于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
按钮进行导航。
希望这可以帮助。
推荐阅读
- windows - 如何在不破坏内部绑定的情况下从外部初始化自定义项的属性?
- java - autoCompleteTextView 仅在删除字母后显示结果
- python - Pandas - 如何向数据框添加多个条件列?
- arrays - 有没有办法将一些列分组为列数组?PostgreSQL
- java - 一种方法来获取另一个方法的运行时间
- reactjs - 你如何让子路径在 minikube 中为 create-react-app 工作?
- c# - 没有为类型“System.Int32”和“System.Object”定义二元运算符 Equal
- vb.net - 匹配单词并添加数量 vb.net
- reactjs - 将 TypeScript 模块从本地模块导入 React 应用程序
- node.js - 从另一台计算机发送 https 请求 nodejs express