是否有可能知道[UIDynamicItemBehavior addLinearVelocity:forItem:]何时完成运行?

时间:2013-10-03 20:58:52

标签: ios ios7 uikit-dynamics

我正在使用UIPanGestureRecognizer和UIAttachmentBehavior在屏幕上移动UIView。当用户结束手势时,我使用UIDynamicItemBehavior和addLinearVelocity:forItem:方法将手势识别器的速度应用于视图。

以下是我使用的代码:

- (void)_handlePanGestureRecognized: (UIPanGestureRecognizer *)panGestureRecognizer
{
    if (panGestureRecognizer.state == UIGestureRecognizerStateBegan)
    {
        _attachmentBehavior.anchorPoint = panGestureRecognizer.view.center;
        [_dynamicAnimator addBehavior: _attachmentBehavior];
    }
    else if (panGestureRecognizer.state == UIGestureRecognizerStateChanged)
    {
        CGPoint point = [panGestureRecognizer locationInView: panGestureRecognizer.view.superview];
        _attachmentBehavior.anchorPoint = point;
    }
    else if (panGestureRecognizer.state == UIGestureRecognizerStateEnded)
    {
        [_dynamicAnimator removeBehavior: _attachmentBehavior];

        CGPoint velocity = [panGestureRecognizer velocityInView: panGestureRecognizer.view.superview];
        [_dynamicItemBehavior addLinearVelocity: velocity 
            forItem: self];
    }
}

当视图停止移动时,我想让它捕捉到屏幕的最近边缘,但我目前无法知道它何时停止使用CADisplayLink轮询视图的中心。

1 个答案:

答案 0 :(得分:1)

您是否尝试将UIDynamicAnimatorDelegate附加到动画师,并使用dynamicAnimatorDidPause:方法触发捕捉到最近边缘?

从开发者论坛上的阅读中,听起来有些人的观点在很长一段时间内一直存在问题(例如来回摇晃1个像素),但这可能对你的情况有用。