我正在实现类似于iOS内存管理器的东西。这是UICollectionView个UICollectionViewCell个孩子。如果您在单元格上水平滑动,则父级会向左平移。但是,如果垂直滑动,细胞会随手指移动。
在我的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
}
}
我已经知道如何用手指移动单元格,但我不知道如何处理另一种情况:让子单元格忽略平移手势并让其父手柄处理它。
答案 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>;
}