Skip to content

Instantly share code, notes, and snippets.

@jorwan
Created October 17, 2016 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jorwan/1ed57459c7b01b5a5b1135219e6219cf to your computer and use it in GitHub Desktop.
Save jorwan/1ed57459c7b01b5a5b1135219e6219cf to your computer and use it in GitHub Desktop.
public static class UtilAnimation
{
/// <summary>
/// Shakes it.
/// </summary>
/// <param name="view">View.</param>
/// <param name="Duration">Duration.</param>
/// <param name="RepeatCount">Repeat count.</param>
/// <param name="MovementDistance">Movement distance.</param>
/// <param name="AnimationKey">Animation key.</param>
public static void ShakeIt(this UIView view, float Duration = 0.05f, int RepeatCount = 8, float MovementDistance = 20f, string AnimationKey = "position")
{
CABasicAnimation animation = new CABasicAnimation();
animation.Duration = Duration;
animation.RepeatCount = RepeatCount;
animation.AutoReverses = true;
animation.SetFrom(NSValue.FromCGPoint(new CGPoint(view.Center.X-MovementDistance, view.Center.Y-MovementDistance)));
animation.SetTo(NSValue.FromCGPoint(new CGPoint(view.Center.X+MovementDistance, view.Center.Y+MovementDistance)));
view.Layer.AddAnimation(animation, AnimationKey);
}
}
@jorwan
Copy link
Author

jorwan commented Oct 17, 2016

Example of Use:

InvokeOnMainThread(() => {
    view.ShakeIt(MovementDistance: 2f, RepeatCount: 4);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment