如何使用滑动手势 XCode 交换视图
我正在使用 XCode 为 iOS 平台开发 Cocoa 触摸应用程序,但无法找到如何实现滑动手势,以允许用户向左或向右滑动手指以更改为新的 ViewController(nib/xib 文件)。我已经使用按钮和模式转换完成了
swapView IBAction
,并且我已阅读有关 Apple 的 TouchGestureRecognizer
的内容,但我不知道如何实现允许视图的滑动操作改变。
我不想使用滚动视图,因为我有几十个视图控制器,我希望用户能够滑动。
这是一个示例:
First View Controller.xib: SwipeRight- 转到第二个 View Controller.xib
第二个 View Controller.xib:
SwipeLeft- 转到第一个视图 Controller.xib
SwipeRight- 转到第三个 View Controller.xib
等
Touch 手势,但我使用过 IBAction 方法通过模态转换按钮来切换视图(见下文):
-(IBAction)swapViews; {
SecondViewController *second2 =[[SecondViewController alloc initWithNibName:@"SecondViewController" bundle:nil];
second2.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:second2 animated:YES];
[second2 release];
}
我之前没有使用过 UISwipe / 使用滑动来执行类似的方法但格式不同?如果是这样,我该如何整理并格式化它。
谢谢
编辑 - 根据问题的评论回答
将其放入您的 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"
在主文件的顶部 。希望这有帮助。
结束编辑
I am using XCode to develop a Cocoa touch application for the iOS platform but have had trouble finding out how to get a swipe gesture implemented that would allow the user to swipe their finger left or right to change to a new ViewController
(nib/xib file). I have done a swapView IBAction
using a button and modal transitioning and I have read about Apple's TouchGestureRecognizer
but I don't know how to implement a swipe action that would allow a view change.
I do NOT want to use a scroll view, as I have several dozen view controllers, that I want the user to be able to swipe through.
Here is an example:
First View Controller.xib:
SwipeRight- Go to second View Controller.xib
Second View Controller.xib:
SwipeLeft- Go to first View Controller.xib
SwipeRight- Go to third View Controller.xib
etc, etc
I have not used UISwipe/Touch Gestures before but I have used an IBAction
method to switch views using a button with Modal Transitioning (see below):
-(IBAction)swapViews; {
SecondViewController *second2 =[[SecondViewController alloc initWithNibName:@"SecondViewController" bundle:nil];
second2.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:second2 animated:YES];
[second2 release];
}
Is using a swipe to do a similar method formatted differently? If so, how do I sort this out and format it.
Thank You
Edit - Answer as Per Comment on Question
Place this in your viewDidLoad
UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftDetected:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];
Then add a selector as by pasting the following code into your main...
- (IBAction)swipeLeftDetected:(UIGestureRecognizer *)sender {
NC2ViewController *second2 =[[NC2ViewController alloc] initWithNibName:@"NC2ViewController" bundle:nil];
second2.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:second2 animated:YES];
[second2 release];
}
Then just make sure you import the otherViewController you are swapping to using
#import "SecondViewController"
at the top of your main file. Hope this helps.
End Edit
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这听起来是使用 UIGestureRecognizer< 的最佳时机/a> 或者,更具体地说, UISwipeGestureRecognizer。
有关如何使用它们的更多信息,请阅读 手势识别器 部分。
This sounds like a perfect time to use UIGestureRecognizer or, more specifically, UISwipeGestureRecognizer.
For more info on how to use them, read up in the Gesture Recognizers section of the Event Handling Guide.
假设您想向左滑动以从右侧调出另一个视图。
在故事板中,拖放滑动手势识别器。它将在视图控制器下方创建一个图标;将此图标拖放到您想要导航到的 ViewController 上。这将添加一个segue,选择自定义segue。然后创建一个 UIStoryboardSegue 类。添加以下代码:
Lets assume you want to swipe left to bring up another view from the right.
In the storyboard, drag and drop a swipe gesture recognizer. It will make an icon below the view controller; drag this icon and drop onto the ViewController you want to navigate to. This will add a segue, select custom segue. Then create a UIStoryboardSegue class. Add the following code: