简体   繁体   English

如何向左或向右滑动以在iOS中推送或弹出ViewContrller

[英]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. 我想在我的应用程序中滑动以推送或弹出ViewControllers,我知道我可以添加一个轻扫手势。 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. 但是当我滑动屏幕时,我希望当前的ViewController跟随我的滑动手势,然后下一个ViewController只需轻扫即可。 How can I do this,Thank you! 我怎么能这样做,谢谢!

Not possible with UINavigationController ; UINavigationController无法实现; you'll have to build your own navigation controller (should be pretty easy) that incorporates a UIPanGestureRecognizer . 你必须建立自己的导航控制器(应该很简单),它包含一个UIPanGestureRecognizer

EDIT for iOS 7: You'll probably want to use a UIScreenEdgePanGestureRecognizer . 编辑iOS 7:您可能想要使用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. 在这里,我创建了LongPress识别器并将其添加到我的单元格中。 If I will longpress(1.0 sec) on my cell, it will call selector(longPress:). 如果我将在我的单元格上长按(1.0秒),它将调用选择器(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. 它将转到NextController。 Pop - backs to the previous controller. 弹出 - 返回上一个控制器。

Here you don't need to show the previous controller. 在这里,您不需要显示以前的控制器。 Just say to navController back 只需对navController说回来

 [self.navigationController popViewControllerAnimated:YES];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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