【问题标题】:How can a child ignore a UIGesture in certain circumstances and let the superview handle it?在某些情况下,孩子如何忽略 UIGesture 并让 superview 处理它?
【发布时间】:2014-08-18 18:27:38
【问题描述】:

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

在我的 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
    }
}

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

【问题讨论】:

  • 添加 UISwipeGestureRecognizer 代替平移手势怎么样?
  • @MohdIftekharQurashi 滑动手势在滑动时不提供反馈。那将是糟糕的用户界面。 See article.

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


【解决方案1】:

您需要让单元格的平移手势识别器与集合视图的平移手势识别器同时识别。

有几种方法可以做到这一点,但一种方法是让您的单元格成为平移手势识别器的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>;
}

【讨论】:

  • shouldRecognizeSimultaneouslyWithGestureRecognizer,我return ![otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]。这应该意味着任何其他平移手势都应该优先于我的单元格的平移手势。然而,实现这个功能没有任何效果——我的手指仍然平移了单元格而不是集合视图。我什至尝试返回一个硬编码的NO,结果是一样的。
  • 按照我的建议尝试返回一个硬编码的YES。那么结果如何呢?
  • 啊,我误解了这个功能。返回 YES 使父滚动和单元格平移。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多