ios - 使用滑动手势在标签栏之间导航

原文 标签 ios swift swift3 xcode8 swipe

我想使用滑动手势在我的标签栏之间导航。最简单的方法是什么?我试过这样的事情......

import UIKit

class postAdViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        var leftSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
        view.addGestureRecognizer(leftSwipe)    
    }

    func handleSwipes(sender:UISwipeGestureRecognizer) {
        if (sender.direction == .left) {
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = storyboard.instantiateViewController(withIdentifier: "favourireviewcontroller") as! UIViewController
            self.present(vc, animated: true, completion: nil)
        }
        if (sender.direction == .right) {

        }
}

如果我尝试向右滑动,则什么也不会发生。向左滑动时应用程序崩溃并显示以下错误消息

unrecognized selector sent to instance 0x7f924380a730

最佳答案

好吧,如果你想通过你导航tabBar你应该实现一个 swipeGestureRecognizer.left.right然后使用 tabBarController?.selectedIndex ,像这样:

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)

func swiped(_ gesture: UISwipeGestureRecognizer) {
    if gesture.direction == .left {
        if (self.tabBarController?.selectedIndex)! < 2 { // set your total tabs here
            self.tabBarController?.selectedIndex += 1
        }
    } else if gesture.direction == .right {
        if (self.tabBarController?.selectedIndex)! > 0 {
            self.tabBarController?.selectedIndex -= 1
        }
    }
}

关于ios - 使用滑动手势在标签栏之间导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44231800/

相关文章:

ios - 如何从didSwift3的didRegisterForRemoteNotificationsWithDeviceToken函数中删除几乎匹配的可选要求警告?

ios - 无法调用非功能类型'(((AnyClass)-> Bool)!'的值!

ios - 来自 Swift 代码的抗锯齿像素被 OpenCV 转换为不需要的黑色像素

ios - SKSpriteNode UserInteractionEnabled不起作用

ios - 如何在 SwiftUI 应用程序生命周期中更改 StatusBarStyle?

ios - tapGestureRecognizer.location在UICollectionView中带来了零

ios - Swift 3:无法调用非函数类型“Bundle”的值

ios - 重新启动iphone时是否调用了applicationWillEnterForeground?

ios - 在Xcode/Swift中运行UITableView函数之前,如何从Firebase加载所有数据

ios - 使用AVPlayer在iOS中进行音频流传输时的播放速度