当UIScrollView有多页的时候我们知道会出现滑动返回失效的情况,同理横置UICollectionView也是同一个道理,因为UICollectionView本身就是继承与UIScrollView,所以他们出现这种情况的原因是一样的,具体造成该问题总结一句话简单来说就是 手势拦截的问题(具体原理可以看我上一篇文章
解决右滑返回手势和UIScrollView中的手势冲突
),下面我直接贴出解决办法
首先创建UIScrollView的类别category,然后在此类别中重写以下方法即可:
#import "UIScrollView+LGScroll.h"
@implementation UIScrollView (LGScroll)
//一句话总结就是此方法返回YES时,手势事件会一直往下传递,不论当前层次是否对该事件进行响应。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
if ([selfpanBack:gestureRecognizer]) {
returnYES;
}
returnNO;
}
//location_X可自己定义,其代表的是滑动返回距左边的有效长度
- (BOOL)panBack:(UIGestureRecognizer *)gestureRecognizer {
//是滑动返回距左边的有效长度
int location_X =40;
if (gestureRecognizer ==self.panGestureRecognizer) {
UIPanGestureRecognizer *pan = (UIPanGestureRecognizer *)gestureRecognizer;
CGPoint point = [pantranslationInView:self];
UIGestureRecognizerState state = gestureRecognizer.state;
if (UIGestureRecognizerStateBegan == state ||UIGestureRecognizerStatePossible == state) {
CGPoint location = [gestureRecognizerlocationInView:self];
//这是允许每张图片都可实现滑动返回
int temp1 = location.x;
int temp2 =SCREEN_W;
NSInteger XX = temp1 % temp2;
if (point.x >0 && XX < location_X) {
returnYES;
}
//下面的是只允许在第一张时滑动返回生效
// if (point.x > 0 && location.x < location_X && self.contentOffset.x <= 0) {
// return YES;
// }
}
}
returnNO;
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
if ([selfpanBack:gestureRecognizer]) {
returnNO;
}
returnYES;
}
@end
以上仅代表个人观点,有什么不足之处还请大家多多指教