我正在使用 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 轮询视图的中心。