如果我想跟踪移动,是否必须使用UIPanGestureRecognizer而不是UISwipeGestureRecognizer? [英] Do I have to use a UIPanGestureRecognizer instead of a UISwipeGestureRecognizer if I want to track the movement?

查看:115
本文介绍了如果我想跟踪移动,是否必须使用UIPanGestureRecognizer而不是UISwipeGestureRecognizer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的一个视图实现分页交互。我以为我只会使用 UISwipeGestureRecognizers 。但是通过试验和错误以及对文档的检查,似乎

I'm trying to implement a paging interaction for one of my views. I thought I would just use UISwipeGestureRecognizers. But through trial and error as well as examination of the documentation it appears that


滑动是一个离散的手势,因此相关的动作消息每个手势只发送一次

A swipe is a discrete gesture, and thus the associated action message is sent only once per gesture.

因此,虽然我可以触发页面,但我无法挂钩在拖动过程中发生的动画。

So while I could trigger the page, I wouldn't be able to hook up animation that occurred during the drag.

我唯一的选择是使用 UIPanGestureRecognizer 并重新实现基本过滤/滑动的计算?

Is my only alternative to use a UIPanGestureRecognizer and reimplement the basic filtering/calculations of the swipe?

事后看来,我真正应该问的是如何实现轻弹手势。如果您不打算使用自己的子类(可能会稍微关闭它),则使用UIPanGestureRecognizer,如@Lyndsey的答案所示。我之后(在评论中)寻找的是如何制作轻弹部分,其中轻弹的动量有助于决定是否进行轻弹动作或快速恢复原始演示。

In hindsight, what I really should have been asking is how to implement a "flick" gesture. If you're not going to roll your own subclass (may bite that off in a bit), you use a UIPanGestureRecognizer as @Lyndsey 's answer indicates. What I was looking for after that (in the comments) was how to do the flick part, where the momentum of the flick contributes to the decision of whether to carry the motion of the flick through or snap back to the original presentation.

UIScrollView有这样的行为,它很有可能挖掘它的实现,以获取有关如何以一致的方式减速动量的详细信息,但是唉 UIScrollView 提供的decelerationRate 是每次迭代值(根据一些)。我对如何正确地将默认值0.998应用到我的平底锅的最终速度感到震惊。

UIScrollView has behavior like that and it's tempting to mine its implementation for details on how one decelerates the momentum in a way that would be consistent, but alas the decelerationRate supplied for UIScrollView is "per iteration" value (according to some). I beat my head on how to properly apply the default value of 0.998 to the end velocity of my pan.

最后,我使用从网站上提取的关于轻弹的代码计算并在我的手势处理程序中做了类似的事情:

In the end, I used code pulled from sites about "flick" computation and did something like this in my gesture handler:

...
else if (pan.state == UIGestureRecognizerStateEnded) {
    CGFloat v = [pan velocityInView: self.view].x;
    CGFloat a = -4000.0; // 4 pixels/millisecond, recommended on gavedev
    CGFloat decelDisplacement = -(v * v) / (2 * a); // physics 101
    // how far have we come plus how far will momentum carry us?
    CGFloat totalDisplacement = ABS(translation) + decelDisplacement;
    // if that is (or will be) half way across our view, finish the transition
    if (totalDisplacement >= self.view.bounds.size.width / 2) {
        // how much time would we need to carry remainder across view with current velocity and existing displacement? (capped)
        CGFloat travelTime = MIN(0.4, (self.view.bounds.size.width - ABS(translation)) * 2 / ABS(v));
        [UIView animateWithDuration: travelTime delay: 0.0 options: UIViewAnimationOptionCurveEaseOut animations:^{
            // target/end animation positions
        } completion:^(BOOL finished) {
            if (finished) {
                // any final state change
            }
        }];
    }
    else { // put everything back the way it was
        ...
    }
}


推荐答案

是的,如果你想要特定的速度,请使用 UIPanGestureRecognizer ,滑动的角度,变化等,以触发您的动画。 UISwipeGestureRecognizer 确实是单个离散手势;类似于 UITapGestureRecognizer ,它会在识别时触发单个操作消息。

Yes, use a UIPanGestureRecognizer if you want the specific speed, angle, changes, etc. of the "swipe" to trigger your animations. A UISwipeGestureRecognizer is indeed a single discrete gesture; similar to a UITapGestureRecognizer, it triggers a single action message upon recognition.

在物理学中, UIPanGestureRecognizer 的速度将指示平移手势的速度和方向。以下是 的文档> velocityInView:方法,它将帮助您以每秒点数计算更改平移手势的水平和垂直分量。

As in physics, the UIPanGestureRecognizer's "velocity" will indicate both the speed and direction of the pan gesture. Here are the docs for velocityInView: method which will help you calculate the horizontal and vertical components of the changing pan gesture in points per second.

这篇关于如果我想跟踪移动,是否必须使用UIPanGestureRecognizer而不是UISwipeGestureRecognizer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆