I have 3 UIViews, layered on top of one large uiview. I want to know if the user touches the top one and not care about the other ones. I will have a couple of buttons in th
When your application receives a touch event, a hit-testing process is initiated to determine which view should receive the event. The process starts with the root of the view hierarchy, typically the application's window, and searches through the subviews in front to back order until it finds the frontmost view under the touch. That view becomes the hit-test view and receives the touch event. Each view involved in this process first tests if the event location is within its bounds. Only after the test is successful, does the view pass the event to the subviews for further hit-testing. So if your view is under the touch but located outside its parent view's bounds, the parent view will fail to test the event location and won't pass the touch event to your view.
One solution to this problem is to modify the layout of the view hierarchy so that your view is inside the bounds of its parent view. If for some reasons the existing layout has to be maintained, you can change the hit-testing behavior of the parent view so that it doesn't ignore the touch events. This can be done by overriding the -(UIView *)hitTest:withEvent: method of the parent view's class
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
// Convert the point to the target view's coordinate system.
// The target view isn't necessarily the immediate subview
CGPoint pointForTargetView = [self.targetView convertPoint:point fromView:self];
if (CGRectContainsPoint(self.targetView.bounds, pointForTargetView)) {
// The target view may have its view hierarchy,
// so call its hitTest method to return the right hit-test view
return [self.targetView hitTest:pointForTargetView withEvent:event];
}
return [super hitTest:point withEvent:event]; }
Other possible causes of this problem include:
The userInteractionEnabled property of your view, or any of its parent views, is set to NO. The application called its beginIgnoringInteractionEvents method without a matching call to its endIgnoringInteractionEvents method. You should make sure the userInteractionEnabled property is set to YES and the application does’t ignore the user events if you want the view to be interactive.
If your view doesn’t receive touch events during animation, it is because the animation methods of UIView typically disable touch events while animations are in progress. You can change that behavior by appropriately configuring the UIViewAnimationOptionAllowUserInteraction option when starting the UIView animation.
Another solution is
Add your own custom implementation in the view
class CustomView: YourParentView {
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
super.hitTest(point, with: event)
return overlapHitTest(point: point, withEvent: event)
}
}
extension UIView {
func overlapHitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
// 1
if !self.isUserInteractionEnabled || self.isHidden || self.alpha == 0 {
return nil
}
// 2
var hitView: UIView? = self
if !self.point(inside: point, with: event) {
if self.clipsToBounds {
return nil
} else {
hitView = nil
}
}
// 3
for subview in self.subviews.reversed() {
let insideSubview = self.convert(point, to: subview)
if let sview = subview.overlapHitTest(point: insideSubview, withEvent: event) {
return sview
}
}
return hitView
}
}
This can also be done the following way.
First find the location of the touch in the view you care about:
CGPoint location = [touches.anyObject locationInView:viewYouCareAbout];
Then see if the point is within the bounds of the view:
BOOL withinBounds = CGRectContainsPoint(viewYouCareAbout.bounds, location);
Then if it's within bounds, perform your action.
This can be done with one statement, which for example, can be used directly within an if statement:
CGRectContainsPoint(viewYouCareAbout.bounds, [touches.anyObject locationInView:viewYouCareAbout])