Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Swipe left/right between tabs possible?

Tags:

ios

iphone

ipad

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

like image 520
user1689272 Avatar asked Sep 21 '12 15:09

user1689272


People also ask

How do you swipe between tabs in Safari?

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!


2 Answers

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]; 
}
like image 175
Sean Kladek Avatar answered Oct 16 '22 04:10

Sean Kladek


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.

like image 31
Kaunteya Avatar answered Oct 16 '22 03:10

Kaunteya