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 UILabel
s have this set to NO
by default. So prior to the above:
self.subviewOfInterest.userInteractionEnabled = YES;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…