我添加了一个UIPanGestureRecognizer
这样的设置gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
if ([gestureRecognizer isEqual:self.panGesture] && [otherGestureRecognizer isEqual:self.collectionView.panGestureRecognizer]){
return YES;
}
return NO;
}
我还添加了签到gestureRecognizerShouldBegin:
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
if ([gestureRecognizer isEqual:self.panGesture]) {
if (gestureRecognizer.numberOfTouches > 0) {
CGPoint point = [gestureRecognizer locationOfTouch:0 inView:gestureRecognizer.view];
CGFloat distX = abs(self.collectionView.lastTouchPos.x - point.x);
CGFloat distY = abs(self.collectionView.lastTouchPos.y - point.y);
if (distX > distY) {
return NO;
}
} else {
return NO;
}
}
return YES;
}
仅self.panGesture
在向上或向下平移时使用。
更新
https://stackoverflow.com/a/8603839/667834似乎有更好的方法gestureRecognizerShouldBegin
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
if ([gestureRecognizer isEqual:self.panGesture]) {
if (gestureRecognizer.numberOfTouches > 0) {
CGPoint translation = [self.panGesture velocityInView:self.collectionView];
return fabs(translation.y) > fabs(translation.x);
} else {
return NO;
}
}
return YES;
}