忽略润色背景(超)查看
问题描述:
我应该如何处理,或者说不是处理(忽略),倒是我的背景有何看法?它恰好是我的视图控制器的视图,它具有我想要响应触摸事件的子视图(对象)。为视图设置userInteractionEnabled = NO似乎也会关闭子视图的所有交互。忽略润色背景(超)查看
我目前正在测试用于
if ([[touch view] superview] == self.view)
中的touchesBegan /移动/截止
。但我试图消除一些有条件的测试,所以寻找更好的方法...
答
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;
递归调用-pointInside:withEvent:
。点在框架坐标
如果你在你的背景视图中覆盖它,你可以指定哪个子视图被打 - 你可以懒惰,只要问你的每个子视图,如果他们会返回yes,并且永远不会返回自己(如果全部返回零,则返回零)。例如:
答
感谢您的回答丹,很好的问题。
但是接受的答案有一个错误:hitTesting子视图应该转换为子视图点来完成。 此外,subviewIndex已在'for'之前定义。
由于子视图根据其z索引进行排序,所以迭代应该从最后一个到第一个(请参阅Event handling for iOS - how hitTest:withEvent: and pointInside:withEvent: are related?和How to get UIView hierarchy index ??? (i.e. the depth in between the other subviews))。
下面是更新后的代码:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *hitView = nil;
NSArray *subviews = [self subviews];
int subviewIndex, subviewCount = [subviews count];
for (subviewIndex = subviewCount-1; !hitView && subviewIndex >= 0; subviewIndex--) {
UIView *subview = [subviews objectAtIndex:subviewIndex];
hitView = [subview hitTest:[self convertPoint:point toView:subview] withEvent:event];
}
return hitView;
}