【问题标题】:How can I switch tab bar tabs using left and right swipe gestures?如何使用左右滑动手势切换标签栏标签?
【发布时间】:2018-07-10 13:02:08
【问题描述】:

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

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

【问题讨论】:

    标签: ios swift xcode uitabbar gesture


    【解决方案1】:

    将以下滑动手势添加到您的视图控制器视图

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

    【讨论】:

    • @TheLukeGuy swiped 函数(屏幕截图中的第 25-45 行)不会进入 viewDidLoad,将其作为自己的函数放在外面。
    • @skladek 现在我滑动时它什么也没做,代码如下:prntscr.com/k4tajg
    • 所有这些都应该在标签栏控制器正在显示的 UIViewControllers 上,而不是在标签栏控制器本身上。
    猜你喜欢
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-10
    相关资源
    最近更新 更多