简体   繁体   中英

How can I except a part from a UIView for a gesture recognizer?

I am developing an app and I want to scroll through different UIViewControllers with a UISwipeGestureRecognizer . Everything works, but there is one thing where I couldn't find a solution for.

I add the UIGestureRecognizers like this

- (void)viewDidLoad {
[super viewDidLoad];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft)];
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.view addGestureRecognizer:swipeLeft];

UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRight)];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[self.view addGestureRecognizer:swipeRight];
}

Now I want to know if it is possible to except a part of the self.view where it is possible to swipe to a different view. I want to do this around a UISwitch .

Can someone please help me?

Best regards,

Lukas Goes

You can use another UIView and add it to self.view . Those UISwipeGestureRecognizer won't work inside that UIView .

Add another UISwipeGestureRecognizer to the switch, and, on your original swipe gesture recognisers, call requireGestureRecognizerToFail: on the switch's new recognizer.

Hope that makes sense, it's such a verbose class... :\\

I suggest you to use the delegate method of a gesture recognizer : gestureRecognizer:shouldReceiveTouch: . In the touch parameter, you have many information, like the position in the screen. You can use this position and test if it is above the switch frame. Return yes or no depending of if you want to trigger the selector associated to the gesture recognizer or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM