簡體   English   中英

如何向左或向右滑動以在iOS中推送或彈出ViewContrller

[英]How to swipe left or right to push or pop ViewContrller in iOS

我想在我的應用程序中滑動以推送或彈出ViewControllers,我知道我可以添加一個輕掃手勢。 但是當我滑動屏幕時,我希望當前的ViewController跟隨我的滑動手勢,然后下一個ViewController只需輕掃即可。 我怎么能這樣做,謝謝!

UINavigationController無法實現; 你必須建立自己的導航控制器(應該很簡單),它包含一個UIPanGestureRecognizer

編輯iOS 7:您可能想要使用UIScreenEdgePanGestureRecognizer

手勢識別器就像按鈕一樣有動作方法。 把它放在動作方法中:

NextViewController *next = [[NextViewController alloc] init ....];
[self.navigationController pushViewController:next animated:YES];

閱讀手勢識別器。 您可以創建它們並將其添加到對象上。 例如

UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] 
                                         initWithTarget:self 
                                         action:@selector(longPress:)];
longTap.minimumPressDuration = 1.0;
[cell addGestureRecognizer:longTap];

在這里,我創建了LongPress識別器並將其添加到我的單元格中。 如果我將在我的單元格上長按(1.0秒),它將調用選擇器(longPress :)。 長按我可以制作任何代碼。

-(void)longPress : (UILongPressGestureRecognizer *)rec {
   if (rec.state == UIGestureRecognizerStateBegan) {
   NSLog (@"I've longPressed");
   }
}

您可以通過相同的方式使用不同的識別器。

關於推送和流行。 它們是導航控制器的方法。 推 - 前進,在你給他看的控制器上;

NextController *goNext = [[NextViewController alloc] init];
[self.navigationController pushViewController:goNext animated:YES];

它將轉到NextController。 彈出 - 返回上一個控制器。

在這里,您不需要顯示以前的控制器。 只需對navController說回來

 [self.navigationController popViewControllerAnimated:YES];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM