博客> 在collectionViewCell上添加手势UIPanGestureRecognizer,collectionView不可拖拽。
在collectionViewCell上添加手势UIPanGestureRecognizer,collectionView不可拖拽。
2018-04-22 17:56 评论:0 阅读:240 hellonian

现象:在collectionViewCell上添加手势UIPanGestureRecognizer,滑动时,手事件可触发,但是collectionView不可拖拽,只在cell之间的间隙位置滑动时可拖拽。 原因:两者冲突。 解决:设置代理《UIGestureRecognizerDelegate》 UIPanGestureRecognizer panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureAction:)]; panGesture.delegate = self; [self addGestureRecognizer:panGesture]; 实现代理方法, -- (BOOL)gestureRecognizer:(UIGestureRecognizer )gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { if ([otherGestureRecognizer.view isKindOfClass:[UICollectionView class]]) { return YES;//允许两者都存在 } return NO;//不允许两者都存在 }

进入api的解释: // note: returning YES is guaranteed to allow simultaneous recognition. returning NO is not guaranteed to prevent simultaneous recognition, as the other gesture's delegate may return YES //注意:返回YES保证允许同时识别。返回NO不能保证防止同时识别,因为另一个手势的委派可能返回YES。

收藏
0
sina weixin mail 回到顶部