userInteractionEnabled

userInteractionEnabled

本文介绍了当父视图在 iOS 中将 userInteractionEnabled 设置为 NO 时如何进行触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当父视图的 userInteractionEnabled=NO 时,其子视图将不接受触摸事件,即使它们的 userInteractionEnabled 属性设置为 YES.

When the parent view has userInteractionEnabled=NO, its subviews will not accept touch events even if their userInteractionEnabled property is set to YES.

有没有办法在子视图中仍然获取触摸事件?

Is there any way to still get touch events in subviews?

推荐答案

要获得一个让触摸通过但让其子视图处理触摸的视图,让该视图上的 userInteractionEnabled 为 YES,然后使用以下代码段:

To get a view to let touches pass-through but give its subviews handle touches, let userInteractionEnabled on that view to YES and, instead, use this snippet:

-(id)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    id hitView = [super hitTest:point withEvent:event];
    if (hitView == self) return nil;
    else return hitView;
}

来源:http://cocoaheads.tumblr.com/post/2130871776/ignore-touches-to-uiview-subclass-but-not-to-its

这篇关于当父视图在 iOS 中将 userInteractionEnabled 设置为 NO 时如何进行触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-19 13:41