Delivering touch events to a view outside the bounds of its parent view (from the official documentation ),

Source: Internet
Author: User

Delivering touch events to a view outside the bounds of its parent view (from the official documentation ),
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 has es a touch event, a hit-testing process is initiated to determine which view shold 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 events es 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 -(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:

  • TheuserInteractionEnabledProperty of your view, or any of its parent views, is set to NO.

  • The application called itsbeginIgnoringInteractionEventsMethod without a matching call to itsendIgnoringInteractionEventsMethod.

You shoshould make sureuserInteractionEnabledProperty 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 methodsUIViewTypically disable touch events while animations are in progress. You can change that behavior by appropriately refreshingUIViewAnimationOptionAllowUserInteractionOption when startingUIViewAnimation.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.