How to swap views using a swipe gesture XCode

后端 未结 2 1559
夕颜
夕颜 2021-02-06 16:59

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 s

相关标签:
2条回答
  • 2021-02-06 17:09

    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:

    - (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];
                         }];
    }
    
    0 讨论(0)
  • 2021-02-06 17:22

    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.

    0 讨论(0)
提交回复
热议问题