Learn to use extension methods to create animations in your .NET MAUI app.
Adding animations to your user interfaces is a valuable way to enhance your design and create a more pleasant and intuitive experience for users. .NET MAUI provides a set of basic animations that gradually change a property from one value to another over a period of time, allowing you to perceive an animation in the visual elements of your app.
To create these animations, you can use the extension methods provided by the ViewExtensions class. In this article, I will guide you through a quick implementation of the main properties:
When using the animation extension methods in the ViewExtensions class, a Task<bool>
object is returned. These methods are asynchronous, and false is returned upon completion, while
true is returned upon abortion. Now, let’s explore some of the main extension methods.
The responsibility of FadeTo is to create a fading animation effect by changing the Opacity property of a visual element in a progressive manner. Receives the following parameters:
Let’s see an example in code:
async void animation()
{
heart.Opacity = 0;
await heart.FadeTo(1, 6000);
}
TranslateTo is responsible for generating a translation animation effect that progressively affects the X and Y properties of a visual element. Receives the following parameters:
Let’s see an example in code:
async void animation()
{
await heart.TranslateTo(40, 90, 1000);
}
It’s responsible for progressively creating an animated scale for a visual element by changing the Scale property. Receives the following parameters:
Let’s see an example in code:
async void animation()
{
await heart.ScaleTo(2, 4000);
}
Generates a continuously rotating animation that directly affects the Rotation property of a visual element. Receives the following parameters:
Let’s see an example in code:
async void animation()
{
await heart.RotateTo(360, 3000);
}
⚠ You can also use the RotateXTo and RotateYTo methods to animate the RotationX and RotationY properties, respectively.
The application can also cancel the animation that is in progress. You just have to call the CancelAnimations extension method, as I show you below:
async void animation()
{
heart.CancelAnimations();
}
I hope this list of helpful basic animations in .NET MAUI was beneficial to you! I encourage you to implement them into your daily life and continue exploring others! 💚💕
See you next time! 💁♀️
This article was based on the official documentation:
Leomaris Reyes is a Software Engineer from the Dominican Republic, with more than 5 years of experience. A Xamarin Certified Mobile Developer, she is also the founder of Stemelle, an entity that works with software developers, training and mentoring with a main goal of including women in Tech. Leomaris really loves learning new things! 💚💕 You can follow her: Twitter, LinkedIn , AskXammy and Medium.