2016-10-18 64 views
0

我有一個tableViewpanGesture
我可以從屏幕的底部垂直移動此tableView
但是,我希望panGesture以一定的速度繼續,以在向上/向下平移時獲得scrollView類效果。分配適當的速度平移手勢iOS?

會發生什麼情況是tableView很快向上或向下的方向移動,沒有停止的跡象。

這是我迄今所做的:

-(void)handlePanWithVelocity:(UIPanGestureRecognizer *)gestureRecognizer{ 

    CGPoint displacement; 
    CGPoint velocity = [gestureRecognizer velocityInView:self.mapView]; 
    switch (gestureRecognizer.state) { 
     case UIGestureRecognizerStateBegan:{ 
      break; 
     }; 
     case UIGestureRecognizerStateChanged:{ 
      CGPoint translation = [gestureRecognizer translationInView:self.view]; 
      displacement = (isVerticalPan) ? CGPointMake(0, translation.y) : CGPointMake(translation.x, 0); 
      mytableView.transform = CGAffineTransformMakeTranslation(displacement.x, displacement.y); 
      break; 
     }; 
     case UIGestureRecognizerStateEnded: { 
      displacement.y = displacement.y + velocity.y; 
      [UIView animateWithDuration:1 animations:^{ 
       mytableView.transform = CGAffineTransformMakeTranslation(displacement.x, displacement.y); 
      }]; 
      break; 
     }; 
     case UIGestureRecognizerStatePossible:break; 
     case UIGestureRecognizerStateCancelled:break; 
     case UIGestureRecognizerStateFailed:break; 
    } 

} 

可以做些什麼來改善當前的行爲?

回答

0

事情是,你不測量它首先要移動的時間。由於速度它x/t你需要調整你UIView動畫的持續時間。您必須測量平移識別器回調之間的時間,然後計算速度。

全局變量:

double velocity; 
NSDate *date; 

然後:

-(void)handlePanWithVelocity:(UIPanGestureRecognizer *)gestureRecognizer{ 

    CGPoint displacement; 
    CGPoint velocity = [gestureRecognizer velocityInView:self.mapView]; 
    switch (gestureRecognizer.state) { 
     case UIGestureRecognizerStateBegan:{ 
      break; 
     }; 
     case UIGestureRecognizerStateChanged:{ 


      CGPoint translation = [gestureRecognizer translationInView:self.view]; 
      displacement = (isVerticalPan) ? CGPointMake(0, translation.y) : CGPointMake(translation.x, 0); 
      mytableView.transform = CGAffineTransformMakeTranslation(displacement.x, displacement.y); 

      if(date){ 
       double timePassed_ms = [date timeIntervalSinceNow] * -1000.0; 
       velocity = displacement.y/timePassed_ms; 
      } 


      date = [NSDate date]; 
      break; 
     }; 
     case UIGestureRecognizerStateEnded: { 
      displacement.y = displacement.y + velocity.y; 
      [UIView animateWithDuration:1 animations:^{ 
       mytableView.transform = CGAffineTransformMakeTranslation(0, velocity); 
      }]; 
      break; 
     }; 
     case UIGestureRecognizerStatePossible:break; 
     case UIGestureRecognizerStateCancelled:break; 
     case UIGestureRecognizerStateFailed:break; 
    } 

} 

我沒有測試代碼,但你得到的總體思路。