首先创建一个继承于UIScrollView的子类,然后在子类中重写以下方法即可:
//一句话总结就是此方法返回YES时,手势事件会一直往下传递,不论当前层次是否对该事件进行响应。
- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer {
if([self panBack:gestureRecognizer]) {
return YES;
}
return NO;
}
//location_X可自己定义,其代表的是滑动返回距左边的有效长度
- (BOOL)panBack:(UIGestureRecognizer*)gestureRecognizer {
//是滑动返回距左边的有效长度
intlocation_X =40;
if(gestureRecognizer ==self.panGestureRecognizer) {
UIPanGestureRecognizer*pan = (UIPanGestureRecognizer*)gestureRecognizer;
CGPointpoint = [pan translationInView:self];
UIGestureRecognizerStatestate = gestureRecognizer.state;
if(UIGestureRecognizerStateBegan== state ||UIGestureRecognizerStatePossible== state) {
CGPointlocation = [gestureRecognizer locationInView:self];
//这是允许每张图片都可实现滑动返回
inttemp1 = location.x;
inttemp2 =SCREEN_W;
NSIntegerXX = temp1 % temp2;
if(point.x>0&& XX < location_X) {
returnYES;
}
//下面的是只允许在第一张时滑动返回生效
// if (point.x > 0 && location.x < location_X && self.contentOffset.x <= 0) {
// return YES;
// }
}
}
return NO;
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer*)gestureRecognizer {
if([self panBack:gestureRecognizer]) {
return NO;
}
return YES;
}
网友评论