Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swipe gesture between view controllers in storyboards

I am looking to add a left and right swipe gesture to change between view controllers, is this possible and is there an easy way to do this in storyboards? Thanks :)

like image 637
David Avatar asked Dec 26 '22 11:12

David


2 Answers

Storyboards allow you to set up segues between two view controllers. I would say start by attaching segues between the views, give it an identifier.

Then use something like

UISwipeGestureRecognizer * recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(myRightAction)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self addGestureRecognizer:recognizer];

to add a swipe recognizer to a view.

Then in the myRightAction method you'd want to do your segue call like

[self performSegueWithIdentifier: @"MySegue" sender: self];

to go to a view or dismiss it to leave the view.

You can use Navigation View Controllers to have a right and left transition animation. You can also make custom segues.

like image 110
Richard C. Avatar answered Jan 13 '23 16:01

Richard C.


on Xcode 5 it is simpler, less code needed:

storyboard -> utility panel -> object Library -> drag and drop the Swipe Gesture Recognizer inside your scene

you MUST drag it into the view itself(where the background color appears) so you can see this connection: referencing outlet collections -> view

add the delegate by dragging the delegate's connection pin to the viewController

Show assistant editor -> add an action by ctrl + drag and drop on your delegate class viewController.m

inside that action write

[self performSegueWithIdentifier:@"mySegueIdentifier" sender:self];
like image 21
Enrico Cupellini Avatar answered Jan 13 '23 14:01

Enrico Cupellini