在某些情况下,孩子如何忽视UIGesture并让superview处理它?

时间:2014-08-18 18:27:39

标签: ios cocoa-touch event-handling uigesturerecognizer uipangesturerecognizer

我正在实现类似于iOS内存管理器的东西。这是UICollectionViewUICollectionViewCell个孩子。如果您在单元格上水平滑动,则父级会向左平移。但是,如果垂直滑动,细胞会随手指移动。

enter image description here

在我的UICollectionViewCell子类中,我有:

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {        
        UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(didPan:)];
        pan.maximumNumberOfTouches = 1;
        pan.minimumNumberOfTouches = 1;
        [self addGestureRecognizer:pan];
    }
    return self;
}

不幸的是,现在所有的子细胞都将处理平移手势。父母永远不会处理它们。

- (void)didPan:(UIPanGestureRecognizer *)gesture
{
    CGPoint velocity = [gesture velocityInView:self.superview];
    if (ABS(velocity.y) > ABS(velocity.x)) {
        // Move the cell with the finger
    } else {
        // Let the parent UICollectionView pan horizontally
    }
}

我已经知道如何用手指移动单元格,但我不知道如何处理另一种情况:让子单元格忽略平移手势并让其父手柄处理它。

1 个答案:

答案 0 :(得分:0)

您需要让细胞的平移手势识别器同时识别您的集合视图的平移手势识别器。

有几种方法可以做到这一点,但一种方法是将您的单元格设置为平移手势识别器的delegate并实现此方法:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    // Either just return YES to allow your cell's gesture recognizer
    // to work simultaneously with all other recognizers:
    return YES;

    // Or you can decide whether your cell's pan gesture recognizer should
    // recognize simultaneously with otherGestureRecognizer. For example,
    // you could get a reference to your collection view's panGestureRecognizer
    // and only return YES if otherGestureRecognizer is equal to that recognizer:
    return otherGestureRecognizer == <your collection view's gesture recognizer>;
}