问题
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) {
}
}
}
}
回答1:
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.
回答2:
I could get your code working by adding the ID to the SecondViewController class as shown in the image below :
The only problem is, after getting this to work, you won't be able to see the Tab bar controller, since you have opened the SecondViewController which doesn't have the UI Tab bar controller.
Please let me know if my solution works and did you find any other way of swiping to second view and having the UI tab bar in place as well.
[EDIT] Actually I dug down a bit more and found out it was easier than I expected. The fix to our problem of tabBar controller not showing up is as follows :
func handleSwipes(sender:UISwipeGestureRecognizer) {
let selectedIndex: Int = self.tabBarController!.selectedIndex
self.tabBarController!.selectedIndex = selectedIndex + 1
}
回答3:
The best way for UITabBarViewController (Swift 3.0) will be next:
func handleSwipes(sender:UISwipeGestureRecognizer) {
if sender.direction == UISwipeGestureRecognizerDirection.left {
self.selectedIndex += 1
} else if sender.direction == UISwipeGestureRecognizerDirection.right {
self.selectedIndex -= 1
}
}
回答4:
You should probably use UITabBarControllerDelegate methods to accomplish this. There are new delegate methods on UITabBarControllerDelegate that lets you return UIViewControllerAnimatedTransitioning and UIViewControllerInteractiveTransitioning.
These are the delegate methods that you would use,
- (id <UIViewControllerInteractiveTransitioning>)tabBarController:(UITabBarController *)tabBarController
interactionControllerForAnimationController: (id <UIViewControllerAnimatedTransitioning>)animationController;
- (id <UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController
animationControllerForTransitionFromViewController:(UIViewController *)fromVC
toViewController:(UIViewController *)toVC;
The Swift code for the delegate methods look like this,
func tabBarController(tabBarController: UITabBarController, interactionControllerForAnimationController animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning?
func tabBarController(tabBarController: UITabBarController, animationControllerForTransitionFromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?
回答5:
copy ad paste this
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)
}
}
来源:https://stackoverflow.com/questions/31783044/want-to-navigate-between-tab-bars-by-using-swipe-gesters-in-swift