简体   繁体   中英

Xamarin iOS - UIView Animation with velocity

I've got a UIView laying right above all others. This UIView has a UIPanGestureRecognizer to drag it around of the screen. If the User stopped panning, the View moves to a calculated Position which is the left screen bounds or the right ones. This works quite good but now I want to consider the velocity the View moved around the screen and when the user lifts his Finger off the screen, the UIView should not stop immediately to move to the endpoint, instead it should do it with a curve.

Edit 1

I think I expressed myself wrongly. I meant that the View should move the direction the user dragged it to but then it should make a curve and move to the desired position. Think of it like gravity. The user will drag the View with a speed from the left bottom corner to the middle of the screen and lifts immediately his finger. Then the View should not stop, else it should move in the last direction und slowly move to the desired location, which is in this case the right bottom corner.

This is the code I already have:

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);
    }
}

As you can see we already have the velocity but I don't know how to continue from here. Hopefully someone has the right starting point from here and could help me.

Thanks a lot!

I think UIKit Dynamics can meet your requirement.

refer to this Blog : https://blog.xamarin.com/drag-drop-and-snap-with-uikit-dynamics/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM