IOS: intercept event gestures when clicking on a top-down view

In my opinion, the controller, I am adding a UITapGestureRecognizer to self.view. And I add a little review on top of self.view. When I click on a small view, I do not want to fire the UITapGestureRecognizer event in self.view. Here is my code, it does not work.

    - (void)viewDidLoad {
    [super viewDidLoad];

    UITapGestureRecognizer *_tapOnVideoRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toggleControlsVisible)];

    [self.view addGestureRecognizer:_tapOnVideoRecognizer];

    UIView *smallView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    smallView.backgroundColor=[UIColor redColor];
    smallView.exclusiveTouch=YES;
    smallView.userInteractionEnabled=YES;

    [self.view addSubview:smallView];
    }

    - (void)toggleControlsVisible
    {
        NSLog(@"tapped");
    }

When I click the small view, it still fires the tap event in self.view. Xcode logs are "tapped." How to capture gesture events from smallView to self.view?

+4
source share
1 answer

Enter UIGestureRecognizerthe delegate method shouldReceiveTouchas follows. If the touch location is inside the topView, you donโ€™t get the touch.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    CGPoint location = [touch locationInView:self.view];

    if (CGRectContainsPoint(self.topView.frame, location)) {
        return NO;
    }
   return YES;
}
+7

Source: https://habr.com/ru/post/1569097/


All Articles