如何使用左右滑动手势切换标签栏选项卡?

How can I switch tab bar tabs using left and right swipe gestures?

我有一个 UITabBarController,上面连接了两个选项卡。如何在两个视图中使用左右滑动手势左右切换标签页?

我看过其他类似的问题,但他们都使用 Objective-C。另外,如果这一切都可以在情节提要中完成,我宁愿这样做也不必使用 Swift 代码。

将以下滑动手势添加到您的 viewcontroller 视图

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