Skip to content

Instantly share code, notes, and snippets.

@taimila
Created February 12, 2019 07:15
Show Gist options
  • Save taimila/1b4161dcbca43df6032ae34aa2576573 to your computer and use it in GitHub Desktop.
Save taimila/1b4161dcbca43df6032ae34aa2576573 to your computer and use it in GitHub Desktop.
Xamarin.Forms view's width and height animation
public static class ExtraAnimations
{
public static async Task<bool> HeightTo(this View view, double height, uint duration = 250, Easing easing = null)
{
var tcs = new TaskCompletionSource<bool>();
var heightAnimation = new Animation(x => view.HeightRequest = x, view.Height, height);
heightAnimation.Commit(view, "HeightAnimation", 10, duration, easing, (finalValue, finished) => { tcs.SetResult(finished); });
return await tcs.Task;
}
public static async Task<bool> WidthTo(this View view, double width, uint duration = 250, Easing easing = null)
{
var tcs = new TaskCompletionSource<bool>();
var heightAnimation = new Animation(x => view.WidthRequest = x, view.Height, width);
heightAnimation.Commit(view, "WidthAnimation", 10, duration, easing, (finalValue, finished) => { tcs.SetResult(finished); });
return await tcs.Task;
}
}
@taimila
Copy link
Author

taimila commented Feb 12, 2019

Simple extenstion methods for animating a view's height and width in Xamarin.Forms. Extends view to provide HeightTo()and WidthTo() methods in order to provide similar API that is already available for many other properties like ScaleTo() and FadeTo().

@shrikantbmali
Copy link

awesome :)

@mecvillarina
Copy link

Great.

@sockstar85
Copy link

sockstar85 commented Apr 27, 2023

Works great! Might want to change the starting value in WidthTo() from view.Height to view.Width though.

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