如何使用滑动手势 XCode 交换视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5684099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:53:40  来源:igfitidea点击:

How to swap views using a swipe gesture XCode

iphonecocoa-touchxcodeviewswipe

提问by AppleFanBoy

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 IBActionusing a button and modal transitioning and I have read about Apple's TouchGestureRecognizerbut I don't know how to implement a swipe action that would allow a view change.

我正在使用 XCode 为 iOS 平台开发 Cocoa 触摸应用程序,但无法找到如何实现滑动手势以允许用户向左或向右滑动手指以更改为新的ViewController(nib/xib 文件) . 我已经完成了swapView IBAction使用按钮和模式转换的操作,并且我已经阅读了有关 Apple 的内容,TouchGestureRecognizer但我不知道如何实现允许视图更改的滑动操作。

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

第一个 View Controller.xib:SwipeRight- 转到第二个 View Controller.xib

Second View Controller.xib:
SwipeLeft- Go to first View Controller.xib
SwipeRight- Go to third View Controller.xib

第二个 View Controller.xib:
SwipeLeft- 转到第一个 View Controller.xib
SwipeRight- 转到第三个 View Controller.xib

etc, etc

等等等等

I have not used UISwipe/Touch Gestures before but I have used an IBActionmethod to switch views using a button with Modal Transitioning (see below):

我之前没有使用过 UISwipe/Touch Gestures,但我使用了IBAction一种使用带有模态转换的按钮来切换视图的方法(见下文):

-(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

将此放在您的 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

然后只需确保导入您要交换使用的 otherViewController

#import "SecondViewController"

at the top of your main file. Hope this helps.

在主文件的顶部。希望这可以帮助。

End Edit

结束编辑

采纳答案by Simon Goldeen

This sounds like a perfect time to use UIGestureRecognizeror, more specifically, UISwipeGestureRecognizer.

这听起来是使用UIGestureRecognizer或更具体地说UISwipeGestureRecognizer 的最佳时机

For more info on how to use them, read up in the Gesture Recognizerssection of the Event Handling Guide.

有关如何使用它们的更多信息,请阅读事件处理指南的手势识别器部分。

回答by anivader

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:

在故事板中,拖放滑动手势识别器。它将在视图控制器下方创建一个图标;将此图标拖放到您要导航到的 ViewController 上。这将添加一个转场,选择自定义转场。然后创建一个 UIStoryboardSegue 类。添加以下代码:

- (void)perform {
    UIViewController* source = (UIViewController *)self.sourceViewController;
    UIViewController* destination = (UIViewController *)self.destinationViewController;

    CGRect sourceFrame = source.view.frame;
    sourceFrame.origin.x = -sourceFrame.size.width;

    CGRect destFrame = destination.view.frame;
    destFrame.origin.x = destination.view.frame.size.width;
    destination.view.frame = destFrame;

    destFrame.origin.x = 0;

    [source.view.superview addSubview:destination.view];

    [UIView animateWithDuration:0.5
                     animations:^{
                         source.view.frame = sourceFrame;
                         destination.view.frame = destFrame;
                     }
                     completion:^(BOOL finished) {
                         UIWindow *window = source.view.window;
                         [window setRootViewController:destination];
                     }];
}