微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐