Skip to content

Instantly share code, notes, and snippets.

@lobrien
Created May 30, 2014 20:38
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lobrien/7d4bc2e3558fe66eceea to your computer and use it in GitHub Desktop.
Save lobrien/7d4bc2e3558fe66eceea to your computer and use it in GitHub Desktop.
Xamarin.Forms Easing
using System;
using Xamarin.Forms;
namespace XamFormsEasing
{
public class App
{
public static Page GetMainPage ()
{
var b = new Button {
Text = "Click me",
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.CenterAndExpand,
};
var v = new BoxView {
BackgroundColor = Color.Green
};
var sl = new AbsoluteLayout {
Children = { b, v }
};
AbsoluteLayout.SetLayoutBounds(sl.Children[0],
new Rectangle(20, 20,
AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize));
AbsoluteLayout.SetLayoutBounds(sl.Children[1],
new Rectangle(20, 80,
AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize));
int iClicks = 0;
var eAndN = new Tuple<Easing,string>[] {
new Tuple<Easing, string> (Easing.BounceIn, "BounceIn"),
new Tuple<Easing, string> (Easing.BounceOut, "BounceOut"),
new Tuple<Easing, string> (Easing.CubicIn, "CubicInOut"),
new Tuple<Easing, string> (Easing.CubicOut, "CubicOut"),
new Tuple<Easing, string> (Easing.Linear, "Linear"),
new Tuple<Easing, string> (Easing.SinIn, "SinIn"),
new Tuple<Easing, string> (Easing.SinInOut, "SinInOut"),
new Tuple<Easing, string> (Easing.SinOut, "SinOut"),
new Tuple<Easing, string> (Easing.SpringIn, "SpringIn"),
new Tuple<Easing, string> (Easing.SpringOut, "SpringOut"),
new Tuple<Easing, string> (new Easing(Math.Sin), "Custom")
};
b.Clicked += (object sender, EventArgs e) => {
var newPos = new Rectangle(240, 80, 20, 20);
var eAndName = eAndN[iClicks];
var easing = eAndName.Item1;
b.Text = eAndName.Item2;
v.LayoutTo ( newPos, 2500, easing);
iClicks++;
iClicks %= eAndN.Length;
};
var cp = new ContentPage {
Content = sl
};
return cp;
}
}
}
@LuisAlbertoPenaNunez
Copy link

What about Custom one?

@scotch83
Copy link

scotch83 commented Oct 6, 2015

I have few problems that I could not succeed to solve.
The first one was to render the BoxView:
I coul not succeed to see it, even using other layouts.
If I use another button and animate that, then it works.
But then I have another problem:
it works only the first time, then not anymore.
Any idea?

@scotch83
Copy link

scotch83 commented Oct 6, 2015

Oké, I have found the problem, at least for the animation executing only once. Obviously, it was in my code :)
I was using the RotateTo method, which is then setting the property of the object to the given angle, so when we call this again, the angle is already set and there is no movement.

The best solution in this case is than:

myFancyView.RotateTo ( myFancyView.Rotation + angle, duration, easing );

which will then use the actual angle of the view.

By the way I still cannot see the BoxView...

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