In this post, I have created an example of animation with triggers, This time, we are talking about triggers.
Xamarin.Forms have a powerful class named Trigger. I want to show you stack layout flipping with animation which I have created with Trigger.
First, let us create a folder named Triggers. Then, create a class with the name SwitchLayout.
Then, write the following code in that.
- public class SwitchLayout : TriggerAction<Button>
- {
- public enum AnimationDirection
- { Left, Right }
-
- protected override async void Invoke(Button sender)
- {
- View Viewhide = null;
- View Viewshow = null;
- if (TargetElement != null)
- {
-
- Viewshow = ((View)sender.Parent.Parent).FindByName<View>(TargetElement);
- if (Viewshow != null)
- {
-
- if (SourceElement != null)
- {
- Viewhide = ((View)sender.Parent.Parent).FindByName<View>(SourceElement);
- if (Viewhide != null)
- {
- await PerformAnimation(Viewhide, Viewshow);
- }
- }
- }
- }
- }
- public string SourceElement { get; set; }
- public string TargetElement { get; set; }
- public AnimationDirection Direction { get; set; }
-
- private async Task PerformAnimation(View ViewElementHide, View ViewElementShow)
- {
- int hideStart = 0;
- int hideStop = 0;
- int showStart = 0;
- int showStop = 0;
- await ViewElementHide.RotateYTo(hideStart, 0);
- await ViewElementShow.RotateYTo(showStart, 0);
- await ViewElementShow.ScaleTo(0.2, 0);
- ViewElementShow.IsVisible = true;
-
- ViewElementHide.FadeTo(0.5, 100, Easing.SinOut);
- ViewElementHide.ScaleTo(0.2, 100, Easing.Linear);
- await ViewElementHide.RotateYTo(hideStop, 150, Easing.Linear);
- ViewElementShow.FadeTo(1, 100, Easing.SinIn);
- ViewElementShow.ScaleTo(1, 150, Easing.Linear);
- await ViewElementShow.RotateYTo(showStop, 150, Easing.Linear);
-
- ViewElementHide.IsVisible = false;
- }
- }
Here is the XAML code.
You need to add the reference like this.
xmlns:triggers="clr-namespace:FlipAnimation.Triggers"
If you want to watch this demo video, click here.
For complete source code, click here