How to detect a tap gesture in subviews

后端 未结 3 788
梦谈多话
梦谈多话 2020-12-15 08:18

Quick question: how do i detect if a tap gesture recognizer is within a subview of the view it is added to? Eg. if i click on an object such as a square that has been added

相关标签:
3条回答
  • 2020-12-15 08:47

    you can use the requireGestureRecognizerToFail: to recognize the tap on subview please refer this code

    0 讨论(0)
  • 2020-12-15 08:52

    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];
    

    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;
    
    0 讨论(0)
  • 2020-12-15 08:53

    maybe you should set as: subviews.userInteractionEnabled = YES; good luck!

    0 讨论(0)
提交回复
热议问题