OGeek|极客世界-中国程序员成长平台

标题: iphone - 如何防止点击事件传递到superview? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:41
标题: iphone - 如何防止点击事件传递到superview?

有一个名为viewA的UIView,viewA有一个UITapGestureRecognizer,我无法修改识别器的目标代码。
viewA 有很多 subview 。对于某些特定的 subview ,我希望它们在我触摸它们时不会将点击事件传递给 viewA(对于其他 subview ,它们应该将点击事件传递给 viewA)。我该怎么办?



Best Answer-推荐答案


将 viewA 设置为识别器的委托(delegate)。然后使用委托(delegate)方法:

- (BOOL)gestureRecognizerUIGestureRecognizer *)gestureRecognizer shouldReceiveTouchUITouch *)touch

检查触摸是否在不需要的 subview 中:

for(UIView *v in unwantedSubviewsArray){
    CGPoint touchLocation = [touch locationInView:v];
    if (CGRectContainsPoint(v.frame, touchLocation)){
        return NO;
    }
}
return YES;

关于iphone - 如何防止点击事件传递到superview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8417597/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4