我有一个UITabBarController
,其中连接了两个标签。如何在两个视图中使用向左和向右滑动手势来左右切换选项卡?
我看过其他与此类似的问题,但所有问题都使用Objective-C。另外,如果这一切都可以在情节提要中完成,那么我宁愿选择使用Swift代码。
答案 0 :(得分:2)
将以下滑动手势添加到视图控制器视图中
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
swipeRight.direction = UISwipeGestureRecognizerDirection.right
self.view.addGestureRecognizer(swipeRight)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
swipeLeft.direction = UISwipeGestureRecognizerDirection.left
self.view.addGestureRecognizer(swipeLeft)
// below code create swipe gestures function
// MARK: - swiped
@objc func swiped(_ gesture: UISwipeGestureRecognizer) {
if gesture.direction == .left {
if (self.tabBarController?.selectedIndex)! < 2
{ // set here your total tabs
self.tabBarController?.selectedIndex += 1
}
} else if gesture.direction == .right {
if (self.tabBarController?.selectedIndex)! > 0 {
self.tabBarController?.selectedIndex -= 1
}
}
}