Delivering touch events to a view outside the bounds of its parent view(来自官方文档)

Q: My view is displayed correctly on the screen, but does not receive any touch events. Why is that?

A: The most common cause of this problem is because your view is located outside the bounds of its parent view. 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, as shown in Listing 1:

Listing 1: Overriding the hit-testing 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];

}

For more information about hit-testing, please refer to the discussion on Event Delivery in Event Handling Guide for iOS.

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.

你可能感兴趣的:(event)