8

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

4

3 回答 3

15

这对我有用:

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

// play nice with subview's pan gesture
[scrollView.panGestureRecognizer 
    requireGestureRecognizerToFail:subviewPanRecognizer];
于 2013-09-25T10:14:09.173 回答
1

如果您不想在触摸子视图上滚动,请将其canCancelContentTouches属性设置UIScrollView为。false

原始答案

于 2018-10-17T11:55:34.787 回答
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。

于 2015-04-08T13:59:44.917 回答