UIScrollView overrides my subview's pan gestur

2019-04-08 12:34发布

If I have a scrollView with a subview and the subview has a pan gesture recognizer, the scrollView's pan gesture override's the subview's pan. What I want is the opposite, I think, so that is I drag a subview it will pan within the scroll view, yet if I touch another area the scroll view will pan as normal. Is there an easy way to set that up?

3条回答
别忘想泡老子
2楼-- · 2019-04-08 13:04

Overwrite these two delegate below,

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;

}

This will allow you to recognize both gestures, the default return is NO, so we need to overwrite it and return YES.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
    if ([otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
        return NO;
    }else{
        return YES;
    }
}
return YES;

}

In this delegate you can do anything as you wish, as it's name the gestureRecoginzer will be required to fail by the otherGestureRecognizer, all you need to do is to judge what kind of these two gestures and return YES or NO.

查看更多
淡お忘
3楼-- · 2019-04-08 13:08

Set canCancelContentTouches property of UIScrollView to false if you don't want to scroll on touching subviews.

Original answer

查看更多
我只想做你的唯一
4楼-- · 2019-04-08 13:12

Here's what works for me:

UIPanGestureRecognizer *subviewPanRecognizer = [[UIPanGestureRecognizer alloc]
    initWithTarget:self action:@selector(panSubview:)];
[subview addGestureRecognizer:subviewPanRecognizer];

// play nice with subview's pan gesture
[scrollView.panGestureRecognizer 
    requireGestureRecognizerToFail:subviewPanRecognizer];
查看更多
登录 后发表回答