如何使用滑动手势XCode交换视图 [英] How to swap views using a swipe gesture XCode
问题描述
我使用XCode为iOS平台开发Cocoa touch应用程序,但是却遇到了如何实现滑动手势的问题,允许用户向左或向右滑动手指以更改为新的 ViewController
(nib / xib文件)。我已经做了一个 swapView IBAction
使用按钮和模态转换,我已经阅读关于苹果的 TouchGestureRecognizer
但我不知道如何实现允许视图更改的滑动操作。
我不想使用滚动视图,因为我有几打视图控制器,我想要
第一个视图Controller.xib:用户可以通过滑动来移动。
SwipeRight-转到第二个视图Controller.xib
第二个视图Controller.xib:
SwipeLeft-转到第一个视图Controller.xib
SwipeRight-转到第三个视图Controller.xib
等,
没有使用UISwipe /触摸手势之前,但我已经使用 IBAction
方法切换视图使用按钮模态转换(见下文):
- (IBAction)swapViews; {
SecondViewController * second2 = [[SecondViewController alloc initWithNibName:@SecondViewControllerbundle: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];
然后通过将以下代码粘贴到您的主...添加一个选择器
$ b2$ b
- (IBAction)swipeLeftDetected:(UIGestureRecognizer *)sender {
NC2ViewController * second2 = [[NC2ViewController alloc] initWithNibName:@NC2ViewController bundle:nil];
second2.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:second2 animated:YES];
[second2 release];
}
然后只需确保导入要交换的otherViewController >
#importSecondViewController
在主文件的顶部。希望这有帮助。
结束编辑
这听起来像是一个完美的时候使用 UIGestureRecognizer ,或更具体地说, UISwipeGestureRecognizer 。
有关如何使用它们的详细信息,请阅读手势识别器部分的事件处理指南。 p>
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
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.
这篇关于如何使用滑动手势XCode交换视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!