且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在子视图中检测点按手势

更新时间: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 UILabels have this set to NO by default. So prior to the above:

self.subviewOfInterest.userInteractionEnabled = YES;