分配适当的速度来平移手势iOS?

时间:2016-10-18 15:06:28

标签: ios objective-c uigesturerecognizer

我有tableView panGesture。我可以从屏幕底部垂直平移tableView
但是,我希望panGesture以一定的速度继续,以便在向上/向下平移时获得scrollView类型的效果。

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; } } ,没有停止的迹象。

这是我到目前为止所做的:

ElseIf ActiveCell.value mod 1 >= 0 and ActiveCell.Value mod 1 <= 1/24 And ActiveCell.Row > Leq730Row + 600 then

可以采取哪些措施来改善目前的行为?

1 个答案:

答案 0 :(得分:0)

问题是你不能测量首先移动所需的时间。由于速度为x / t,您需要调整UIView动画的持续时间。您必须测量Pan Recognizer回调之间的时间,然后计算速度。

全局变量:

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;
    }

}

我没有测试代码,但你得到了一般的想法。