我正在使用 XCode 为 iOS 平台开发 Cocoa 触摸应用程序,但无法找到如何实现滑动手势,以允许用户向左或向右滑动手指以更改为新的 ViewController
(nib/xib 文件)。我已经使用按钮和模式转换完成了 swapView IBAction
,并且我已阅读有关 Apple 的 TouchGestureRecognizer
的内容,但我不知道如何实现允许 View 的滑动操作改变。
我不想使用 ScrollView ,因为我有几十个 View Controller ,我希望用户能够滑动。
这是一个例子:
第一个 View Controller .xib: SwipeRight- 转到第二个 View Controller.xib
第二个 View Controller .xib:
SwipeLeft- 转到第一个 View Controller.xib
SwipeRight- 转到第三个 View Controller.xib
等等,等等
我之前没有使用过 UISwipe/Touch 手势,但我使用过 IBAction
方法,通过模态转换按钮来切换 View (见下文):
-(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"
位于主文件的顶部。希望这会有所帮助。
结束编辑
最佳答案
这听起来是使用 UIGestureRecognizer 的最佳时机或者,更具体地说,UISwipeGestureRecognizer .
有关如何使用它们的更多信息,请阅读 Gesture Recognizers事件处理指南部分。
关于iphone - 如何使用滑动手势 XCode 交换 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5684099/