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 中导航,你应该为 .left.right 实现一个 swipeGestureRecognizer > 然后使用 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 - 从远程 URL 下载视频到设备并通过 AVPlayer 流式传输

ios - Swift 项目在启动时崩溃 - 适用于 Objective-C

ios - 字段 `lyrics` 对于 swift 中当前播放歌曲的 MPMediaItem 是空的,ios 12+

swift - Alamofire 3 自定义编码到 Alamofire 4 自定义编码

swift - 在可重用的表格 View 单元格中自动触摸按钮

objective-c - 如何移动 UIViewController?

android - Webview加载远程url

ios - Swift 3 中的 Wikitude 实例

ios - 如何在设备屏幕因不活动而关闭时/之前立即执行方法?

ios - 关于Swift中可选变量操作的问题