UIView 子视图向前触摸 [英] UIView subview touch forward
问题描述
我有两个自定义 UIView(分别为 A 和 B),每个都将框架设置为相同的原点和相同的大小.它们都作为子视图添加到父 UIView (C).
I have two custom UIViews (A and B respectively) each with a frame set to the same origin and same size. They are both added to a parent UIView (C) as subviews.
我有一个触摸识别器作为 A 和 B 的一部分,可以侦听某些位置的触摸.如果接收到该触摸,它会向父 UIView 提出一个委托,表示它已被触摸.问题在于,由于 B 是在 A 之后添加的,因此 B 永远不会收到触摸事件.
I have a touch recognizer as part of A and B that listen for touches on certain spots. If that touch is received, it raises a delegate up to the parent UIView saying it has been touched. The issue is that since B is added after A, B never receives a touch event.
A 和 B UIViews 是带有端点的单行.它们都识别端点上的触摸并发出委托通知.如何添加子视图并识别顶部带有父视图的触摸?这就像一个排他性的东西.
The A and B UIViews are single lines with endpoints. They both recognize touches on the endpoints and raise up delegate notifications. How do I add subviews and recognize touches on them with parent views on top? It's like an exclusivity thing.
推荐答案
如果 A 正在接收触摸事件而 B 虽然它们都是 C 的孩子,但 B 不是,那是因为 A 为自己声明了所有触摸事件,包括那些供 B 使用的.
If A is receiving touch events and B isn't though they're both children of C, then it's because A is claiming all touch events for itself, including those intended for B.
解决方案是覆盖 A 上的 pointInside:withEvent: 方法,以确定它是否应该为自己接受事件,如下所示:
The solution is to override the pointInside:withEvent: method on A in order to determine whether it should accept the event for itself, like this:
-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
if ([self pointIsInHotspot:point]) return YES;
return NO;
}
其中 pointIsInHotspot 是您需要执行的任何测试,以确定该点是否位于视图内的活动区域.
Where pointIsInHotspot is whatever tests you need to perform in order to determine if the point is on an active area within the view.
这篇关于UIView 子视图向前触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!