Is it possible to swipe left or right anywhere on the screen to switch tabs in iOS? Thanks
Example 1: Switching between months on a calender by simply swiping left/right Example 2: start at 0:12 http://www.youtube.com/watch?v=5iX4vcsSst8
Tap the “Tabs” button (two cascading squares) on the right (top or bottom, depending on where you've placed the tab bar). It'll open a card-style grid of Safari tabs. You can scroll and tap to switch to any tab, and it's quicker than swiping through other open tabs. That's it!
If you are using a tab bar controller, you could set up a swipe gesture recognizer on each tab's view. When the gesture recognizer is triggered, it can change the tabBarController.selectedTabIndex
This effect will not be animated, but it will switch the tabs with a swipe gesture. This was approximately what I used when I had an app with a UITabBar with buttons on the left and right side and swipe gestures to change the active tab.
- (void)viewDidLoad
{
[super viewDidLoad];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(tappedRightButton:)];
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.view addGestureRecognizer:swipeLeft];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(tappedLeftButton:)];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[self.view addGestureRecognizer:swipeRight];
}
- (IBAction)tappedRightButton:(id)sender
{
NSUInteger selectedIndex = [rootVC.tabBarController selectedIndex];
[rootVC.tabBarController setSelectedIndex:selectedIndex + 1];
}
- (IBAction)tappedLeftButton:(id)sender
{
NSUInteger selectedIndex = [rootVC.tabBarController selectedIndex];
[rootVC.tabBarController setSelectedIndex:selectedIndex - 1];
}
Assuming you are using UITabBarConroller
All your child ViewControllers can Inherit from a class that does all the heavy lifting for you.
This is how I have done it
class SwipableTabVC : 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)
}
func swipeLeft() {
let total = self.tabBarController!.viewControllers!.count - 1
tabBarController!.selectedIndex = min(total, tabBarController!.selectedIndex + 1)
}
func swipeRight() {
tabBarController!.selectedIndex = max(0, tabBarController!.selectedIndex - 1)
}
}
So all your viewcontrollers that are part of UITabControllers can inherit from SwipableTabVC
instead of UIViewController.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With