简体   繁体   English

在UIPanGestureRecognizer中使用velocityInView

[英]Making use of velocityInView with UIPanGestureRecognizer

I have a custom slider-type object, that I wish to make more usable. 我有一个自定义滑块类型的对象,我希望它更有用。 Currently I use UIPanGestureRecognizer and translationInView to make it work. 目前我使用UIPanGestureRecognizertranslationInView来使其工作。 It works pretty well but I'd like some sort of velocity in there to make it feel a bit more useful. 它工作得很好,但我想在那里使用某种速度让它感觉更有用。 I've tried a few things but cant quite figure out how to properly implement velocity changedLevel equation. 我已经尝试了一些东西,但不能弄清楚如何正确实现速度changedLevel方程。

- (void)panDetected:(UIPanGestureRecognizer *)gesture {

    CGPoint swipeLocation = [gesture locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:swipeLocation];
    LevelCounterTableCell *swipedCell = (LevelCounterTableCell *)[self.tableView cellForRowAtIndexPath:indexPath];

    if([gesture state] == UIGestureRecognizerStateBegan) {
        NSString *originalLevelString = swipedCell.levelNumber.text;
        originalLevel = [originalLevelString intValue]; // int originalLevel
    }

    if ([gesture state] == UIGestureRecognizerStateChanged) {

        CGFloat xTranslation = [gesture translationInView:[[gesture view] superview]].x;
        CGFloat xVelocity = [gesture velocityInView: [[gesture view] superview]].x;

        // Pan threshold is currently set to 8.0. 
        // 8.0 is a decent level for slow panning
        // for fast panning 2.0 is more reasonable
        changedLevel = ceilf((xTranslation / panThreshold) + originalLevel); // int changedLevel

        // Raw velocity seems to go from around 3 (slow)
        // to over 200 (fast)
        NSLog(@"raw velocity = %f", xVelocity);

        if (changedLevel >= 15 && changedLevel <= 100) {
            swipedCell.levelNumber.text = [NSString stringWithFormat:@"%i", changedLevel];
            swipedCell.meter.frame = [self updateMeter: changedLevel];

        }
    }

    if ([gesture state] == UIGestureRecognizerStateEnded || [gesture state] == UIGestureRecognizerStateCancelled) {
        if (changedLevel >= 15 && changedLevel <= 100) {
            //... Save the values...            
        }

    }
}

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thank you. 谢谢。

In my experience, the velocityInView: of a pan gesture recognizer isn't important until the user lifts their finger(s) and the recognizer finishes. 根据我的经验,平移手势识别器的velocityInView:在用户抬起手指并且识别器完成之前并不重要。 At that point, you can use the velocity to calculate an animation duration to move your views. 此时,您可以使用力度来计算动画持续时间以移动视图。

Just stick with translationInView: until the state is UIGestureRecognizerStateEnded and then use velocityInView: to animate any onscreen view changes. 只需坚持使用translationInView:直到stateUIGestureRecognizerStateEnded ,然后使用velocityInView:为任何屏幕上的视图更改设置动画。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM