Xamarin iOS - UIView Animation with velocity



我有一个UIView,它位于所有其他UIView之上。此UIView有一个UIPanGestureRecognizer,可以在屏幕上拖动它。如果用户停止平移,视图将移动到计算的位置,该位置是左侧屏幕边界或右侧屏幕边界。这工作得很好,但现在我想考虑视图在屏幕上移动的速度,当用户将手指抬离屏幕时,UIView 不应立即停止移动到端点,而是应该使用曲线。

编辑 1

我想我表达错了。我的意思是视图应该移动用户拖动它的方向,但随后它应该做一条曲线并移动到所需的位置。把它想象成重力。用户将以一定速度将视图从左下角拖动到屏幕中间,并立即抬起手指。然后视图不应该停止,否则它应该向最后一个方向移动并缓慢移动到所需的位置,在本例中为右下角。

这是我已有的代码:

private void DidPan()
{
CGPoint translation = PanGesture.TranslationInView(this.Superview);
CGPoint velocity = PanGesture.VelocityInView(this.Superview);
if (PanGesture.State == UIGestureRecognizerState.Changed)
{
this.Center = new CGPoint(lastLocation.X + translation.X, lastLocation.Y + translation.Y);
}
else if (PanGesture.State == UIGestureRecognizerState.Ended)
{
// Calculates the new position the view will be moving to
var newLocation = new CGPoint(SetX(Superview.Bounds), SetY(Superview.Bounds));
// Animate it to the desired location
UIView.Animate(0.3, () =>
{
this.Center = newLocation;
}, null);
}
}

如您所见,我们已经有了速度,但我不知道如何从这里继续。希望有人从这里开始有正确的起点,可以帮助我。

多谢!

我认为UIKit Dynamics可以满足您的要求。

请参阅此博客:https://blog.xamarin.com/drag-drop-and-snap-with-uikit-dynamics/

最新更新