[英]UITapGestureRecognizer does not respond to subview area outside parent view
I have a UIView called view1.我有一个名为 view1 的 UIView。 view1 has a subview called subview.
view1 有一个名为 subview 的子视图。 I added
UITapGestureRecognizer
to subview as follow:我将
UITapGestureRecognizer
添加到子视图中,如下所示:
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
[subview addGestureRecognizer:recognizer];
If I tapped an area overlapped between subview and view1 then the handleTap method got called.如果我点击了 subview 和 view1 之间重叠的区域,则调用了 handleTap 方法。 But if I tapped an area on the subview that was outside view1, then handleTap never got called.
但是,如果我在 view1 之外的子视图上点击了一个区域,那么 handleTap 永远不会被调用。 Is this behavior right?
这种行为对吗? If not, any suggestion to what should I check for?
如果没有,有什么建议我应该检查什么?
btw: The UIPanGestureRecognizer works fine.顺便说一句: UIPanGestureRecognizer 工作正常。 It does not exhibit the behavior mentioned above.
它不表现出上述行为。
That is the default behaviour of UIView
, the subview should be inside parent view bounds.这是
UIView
的默认行为,子视图应该在父视图范围内。 If you want something different is better you create a custom subclass of the top view and override (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
如果您想要不同的东西,最好创建顶视图的自定义子类并覆盖
(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
You need to customize the parent view and change the way it handles touches.您需要自定义父视图并更改它处理触摸的方式。 See this question for more details.
有关更多详细信息,请参阅此问题。
I've found the answers talking about overriding pointInside:withEvent: to be lacking detail on explanation or implementation.我发现关于覆盖 pointInside:withEvent: 的答案缺乏解释或实施的细节。 In the original question, when the user taps in the black, unlabeled area/view (we'll call it view2), the event framework will only trigger hitTest:withEvent: for the main window down through the view2 (and its immediate subviews), and will never hit it for view1 because the point tested for in pointInside:point is outside of the bounds of view1's frame.
在最初的问题中,当用户点击黑色的未标记区域/视图(我们将其称为 view2)时,事件框架只会触发 hitTest:withEvent: 主窗口向下穿过 view2(及其直接子视图) ,并且永远不会为 view1 命中它,因为在 pointInside:point 中测试的点超出了 view1 的框架范围。 In order to get subview1 to register the gesture, you should override view2's implementation of hitTest:withEvent to include a check for subview's pointInside:point
为了让 subview1 注册手势,您应该覆盖 view2 的 hitTest:withEvent 实现以包括对 subview 的 pointInside:point 的检查
//This presumes view2 has a reference to view1 (since they're nested in the example).
//In scenarios where you don't have access, you'd need to implement this
//in a higher level in the view hierachy
//In view2
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let ptRelativeToSubviewBounds = convert(point, to: view1.subview)
if view1.subview.point(inside:ptRelativeToSubviewBounds, with:event){
return view1.subview
}
else{
return super.hitTest(point, with: event)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.