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.

  1. public class SwitchLayout : TriggerAction<Button>
  2. {
  3. public enum AnimationDirection
  4. { Left, Right }
  5. protected override async void Invoke(Button sender)
  6. {
  7. View Viewhide = null;
  8. View Viewshow = null;
  9. if (TargetElement != null)
  10. {
  11. // This is For Finding TargetView
  12. Viewshow = ((View)sender.Parent.Parent).FindByName<View>(TargetElement);
  13. if (Viewshow != null)
  14. {
  15. // This is For Finding SourceView
  16. if (SourceElement != null)
  17. {
  18. Viewhide = ((View)sender.Parent.Parent).FindByName<View>(SourceElement);
  19. if (Viewhide != null)
  20. {
  21. await PerformAnimation(Viewhide, Viewshow);
  22. }
  23. }
  24. }
  25. }
  26. }
  27. public string SourceElement { get; set; }
  28. public string TargetElement { get; set; }
  29. public AnimationDirection Direction { get; set; }
  30. private async Task PerformAnimation(View ViewElementHide, View ViewElementShow)
  31. {
  32. int hideStart = 0;
  33. int hideStop = 0;//if you want to use another rotate animation So you Comment Out and remove 0//(Direction == AnimationDirection.Left ? -90 : 90);
  34. int showStart = 0;//if you want to use another rotate animation So you Comment Out and remove 0//(Direction == AnimationDirection.Left ? 90 : 270);
  35. int showStop = 0;//if you want to use another rotate animation So you Comment Out and remove 0//(Direction == AnimationDirection.Left ? 0 : 360);
  36. await ViewElementHide.RotateYTo(hideStart, 0);
  37. await ViewElementShow.RotateYTo(showStart, 0);
  38. await ViewElementShow.ScaleTo(0.2, 0);
  39. ViewElementShow.IsVisible = true; // This is rotated at 90 or 270 degrees
  40. // Animate
  41. ViewElementHide.FadeTo(0.5, 100, Easing.SinOut);
  42. ViewElementHide.ScaleTo(0.2, 100, Easing.Linear);
  43. await ViewElementHide.RotateYTo(hideStop, 150, Easing.Linear);
  44. ViewElementShow.FadeTo(1, 100, Easing.SinIn);
  45. ViewElementShow.ScaleTo(1, 150, Easing.Linear);
  46. await ViewElementShow.RotateYTo(showStop, 150, Easing.Linear);
  47. ViewElementHide.IsVisible = false;
  48. }
  49. }

Here is the XAML code.

You need to add the reference like this.

xmlns:triggers="clr-namespace:FlipAnimation.Triggers"

  1. <ContentPage.Content>
  2. <Grid>
  3. <Grid.RowDefinitions>
  4. <RowDefinition Height="*" />
  5. </Grid.RowDefinitions>
  6. <Grid.ColumnDefinitions>
  7. <ColumnDefinition Width="*" />
  8. </Grid.ColumnDefinitions>
  9. <Image Opacity="1"
  10. Source="icon.png"
  11. Aspect="AspectFill"/>
  12. <Grid>
  13. <Grid.Padding>
  14. <OnPlatform x:TypeArguments="Thickness">
  15. <OnPlatform.iOS>
  16. 0, 20, 0, 0
  17. </OnPlatform.iOS>
  18. <OnPlatform.Android>
  19. 0, 0, 0, 0
  20. </OnPlatform.Android>
  21. </OnPlatform>
  22. </Grid.Padding>
  23. <Grid.RowDefinitions>
  24. <RowDefinition Height="Auto" />
  25. <RowDefinition Height="Auto" />
  26. <RowDefinition Height="*" />
  27. </Grid.RowDefinitions>
  28. <StackLayout Grid.Row="0">
  29. <Label Text="Login"
  30. TextColor="White"
  31. HorizontalTextAlignment="Center" />
  32. </StackLayout>
  33. <StackLayout Grid.Row="1">
  34. <!--If you want to use Loader and Error Message so you can comment out this-->
  35. <!--<ActivityIndicator x:Name="actInd" IsRunning="False" IsVisible="False" Color="Blue" />
  36. <Label x:Name="lblBusyMsg" IsVisible="False" Text="My Message" >
  37. <Label.Font>
  38. <OnPlatform x:TypeArguments="Font">
  39. <OnPlatform.iOS>Small</OnPlatform.iOS>
  40. </OnPlatform>
  41. </Label.Font>
  42. </Label>
  43. <Label x:Name="labelErrorMessage" IsVisible="False" Text="MY Message" TextColor="Red">
  44. <Label.Font>
  45. <OnPlatform x:TypeArguments="Font">
  46. <OnPlatform.iOS>Small</OnPlatform.iOS>
  47. </OnPlatform>
  48. </Label.Font>
  49. </Label>-->
  50. </StackLayout>
  51. <StackLayout x:Name="stLog"
  52. Grid.Row="1" Grid.Column="0"
  53. IsVisible="True"
  54. Padding="20,20,20,20"
  55. >
  56. <Entry Placeholder="Email" Text="" />
  57. <Entry Placeholder="Password" Text=""
  58. IsPassword="True" />
  59. <Button Text="Login" Clicked="Login_Clicked" />
  60. <Button Text="Register" >
  61. <Button.Triggers>
  62. <EventTrigger Event="Clicked">
  63. <triggers:SwitchLayout SourceElement="stLog" TargetElement="stReg" Direction="Right" />
  64. </EventTrigger>
  65. </Button.Triggers>
  66. </Button>
  67. <Button Text="Forgot Password">
  68. <Button.Triggers>
  69. <EventTrigger Event="Clicked">
  70. <triggers:SwitchLayout SourceElement="stLog" TargetElement="stForgotPass" Direction="Right" />
  71. </EventTrigger>
  72. </Button.Triggers>
  73. </Button>
  74. <Button Text="Change Password">
  75. <Button.Triggers>
  76. <EventTrigger Event="Clicked">
  77. <triggers:SwitchLayout SourceElement="stLog" TargetElement="stChangePass" Direction="Right" />
  78. </EventTrigger>
  79. </Button.Triggers>
  80. </Button>
  81. </StackLayout>
  82. <StackLayout x:Name="stReg" Grid.Row="1" Grid.Column="0" IsVisible="False" Padding="20" >
  83. <Entry Placeholder="Email" Text="" />
  84. <Entry Placeholder="First name" Text="" />
  85. <Entry Placeholder="Last name" Text="" />
  86. <Button Text="Register" Clicked="Register_Clicked" />
  87. <Button Text="Cancel">
  88. <Button.Triggers>
  89. <EventTrigger Event="Clicked">
  90. <triggers:SwitchLayout SourceElement="stReg" TargetElement="stLog" Direction="Left" />
  91. </EventTrigger>
  92. </Button.Triggers>
  93. </Button>
  94. </StackLayout>
  95. <StackLayout x:Name="stForgotPass" Grid.Row="1" Grid.Column="0" IsVisible="False" Padding="20" >
  96. <Entry Placeholder="Email"
  97. Text="" />
  98. <Button Text="Send email" Clicked="ForgetPass_Clicked"/>
  99. <Button Text="Cancel">
  100. <Button.Triggers>
  101. <EventTrigger Event="Clicked">
  102. <triggers:SwitchLayout SourceElement="stForgotPass" TargetElement="stLog" Direction="Left" />
  103. </EventTrigger>
  104. </Button.Triggers>
  105. </Button>
  106. </StackLayout>
  107. <StackLayout x:Name="stChangePass" Grid.Row="1" Grid.Column="0" IsVisible="False" Padding="20" >
  108. <Entry Placeholder="Old password"
  109. IsPassword="True" />
  110. <Entry Placeholder="New password" IsPassword="True" />
  111. <Entry Placeholder="Confirm password" IsPassword="True" />
  112. <Button Text="OK" Clicked="ChangePass_Clicked"/>
  113. <Button Text="Cancel">
  114. <Button.Triggers>
  115. <EventTrigger Event="Clicked">
  116. <triggers:SwitchLayout SourceElement="stChangePass" TargetElement="stLog" Direction="Left" />
  117. </EventTrigger>
  118. </Button.Triggers>
  119. </Button>
  120. </StackLayout>
  121. </Grid>
  122. </Grid>
  123. </ContentPage.Content>

If you want to watch this demo video, click here.

For complete source code, click here