更新时间:2023-01-25 14:27:37
当您使用 -locationInView:。然后,在UIView上使用以下方法:
- (UIView *)hitTest:(CGPoint)指向withEvent:(UIEvent *)事件
以获取对实际子视图的引用被点击,记住您传入的点与视图位于同一坐标空间。
You can grab the point of the tap off the gesture recognizer when your handler method is called respective to any view you wish using -locationInView:
. Then, use the following method on UIView: - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
to get a reference to the actual sub view that was tapped remembering that the point you pass in is in the same coordinate space as the view.
一些代码可以帮助您入门:
Some code to get you started:
CGPoint point = [tapGestureRecognizer locationInView:parentView];
UIView *tappedView = [parentView hitTest:point withEvent:nil];
要使命中测试生效,视图需要 userInteractionEnabled
属性设置为是
。许多视图,例如 UILabel
,默认设置为否
。所以在上述之前:
For hit testing to work the view needs to have the userInteractionEnabled
property set to YES
. Many views, such as UILabel
s have this set to NO
by default. So prior to the above:
self.subviewOfInterest.userInteractionEnabled = YES;