user1689272
user1689272

Reputation: 195

iOS: Swipe left/right between tabs possible?

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

Upvotes: 15

Views: 29447

Answers (5)

Kaunteya
Kaunteya

Reputation: 3090

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 leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(onSwipeLeft))
        leftSwipe.direction = .left
        self.view.addGestureRecognizer(leftSwipe)

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

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

    }

    func onSwipeRight() {
        tabBarController!.selectedIndex = max(0, tabBarController!.selectedIndex - 1)
    }
}

With this, all the viewcontrollers that are part of UITabController must inherit from SwipableTabVC instead of UIViewController.

Upvotes: 10

Ali
Ali

Reputation: 514

Try this,

- (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];

    [self.tabBarController setSelectedIndex:selectedIndex + 1];

    //To animate use this code
    CATransition *anim= [CATransition animation];
    [anim setType:kCATransitionPush];
    [anim setSubtype:kCATransitionFromRight];
    [anim setDuration:1];
    [anim setTimingFunction:[CAMediaTimingFunction functionWithName:
                                  kCAMediaTimingFunctionEaseIn]];
    [self.tabBarController.view.layer addAnimation:anim forKey:@"fadeTransition"];
} 

- (IBAction)tappedLeftButton:(id)sender
{
    NSUInteger selectedIndex = [rootVC.tabBarController selectedIndex];

    [self.tabBarController setSelectedIndex:selectedIndex - 1]; 

    CATransition *anim= [CATransition animation];
    [anim setType:kCATransitionPush];
    [anim setSubtype:kCATransitionFromRight];

    [anim setDuration:1];
    [anim setTimingFunction:[CAMediaTimingFunction functionWithName:
                              kCAMediaTimingFunctionEaseIn]];
    [self.tabBarController.view.layer addAnimation:anim forKey:@"fadeTransition"];
}

Upvotes: 7

cwRichardKim
cwRichardKim

Reputation: 1060

I would recommend embedding the entire thing into a pageviewcontroller like so: https://github.com/cwRichardKim/RKSwipeBetweenViewControllers

Upvotes: 3

Sean Kladek
Sean Kladek

Reputation: 4446

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]; 
}

Upvotes: 20

DBD
DBD

Reputation: 23233

Sure, it's possible.

Every screen would need to have a UISwipeGestureRecognizerfor the swipes, then make a call to the tab bar perform the desired action. Which could be anything from incrementing or decrementing the active tab to anything you want.

For the sake of code duplication prevention, you could create a custom UIViewController and have all your view controllers inherit from there (or a couple other ways).

Upvotes: 1

Related Questions