UIScrollView默认手势有:PagingSwipeGestureRecognizer、DelayedTouchesBeganGestureRecognizer、PanGestureRecognizer;UITableview只有后两个手势;
一般在含有UIScrollView或者UIScrollView的子类的视图上添加自己的手势就会有冲突;利用UIGestureRecognizerDelegate中的代理方法可以解决相应的冲突;
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
该代理方法可以拦截相应的手势,返回YES为拦截手势;返回NO为不做处理;
相应的手势被拦截之后,就会执行另一个代理方法:
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
例如:
场景:实现一个父子控制器,然后再划到最两边的时候,实现抽屉效果;
实现:在主控制器的view上添加pan 手势,不做任何处理时,系统默认执行UIScrollView的手势;在第一个代理方法中判断UIScrollView的偏移量,然后拦截手势;然后在第二个代理方法对其进行相应的处理;
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
CGFloat offsetX = self.scrollView.contentOffset.x;
//满足条件拦截手势
if (offsetX == self.scrollView.contentSize.width - screenSize.width || offsetX == 0.0) {
return YES;
}
}
return NO;
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
UIPanGestureRecognizer *pan = (UIPanGestureRecognizer *)gestureRecognizer;
CGPoint translation = [pan translationInView:self.view];
CGFloat offsetX = self.scrollView.contentOffset.x;
if (translation.x > 0 && offsetX == 0.0 ) {
if ([self.delegate respondsToSelector:@selector(centerControllerPrepareLeftView:)]) {
[self.delegate centerControllerPrepareLeftView:self];
}
}else if (translation.x < 0 && offsetX == self.scrollView.contentSize.width - screenSize.width){
if ([self.delegate respondsToSelector:@selector(centerControllerPrepareRightView:)]) {
[self.delegate centerControllerPrepareRightView:self];
}
}
}
return YES;
}