您的位置:首页 > 移动开发 > IOS开发

iOS View添加手势,防止View上点击其他视图触发点击效果

2016-10-14 09:15 537 查看
在开发过程中,我们可能会遇到这个问题. 当我们给一个
view
添加了手势,但是我们又不想点击
view
上面的视图也触发手势.如下图:
我们在
红色view
上添加了手势,但是又不想点击
黄色view
也触发.其实这里用到
UITapGestureRecognizer
的一个代理方法

0.png



上代码,先创建两个
view
,并且给
bigView
添加手势

self.bigView = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];
self.bigView.backgroundColor = [UIColor redColor];
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(bigMap:)];
recognizer.delegate = self;
[self.bigView addGestureRecognizer:recognizer];
[self.view addSubview:self.bigView];

self.smallView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
self.smallView.backgroundColor = [UIColor yellowColor];
[self.bigView addSubview:self.smallView];实现
UITapGestureRecognizer
的一个代理方法,我不用多说,大家一看就明白怎么回事了.这是就解决了防止点击
黄色view
也触发的问题了

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{

if ([touch.view isDescendantOfView:self.smallView]) {
return NO;
}
return YES;
}是不是很简单啊.最后送大家一个我自己用无人机拍摄的小视频.

文章转自 Senior丶的简书
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS view 手势 点击
相关文章推荐