星空网 > 软件开发 > 操作系统

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.




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

关键词:ie

ie
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

网站内容营销-7款免费的话题研究工具推荐:https://www.ikjzd.com/articles/98149
为什么亚马逊广告转化率不高?:https://www.ikjzd.com/articles/98150
卖家注意!又有罢工将举行!:https://www.ikjzd.com/articles/98151
平板电脑等设备出口到美国注意事项:https://www.ikjzd.com/articles/98152
Lazada泰国直播:带货吸引观众超40万!:https://www.ikjzd.com/articles/98154
卖了3年的产品惨遭下架,有人建议翻新listing...:https://www.ikjzd.com/articles/98155
TikTok斥资210万美元游说美国参议院阻止法案通过 :https://www.goluckyvip.com/news/188220.html
北京飞机票查询(快速查询北京至各地机票价格和航班信息):https://www.vstour.cn/a/366178.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流