Delivering touch events to a view outside the bounds of its parent view(來自官方文檔),

來源:互聯網
上載者:User

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.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.