将velocityInView与UIPanGestureRecognizer结合使用

发布于 2025-01-01 11:39:13 字数 1908 浏览 4 评论 0原文

我有一个自定义滑块类型的对象,我希望使其更有用。目前我使用 UIPanGestureRecognizertranslationInView 来使其工作。它工作得很好,但我想要某种速度,让它感觉更有用。我已经尝试了一些方法,但无法完全弄清楚如何正确实现速度 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...            
        }

    }
}

任何帮助将不胜感激。谢谢。

I have a custom slider-type object, that I wish to make more usable. Currently I use UIPanGestureRecognizer and translationInView to make it work. 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.

- (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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哥,最终变帅啦 2025-01-08 11:39:13

根据我的经验,在用户抬起手指并且识别器完成之前,平移手势识别器的 velocityInView: 并不重要。此时,您可以使用速度来计算移动视图的动画持续时间。

只需坚持使用 translationInView: 直到 stateUIGestureRecognizerStateEnded,然后使用 velocityInView: 为任何屏幕视图更改添加动画效果。

In my experience, the velocityInView: of a pan gesture recognizer isn't important until the user lifts their finger(s) and the recognizer finishes. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文