How To Create An Instagram Like Heart Animation In A UWP App

We all have seen the lovely heart animation we get when we double tap on a photo in Instagram. Well, it’s pretty easy to get a similar kind of animation in a UWP app using XAML storyboards. So, let’s get started. First of all, create a blank Universal Windows app in Visual Studio.

For the heart, we can use the Segoe MDL2 Assets font, which is built into Windows 10. Also, we can easily scale them without getting any blurriness. We can use the `FontIcon` control to add these shapes. We need two heart shapes - one with stroke and the other one with fill.

Basically, what we are going to do is scale up the filled heart shape when the user double taps the other heart shape. So, we are going to overlap them so that the filled heart is in the front. Below is the XAML code to achieve the above.

  1. <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  2.     <FontIcon x:Name="HeartBase" VerticalAlignment="Center" HorizontalAlignment="Center" Glyph="" RenderTransformOrigin="0.5,0.5" Foreground="#FFDE1C1C" DoubleTapped="HeartBase_DoubleTapped">  
  3.         <FontIcon.RenderTransform>  
  4.             <ScaleTransform ScaleX="4" ScaleY="4" /> </FontIcon.RenderTransform>  
  5.     </FontIcon>  
  6.     <FontIcon x:Name="Heart" VerticalAlignment="Center" HorizontalAlignment="Center" Glyph="" RenderTransformOrigin="0.5,0.5" Foreground="#FFDE1C1C" Visibility="Collapsed">  
  7.         <FontIcon.RenderTransform>  
  8.             <ScaleTransform x:Name="HeartScaleTransform" /> </FontIcon.RenderTransform>  
  9.     </FontIcon>  
  10. </Grid>  

Now, we have to create the animations. To do that, we are going to use XAML `Storyboards`. We are going to change the value of ScaleX and ScaleY of the `ScaleTransform` of the filled heart. Below is the XAML code for the animations. The second `Storyboard` will add a little shrinking effect after the heart is fully scaled up. So, it will add a natural touch to the animation. We can add these two storyboards to the page resources -

  1. <Page.Resources>  
  2.     <!-- First storyboard -->  
  3.     <Storyboard x:Name="HeartAnimStoryboard">  
  4.         <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Heart" Storyboard.TargetProperty="Visibility">  
  5.             <DiscreteObjectKeyFrame KeyTime="0" Value="Visible" /> </ObjectAnimationUsingKeyFrames>  
  6.         <DoubleAnimation From="4.0" To="9.0" Duration="0:0:0.4" Storyboard.TargetName="HeartScaleTransform" Storyboard.TargetProperty="ScaleX">  
  7.             <DoubleAnimation.EasingFunction>  
  8.                 <QuadraticEase EasingMode="EaseOut" /> </DoubleAnimation.EasingFunction>  
  9.         </DoubleAnimation>  
  10.         <DoubleAnimation From="4.0" To="9.0" Duration="0:0:0.4" Storyboard.TargetName="HeartScaleTransform" Storyboard.TargetProperty="ScaleY">  
  11.             <DoubleAnimation.EasingFunction>  
  12.                 <QuadraticEase EasingMode="EaseOut" /> </DoubleAnimation.EasingFunction>  
  13.         </DoubleAnimation>  
  14.     </Storyboard>  
  15.     <!--Second storyboard-->  
  16.     <Storyboard x:Name="HeartBackAnimStoryboard">  
  17.         <DoubleAnimation From="9.0" To="7.0" Duration="0:0:1" Storyboard.TargetName="HeartScaleTransform" Storyboard.TargetProperty="ScaleY">  
  18.             <DoubleAnimation.EasingFunction>  
  19.                 <QuadraticEase EasingMode="EaseOut" /> </DoubleAnimation.EasingFunction>  
  20.         </DoubleAnimation>  
  21.         <DoubleAnimation From="9.0" To="7.0" Duration="0:0:1" Storyboard.TargetName="HeartScaleTransform" Storyboard.TargetProperty="ScaleX">  
  22.             <DoubleAnimation.EasingFunction>  
  23.                 <QuadraticEase EasingMode="EaseOut" /> </DoubleAnimation.EasingFunction>  
  24.         </DoubleAnimation>  
  25.     </Storyboard>  
  26. </Page.Resources>   

Finally, we have to add the `DoubleTap` event handler to the stroked heart shape. Inside the event handler, we are going to begin the first `Storyboard` and we are adding an event handler to the ‘Completed’ event of that `Storyboard`. So, we can stop the first one and begin the second ‘Storyboard’. Check the below code. 

  1. private void HeartBase_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e) {  
  2.     HeartAnimStoryboard.Begin();  
  3.     HeartAnimStoryboard.Completed += HeartAnimStoryboard_Completed;  
  4. }  
  5. private void HeartAnimStoryboard_Completed(object sender, object e) {  
  6.     HeartBackAnimStoryboard.Begin();  
  7.     HeartBackAnimStoryboard.Completed += (s, m) => {  
  8.         HeartBackAnimStoryboard.Stop();  
  9.         HeartAnimStoryboard.Stop();  
  10.     };  
  11.     HeartAnimStoryboard.Completed -= HeartAnimStoryboard_Completed;  
  12. }   

Now, we can run the app. The below image is the final result. Now, this is very simple version for the sake of explaining. But in a real app, these shapes will probably end up in a user control.

UWP

Up Next
    Ebook Download
    View all
    Learn
    View all