UIScrollView会覆盖我的子视图的平移手势识别器

时间:2013-09-23 10:43:05

标签: ios cocoa-touch uiscrollview uigesturerecognizer

如果我有一个带有子视图的scrollView并且子视图有一个平移手势识别器,则scrollView的平移手势会覆盖子视图的平移。我认为我想要的是相反的,所以我拖动它将在滚动视图中平移的子视图,但是如果我触摸另一个区域,滚动视图将正常平移。有没有简单的方法来设置它?

3 个答案:

答案 0 :(得分:12)

这对我有用:

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

// play nice with subview's pan gesture
[scrollView.panGestureRecognizer 
    requireGestureRecognizerToFail:subviewPanRecognizer];

答案 1 :(得分:1)

如果您不想在触摸子视图上滚动,请将canCancelContentTouches的{​​{1}}属性设置为UIScrollView

Original answer

答案 2 :(得分:0)

覆盖下面的这两个代表,

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

}

这将允许您识别两个手势,默认返回为NO,因此我们需要覆盖它并返回 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;

}

在此代表中,您可以按照自己的意愿执行任何操作,因为 gestureRecoginzer 的名称将被 otherGestureRecognizer 要求失败,您只需要做的就是判断这两种姿势是什么样的,然后返回YES或NO。