iOS:截取从顶视图到底视图的轻击手势事件

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

- (void)viewDidLoad {
    [super viewDidLoad];

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

    [self.view addGestureRecognizer:_tapOnVideoRecognizer];

    UIView *smallView=[[UIView alloc] initWithFrame:CGRectMake(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的手势事件?

解决方法

像这样实现UIGestureRecognizer委托方法shouldReceivetouch.如果触摸位置在topView内,请勿接触.

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

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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...