我有以下设置。
+- XXXCustomControl : UIControl -------+
| A |
| +- ContentView -------------------+|
| | ||
| | B ||
| | ||
| +---------------------------------+|
+--------------------------------------+
作为 UIControl 子类的 XXXCustomControl。它包含一个名为 contentView 的 UIView 类型的 subview ,其大小小于控件的区域。
该 View 具有 .userInteractionEnabled = YES
;
我需要将该属性设置为 YES,因为水平 ScrollView 偶尔会被放入其中并且它们需要可滚动。如果父 View (在我们的例子中,内容 View 不允许用户交互,这是从 subview 继承的。)
但与此同时,这个 XXXCustomControl
需要在其内容 View 中不包含 ScrollView 时不仅在区域 A 中而且在区域 B 中都可以点击。
所以我在这里有一个“利益冲突”,因为我要么
1) 将内容 View 设置为 userInteractionEnabled = NO
,然后我可以在 A 和 B 中点击内容 View 区域中的空控件,但是我将放置在那里的 ScrollView 不会可滚动..
2) 将内容 View 设置为 userInteractionEnabled = YES
但是,如果控件为空,我只能点击区域 A 来触发触摸事件。
我想出的一个想法是,我将该属性默认设置为 NO,当我填充 contentView 时,我将其设置为 yes。当我清除 contentView 时,我将属性设置回否。 基本上我希望它一直设置为 yes,当它为空时,强制 contentView 将 touchUpInside 事件传递到它的 super View 。
这可能吗?
最佳答案
您可以尝试覆盖 pointInside:withEvent:
方法在你的内心深处。这将允许您在想要将触摸转发到 super View 时返回 NO:
-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
if( /* You have content, and you want to receive touches */){
return YES;
}else{
return NO;
}
}
关于ios - 当 userInteractionEnabled = YES 时,如何将触摸事件传递给 superview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23497457/