希望通过在swift中使用swipe gesters在标签栏之间导航
我想要使用滑动手势在标签栏控制器之间导航,同时保持默认的标签栏。 我用这个代码,但显示一个错误。
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
必须是一个类级别的函数,而不是另一个函数的内部函数:
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) { } }
这是我可以看到的明显的错误。 你应该总是发布你的错误信息与你的问题,以提高答案的质量。
我可以通过将ID添加到SecondViewController类来获取代码,如下图所示:
唯一的问题是,得到这个工作后,你将无法看到Tab栏控制器,因为你打开了没有UI Tab栏控制器的SecondViewController。
请让我知道,如果我的解决scheme的工作原理,你有没有find其他方式刷到第二个视图,并具有UI标签栏。
[编辑]其实我挖了多一点,发现它比我想象的更容易。 我们对tabBar控制器问题的解决方法没有显示如下:
func handleSwipes(sender:UISwipeGestureRecognizer) { let selectedIndex: Int = self.tabBarController!.selectedIndex self.tabBarController!.selectedIndex = selectedIndex + 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 } }
你应该使用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?
复制广告粘贴这个
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) } }