translationInView:(获取当前拖拽位置)translationInView 返回父视图系统当中,返回横坐标,纵坐标上拖动了多少像素
可以判断拖动方向
1 2 3 CGPoint translantion = [UIPanGestureRecognizer translationInView:UIView ];ABS(translantion.x)/ABS(translantion.y) > 1
velocityInView:(设置拖拽速度,单位:像素/秒)velocityInView 返回指定坐标系统当中拖动的速度,x,y分别代表x轴y轴的拖动速度(矢量)
1 2 CGPoint velocity = [UIPanGestureRecognizer velocityInView:UIView ];velocity.x > 0
实例应用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 typedef NS_ENUM (NSInteger , PanDirection){ PanDirectionHorizontalMoved, PanDirectionVerticalMoved }; static PanDirection panDirection;- (void )panGestureOnMainVC2:(UIPanGestureRecognizer *)sender{ if (sender.numberOfTouches == 0 ) { NSLog (@"手指离开屏幕" ); } CGPoint veloctyPoint = [sender velocityInView:self .view]; CGPoint transPoint = [sender translationInView:self .view]; switch (sender.state) { case UIGestureRecognizerStateBegan :{ NSLog (@"x:%f y:%f aaa:%f,bbb:%f" ,veloctyPoint.x, veloctyPoint.y,transPoint.x,transPoint.y); CGFloat x = fabs(veloctyPoint.x); CGFloat y = fabs(veloctyPoint.y); if (x > y) { panDirection = PanDirectionHorizontalMoved; }else if (x < y){ panDirection = PanDirectionVerticalMoved; } } break ; case UIGestureRecognizerStateChanged :{ switch (panDirection) { case PanDirectionHorizontalMoved:{ [self horizontalMoved:veloctyPoint.x]; break ; } case PanDirectionVerticalMoved:{ [self verticalMoved:veloctyPoint.y]; break ; } default : break ; } } break ; case UIGestureRecognizerStateEnded :{ NSLog (@"UIGestureRecognizerStateEnded" ); switch (panDirection) { case PanDirectionHorizontalMoved:{ } break ; case PanDirectionVerticalMoved:{ } break ; default : break ; } } break ; default : break ; } }
设置最小滑动距离,相比用屏幕宽度*0.5为或滑动方向为标准判断是否清屏体验要好。
1 2 3 4 5 6 7 8 9 10 11 12 UIView *bottomView;- (void )viewDidLoad { [super viewDidLoad]; self .view.backgroundColor=[UIColor whiteColor]; UIView *panview =[[UIView alloc]initWithFrame:self .view.bounds]; panview.backgroundColor=[UIColor orangeColor]; [self .view addSubview:panview]; bottomView=panview; [self .view addGestureRecognizer:[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector (panGestureOnMainVC:)]]; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 #pragma mark---滑动清屏幕功能 static CGFloat const MIN_SPACE_VALUE= 50.0 ;-(void )panGestureOnMainVC:(UIPanGestureRecognizer *)sender { CGPoint translation = [sender translationInView:self .view]; if (sender.state == UIGestureRecognizerStateBegan ) { } UIView *panView= bottomView; panView.transform = CGAffineTransformTranslate (panView.transform, translation.x, 0 ); [sender setTranslation:CGPointZero inView:self .view]; CGAffineTransform rightScopeTransform=CGAffineTransformMakeTranslation (self .view.frame.size.width, 0 ); if (panView.transform.tx > rightScopeTransform.tx) { panView.transform = rightScopeTransform; }else if (panView.transform.tx < 0.0 ){ panView.transform=CGAffineTransformMakeTranslation (0 , 0 ); } CGPoint veloctyPoint = [sender velocityInView:self .view]; if (sender.state == UIGestureRecognizerStateEnded ) { [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{ if (veloctyPoint.x>0 ) { if (panView.transform.tx<MIN_SPACE_VALUE) { panView.transform = CGAffineTransformIdentity ; }else { panView.transform=rightScopeTransform; } }else { if (panView.transform.tx>(self .view.frame.size.width-MIN_SPACE_VALUE)) { panView.transform=rightScopeTransform; }else { panView.transform = CGAffineTransformIdentity ; } } } completion:^(BOOL finished) { }]; } }