本文介绍了想要通过快速滑动手势来在选项卡栏之间导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用滑动手势在选项卡栏控制器之间导航,同时保留默认选项卡栏.我使用了这段代码,但显示了一个错误.

I want to use a swipe gesture to navigate between tab bar controllers while keep the default tab bars. I used this code but shows an error.

import UIKit

 class FirstViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()


        var leftSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))


        leftSwipe.direction = .Left


        view.addGestureRecognizer(leftSwipe)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.

        func handleSwipes(sender:UISwipeGestureRecognizer) {
            if (sender.direction == .Left) {

                let storyboard = UIStoryboard(name: "Main", bundle: nil)
                let vc = storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as! UIViewController
                self.presentViewController(vc, animated: false, completion: nil)

            }

            if (sender.direction == .Right) {

            }
        }


    }


}

推荐答案

您的函数 handleSwipes 必须是类级别的函数,而不是另一个函数的内部函数:

Your function handleSwipes must be a class level function, not an inner function of another function:

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.

    // remove the func from here
}

func handleSwipes(sender:UISwipeGestureRecognizer) {
    if (sender.direction == .Left) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as! UIViewController
        self.presentViewController(vc, animated: false, completion: nil)
    }

    if (sender.direction == .Right) {

    }
}

这是我可以看到的明显错误.您应该始终将错误消息和问题一起发布,以提高答案的质量.

That is the obvious error I can see. You should always post your error message with your questions to improve the quality of answers.

这篇关于想要通过快速滑动手势来在选项卡栏之间导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 21:27