简体   繁体   English

UITapGestureRecognizer 不响应父视图外的子视图区域

[英]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.

相关问题 UITapGestureRecognizer在触摸时不响应 - UITapGestureRecognizer does not respond on touch UIView上的UITapGestureRecognizer及其子视图在点击子视图时一起响应 - UITapGestureRecognizer on UIView and Its Subview Respond Together When Subview Is Tapped 如何防止子视图上的拍子触发父视图上的UITapGestureRecognizer? - How do I prevent taps on a subview from triggering a UITapGestureRecognizer on a parent view? 子视图在父视图中接收其框架外的触摸 - Subview receiving touches outside its frame in parent view 子视图之外的UIButton可点击区域 - UIButton tapable area outside of subview 将UITapGestureRecognizer移到外部视图后再移回 - UITapGestureRecognizer is ignored after moving outside view and back in 子视图在将其拖动到滚动视图(父视图)之外时隐藏,并在拖放到另一个视图后出现。 - Subview hidden on dragging it outside the scroll view(which is the parent view) and appeared after being dropped into another view. 快速子视图不会完全填充父视图 - swift subview does not full-fill parent view UITapGestureRecognizer仅检测父视图点击 - UITapGestureRecognizer only detects parent view tap UITapGestureRecognizer不适用于UILabel,但适用于父视图 - UITapGestureRecognizer not working on UILabel but working on parent view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM