Views Navigation Using Swipe Gesture

后端 未结 5 456
南旧
南旧 2020-12-07 13:47

How can I switch views vertically using swipe gestures ?

5条回答
  •  醉酒成梦
    2020-12-07 13:53

    Certainly! Just set your viewController to be the UIGestureRecognizerDelegate and declare UISwipeGestureRecognizer *swipeLeftRecognizer; (also retain and synthesize). Then, in the implementation, set up the recognizers with

        UIGestureRecognizer *recognizer;
        // RIGHT SWIPE
        recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self 
                                                               action:@selector(handleSwipeFrom:)];
        [self.view addGestureRecognizer:recognizer];
        [recognizer release];
        // LEFT SWIPE
        recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
        self.swipeLeftRecognizer = (UISwipeGestureRecognizer *)recognizer;
    swipeLeftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
        [self.view addGestureRecognizer:swipeLeftRecognizer];
    self.swipeLeftRecognizer = (UISwipeGestureRecognizer *)recognizer;
        [recognizer release];
    

    Then trigger the actions you want with the method

    - (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
        if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
            // load a different viewController
        } else {
            // load an even different viewController
        }
    }
    

    What you do here is specific to your app. You can switch the tabBar selection, jump through a navigationController, present a different view modally, or just do a simple slide in transition.

提交回复
热议问题