做一个ViewController是A,里面有一个圆形控制按钮B,在B的里面有一个操作控制按钮C
在原来的AViewController里面有一个UITapGestureRecognizer事件,点击会使得B消失。
UITapGestureRecognizer *tapAction = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
[self addGestureRecognizer:tapAction];
在视图C中,使用了UITouch相关方法
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;遇到问题是,在C视图中,如果做点击操作,这时候会被A视图认为是UITapGestureRecognizer事件,这样会使得B视图和C视图消失
解决的办法是:
首先是A视图,使用UIGestureRecognizerDelegate
UITapGestureRecognizer *tapAction = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
tapAction.delegate = self;
[self addGestureRecognizer:tapAction];
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isKindOfClass:[AViewController class]]) {
return YES;
}
return NO;
}
如此既可以解决,两边的事件不会互相影响。
本文介绍了一种在iOS开发中解决点击穿透问题的方法。通过在AViewController中设置UITapGestureRecognizer并指定其代理为自身,可以避免C视图中的点击操作被误识别为关闭B视图的操作。文中详细解释了如何通过实现`gestureRecognizer:shouldReceiveTouch:`方法来过滤不需要的触摸事件。
1357

被折叠的 条评论
为什么被折叠?



