简体   繁体   English

UIView子视图向前触摸

[英]UIView subview touch forward

I have two custom UIViews (A and B respectively) each with a frame set to the same origin and same size. 我有两个自定义UIView(分别为A和B),每个框架都设置为相同的原点和相同的大小。 They are both added to a parent UIView (C) as subviews. 它们都作为子视图添加到父UIView(C)。

I have a touch recognizer as part of A and B that listen for touches on certain spots. 作为A和B的一部分,我有一个触摸识别器,用于侦听某些位置上的触摸。 If that touch is received, it raises a delegate up to the parent UIView saying it has been touched. 如果收到该触摸,它将引发一个委托,直到父UIView声明已被触摸。 The issue is that since B is added after A, B never receives a touch event. 问题在于,由于B是在A之后添加的,因此B永远不会收到触摸事件。

The A and B UIViews are single lines with endpoints. A和B UIView是带有端点的单行。 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. 这就像排他性的事情。

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正在接收触摸事件,而B虽然不是C的孩子,那么这是因为A声称自己拥有所有触摸事件,包括针对B的事件。

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: 解决方案是重写A上的pointInside:withEvent:方法,以确定它是否应自行接受事件,如下所示:

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    if ([self pointIsInHotspot:point]) return YES;
    return NO;
}

Where pointIsInHotspot is whatever tests you need to perform in order to determine if the point is on an active area within the view. 其中pointIsInHotspot是您需要执行的任何测试,以确定该点是否在视图内的活动区域上。

在手势识别器cancelsTouchesInView设置为NO。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM