I want to swipe in my app to push or pop ViewControllers , I know I can add a swipe gesture to that. But When I swipe the screen ,I want the current ViewController followed with my swipe gesture ,and next ViewController pushes in just with the swipe. How can I do this,Thank you!
-
The perfect working solution with code and explanation:- http://stackoverflow.com/a/32990248/988169 – pkc456 Oct 07 '15 at 10:49
3 Answers
Not possible with UINavigationController
; you'll have to build your own navigation controller (should be pretty easy) that incorporates a UIPanGestureRecognizer
.
EDIT for iOS 7: You'll probably want to use a UIScreenEdgePanGestureRecognizer
.

- 5,345
- 4
- 32
- 49
Gesture recognizers have action methods just like buttons. Just put this in the action method:
NextViewController *next = [[NextViewController alloc] init ....];
[self.navigationController pushViewController:next animated:YES];

- 103,982
- 12
- 207
- 218
-
-
@lixiaoyu, well, then that's not a "push" or "pop" in the usual sense, and you need to do it with a pan rather than a swipe, because a swipe is a discrete gesture. – rdelmar Mar 21 '13 at 06:37
Read about gesture recognizers. You can create them and add it on objects. For example
UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:@selector(longPress:)];
longTap.minimumPressDuration = 1.0;
[cell addGestureRecognizer:longTap];
Here I have created LongPress recognizer and added it on my cell. If I will longpress(1.0 sec) on my cell, it will call selector(longPress:). In long press I can make any code.
-(void)longPress : (UILongPressGestureRecognizer *)rec {
if (rec.state == UIGestureRecognizerStateBegan) {
NSLog (@"I've longPressed");
}
}
You can use different recognizers by the same way.
About push and pop. They are methods of Navigation controller. Push - goes forward, on controller which you shows him;
NextController *goNext = [[NextViewController alloc] init];
[self.navigationController pushViewController:goNext animated:YES];
it will go to the NextController. Pop - backs to the previous controller.
Here you don't need to show the previous controller. Just say to navController back
[self.navigationController popViewControllerAnimated:YES];

- 1,740
- 3
- 16
- 36