UITapGestureRecognizer not triggered in deep view hiearchy

浪子不回头ぞ 提交于 2019-12-23 04:37:29

问题


I have placed a UIGestureRecognizer deep within my uiview hierarchy but it is not being trigger. Here is a general map of the views:

UIScrollView > UIView > UIView > UIView > UIView

The last view has the gesture recognizer:

- (id)initWithFrame:(CGRect)frame {    
    self = [super initWithFrame:frame];

    if (self) {
        self.userInteractionEnabled = TRUE;

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
        [self addGestureRecognizer:tap];
        [tap release];
    }  

    return self;
}

- (void)tap:(UIGestureRecognizer *)recognizer {
    NSLog(@"tap");
}

I am setting the canCancelContentTouches of the scrollview to allow gestures to propagate through.

When I moved the view with the gesture to directly underneath the scrollview it works. Can someone explain why it does not work in a deep hierarchy?

Thanks!


回答1:


In such a hierarchy the point you touch must be in within the views frame and all parent frames. The responder chain starts at the scrollview, and if the touch is outside of the scrollview it stops there. Then it checks the direct children of the scrollview and so on.

If that's not the case i suggest writing your own hittest function to check if the last frame is hit and return it then.



来源:https://stackoverflow.com/questions/6871946/uitapgesturerecognizer-not-triggered-in-deep-view-hiearchy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!