1,根据手势变动状态判断
- (void)panHandle:(UIPanGestureRecognizer *)pan {
// 获取顶部视图
TABBaseCardView * cardView = self.cards[self.currentIndex];
if (pan.state == UIGestureRecognizerStateChanged) {
[self commitTranslation:[pan translationInView:cardView]];
}
}
- (void)commitTranslation:(CGPoint)translation
{
CGFloat absX = fabs(translation.x);
CGFloat absY = fabs(translation.y);
// 设置滑动有效距离
if (MAX(absX, absY) < 10)
return;
if (absX > absY ) {
if (translation.x<0) {
//向左滑动
self.isSliding = LEFT;
}else{
self.isSliding = RIGHT;
//向右滑动
}
} else if (absY > absX) {
if (translation.y<0) {
//向上滑动
self.isSliding = UP;
}else{
//向下滑动
self.isSliding = DOWN;
}
}
}
第二种手势有个代理方法 runturn no禁止滑动,yes可以滑动
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
CGPoint translation = [gestureRecognizer translationInView:gestureRecognizer.view];
//相对有手势父视图的坐标点(注意如果父视图是scrollView,locationPoint.x可能会大于视图的width)
if (translation.x < 0) {
//向左滑
NSLog(@"向左滑");
return YES;
}else if (translation.x > 0) {
//向右滑
NSLog(@"向右滑");
return YES;
}
if (translation.y < 0) {
//向上滑
NSLog(@"向上滑");
return NO;
}else if (translation.y > 0) {
//向下滑
NSLog(@"向下滑");
return NO;
}
return YES;
}