我正在使用 XCode 为 iOS 平台开发 Cocoa 触摸应用程序,但无法找到如何实现滑动手势,该手势将允许用户向左或向右滑动手指以更改为新的ViewController
(nib/xib 文件)。我做了一个swapView IBAction
使用按钮和模式转换,我读过有关苹果的TouchGestureRecognizer
但我不知道如何实现允许视图更改的滑动操作。
我不想使用滚动视图,因为我有几十个视图控制器,我希望用户能够滑动。
这是一个例子:
第一个视图Controller.xib:
SwipeRight- 转到第二个 View Controller.xib
第二个视图控制器.xib:
SwipeLeft- 转到第一个视图 Controller.xib
SwipeRight- 转到第三个视图 Controller.xib
etc, etc
我以前没有使用过 UISwipe/Touch Gestures,但我使用过IBAction
使用模态转换按钮切换视图的方法(见下文):
-(IBAction)swapViews; {
SecondViewController *second2 =[[SecondViewController alloc initWithNibName:@"SecondViewController" bundle:nil];
second2.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:second2 animated:YES];
[second2 release];
}
使用滑动来执行类似的方法是否具有不同的格式?如果是这样,我该如何整理并格式化它。
谢谢
编辑 - 根据问题评论回答
将其放入您的 viewDidLoad 中
UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftDetected:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];
然后添加一个选择器,将以下代码粘贴到您的主...
- (IBAction)swipeLeftDetected:(UIGestureRecognizer *)sender {
NC2ViewController *second2 =[[NC2ViewController alloc] initWithNibName:@"NC2ViewController" bundle:nil];
second2.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:second2 animated:YES];
[second2 release];
}
然后确保导入要交换到的 otherViewController
#import "SecondViewController"
在主文件的顶部。希望这可以帮助。
End Edit