iOS:截获从顶视图到底视图的点击手势事件

iOS:截获从顶视图到底视图的点击手势事件,ios,objective-c,cocoa-touch,Ios,Objective C,Cocoa Touch,在我的视图控制器中,我向self.view添加了一个UITapGestureRecognitor。我在self.view上面添加了一个小视图。当我点击小视图时,我不想在self.view中触发UITapgestureRecognitor事件。这是我的代码,它不工作 - (void)viewDidLoad { [super viewDidLoad]; UITapGestureRecognizer *_tapOnVideoRecognizer = [[UITapGestur

在我的视图控制器中,我向self.view添加了一个UITapGestureRecognitor。我在self.view上面添加了一个小视图。当我点击小视图时,我不想在self.view中触发UITapgestureRecognitor事件。这是我的代码,它不工作

    - (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");
    }

当我点击小视图时,它仍然会触发self.view中的点击事件。Xcode日志被“点击”。如何截获从smallView到self.view的手势事件?

实现
UIGestureRecognitor
代理方法
应该像这样接收触摸。如果触摸位置在topView内,则不接收触摸

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

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

非常感谢。这很有效。但是为什么smallView不能像UIButton那样截获点击事件呢?@nimingzhe2008您需要在
smallView
中添加一个
UIAPTestureRecognitizer
。(不同之处在于,
UIButton
s本身就内置了一个
uitagesturerecognizer
),但gabbler的解决方案更干净。它的工作原理与Lyndsey Scott所提到的一样,并且:只有在同一窗口中没有其他视图与之相关联的触摸时,排他性触摸视图才会收到触摸;一旦一个排他性的触摸视图接收到触摸,那么当该触摸存在时,同一窗口中的其他视图不会接收任何触摸。