想要在swift中使用滑动gesters在标签栏之间导航

时间:2015-08-03 08:46:56

标签: ios swift uitabbarcontroller

我想使用滑动手势在标签栏控制器之间导航,同时保留默认标签栏。我使用了这段代码,但显示错误。

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) {

            }
        }


    }


}

5 个答案:

答案 0 :(得分:2)

您的函数handleSwipes必须是类级函数,而不是其他函数的内部函数:

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) {

    }
}

这是我能看到的明显错误。您应该始终发布包含问题的错误消息,以提高答案的质量。

答案 1 :(得分:1)

我可以通过将ID添加到SecondViewController类来使代码工作,如下图所示:

enter image description here

唯一的问题是,在使用此功能后,您将无法看到Tab栏控制器,因为您已经打开了没有UI Tab栏控制器的SecondViewController。

如果我的解决方案有效,请告诉我,您是否找到了其他方式滑动到第二个视图并同时使用UI标签栏。

[编辑] 实际上我挖了一下,发现它比我想象的要容易。 我们的tabBar控制器问题没有显示的解决方法如下:

   func handleSwipes(sender:UISwipeGestureRecognizer) {
        let selectedIndex: Int = self.tabBarController!.selectedIndex
        self.tabBarController!.selectedIndex = selectedIndex + 1
}

答案 2 :(得分:1)

下一步是UITabBarViewController(Swift 3.0)的最佳方式:

func handleSwipes(发件人:UISwipeGestureRecognizer){

    if sender.direction == UISwipeGestureRecognizerDirection.left {

        self.selectedIndex += 1

    } else if sender.direction == UISwipeGestureRecognizerDirection.right {

        self.selectedIndex -= 1
    }

}

答案 3 :(得分:0)

您应该使用 UITabBarControllerDelegate 方法来完成此任务。 UITabBarControllerDelegate上有新的委托方法,允许您返回 UIViewControllerAnimatedTransitioning UIViewControllerInteractiveTransitioning

这些是您将使用的委托方法,

- (id <UIViewControllerInteractiveTransitioning>)tabBarController:(UITabBarController *)tabBarController
                      interactionControllerForAnimationController: (id <UIViewControllerAnimatedTransitioning>)animationController;

- (id <UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController
            animationControllerForTransitionFromViewController:(UIViewController *)fromVC
                                              toViewController:(UIViewController *)toVC;

委托方法的Swift代码如下所示,

func tabBarController(tabBarController: UITabBarController, interactionControllerForAnimationController animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning?

func tabBarController(tabBarController: UITabBarController, animationControllerForTransitionFromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?

答案 4 :(得分:0)

复制广告粘贴

class SwipeGesture: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let left = UISwipeGestureRecognizer(target: self, action: #selector(swipeLeft))
        left.direction = .left
        self.view.addGestureRecognizer(left)

        let right = UISwipeGestureRecognizer(target: self, action: #selector(swipeRight))
        right.direction = .right
        self.view.addGestureRecognizer(right)

    }

    @objc func swipeLeft() {
        let total = self.tabBarController!.viewControllers!.count - 1
        tabBarController!.selectedIndex = min(total, tabBarController!.selectedIndex + 1)

    }

    @objc func swipeRight() {
        tabBarController!.selectedIndex = max(0, tabBarController!.selectedIndex - 1)
    }


}