菜鸟教程小白 发表于 2022-12-11 20:41:13

iphone - 如何防止点击事件传递到superview?


                                            <p><p>有一个名为viewA的UIView,viewA有一个UITapGestureRecognizer,我无法修改识别器的目标代码。<br/>
viewA 有很多 subview 。对于某些特定的 subview ,我希望它们在我触摸它们时不会将点击事件传递给 viewA(对于其他 subview ,它们应该将点击事件传递给 viewA)。我该怎么办?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>将 viewA 设置为识别器的委托(delegate)。然后使用委托(delegate)方法:</p>

<pre><code>- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
</code></pre>

<p>检查触摸是否在不需要的 subview 中:</p>

<pre><code>for(UIView *v in unwantedSubviewsArray){
    CGPoint touchLocation = ;
    if (CGRectContainsPoint(v.frame, touchLocation)){
      return NO;
    }
}
return YES;
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于iphone - 如何防止点击事件传递到superview?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/8417597/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/8417597/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iphone - 如何防止点击事件传递到superview?