简体   繁体   中英

How to swipe left or right to push or pop ViewContrller in iOS

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!

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 .

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM