UWP Connected Animations updates with Windows Creators release

Context is one of the most important things that helps us understand what is going on, without context things become crazy and prone to interpretation. Think about it if you were writing a piece of code where you have to show something on the screen you will need context to get the right information or to understand what information to get. If a friend of yours start to talk about moon travel and how great experience it was for him, you will definitely stop him right there as ask for some context to make sense of it all.

Dictionary definition for context:

The set of circumstances or facts that surround a particular event, situation, etc.

Similarly in UX when user navigates from one page to another to consume more information about a specific topic or item they require some context to help them be comfortable in their new surroundings and to make sense of it. ConnectedAnimationService provide a rich set of APIs for Windows UWP developers to create beautiful and engaging experiences which help users maintain context as they navigate to consume content. Some easy UX wins that you can achieve by using this API are

  1. Help user understand their action better, this helps user be more confident in your app and not lost. This helps “Time to action” which is a KPI to track user experience success.
  2. You can manage where should user focus when they land on the page, what is the most important thing that user should see first and start from as they start consuming content
  3. Remove any seams from your app. With this API you can absolutely remove all the flashes from your app navigation. It will feel like one cohesive and immersive experience
  4. Users experience a playful app which is keeping them engaged and informed during the most critical navigation.

What does a connected experience look like?

Here the user is navigating from a master to detail page to consume more information about the Item.

 

Connected Animations

XAML released a new API in Anniversary Update named ConnectedAnimationService. This API lets you create experiences like above with simple API calls. Basically the API is divided into 4 parts

  1. Identify the source element that you want to animate with navigation and provide an ID
  2. Identify the destination element that source will animate to.
  3. Prepare the animation on source page right before navigation
  4. On the destination page when the target element has _loaded get the animation with the ID then start the animation with destination element as target.

This is what the APIs look like

On your source page identify the element that you want to animate when it is clicked and results into navigation In your click handler

ConnectedAnimationService.GetForCurrentView().PrepareToAnimate("ID", SourceImage);
Frame.Navigate(typeof(DestinationPage));

Use Unique ID as you can have more than one element animating from source to destination page.

On destination page: using OnNavigatedTo of page or in case of Image use ImageOpened event handler perform the following code to get the animation associated with the ID that you identified on the source page and TryStart the same animation on the destination page’s element which you want to connect.

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    ConnectedAnimation imageAnimation = 
        ConnectedAnimationService.GetForCurrentView().GetAnimation("ID");
    if (imageAnimation != null)
    {
        imageAnimation.TryStart(DestinationImage);
    }
}

Custom Animations

Before Creators update ConnectedAnimationService provided only a default animation which meant your app navigation will look the same as everyone else. Creators update adds this much needed ability for developers to provide custom animations to have beautiful transitions that differentiate your app from others.

There are 4 components that you can customize

  1. Crossfade – Let’s you to customize how elements crossfade as source element reaches destination
  2. OffsetX – Let’s you customize the X channel of Offset.
  3. OffsetY – Let’s you customize the Y channel of Offset.
  4. Scale – Let’s you customize scale of the element as it animates

Customize X channel of your Offset

 

//OffsetX Custom Animation

 var customXAnimation = _compositor.CreateScalarKeyFrameAnimation();
 customXAnimation.InsertExpressionKeyFrame(0.5f, "FinalValue - 25f"); //You can also use StartingValue in case you need to use starting value to customize animation.
 customXAnimation.InsertExpressionKeyFrame(1f, "FinalValue");

ConnectedAnimationService.GetForCurrentView().GetAnimation("ID").SetAnimationComponent(ConnectedAnimationComponent.OffsetX, customXAnimation);

 

Customize Y channel of your Offset animation

//OffsetY Custom Animation

var customYAnimation = _compositor.CreateScalarKeyFrameAnimation();
customYAnimation.InsertExpressionKeyFrame(0.5f, "FinalValue - 25f"); //You can also use StartingValue in case you need to use starting value to customize animation.
customYAnimation.InsertExpressionKeyFrame(1f, "FinalValue");

ConnectedAnimationService.GetForCurrentView().GetAnimation("ID").SetAnimationComponent(ConnectedAnimationComponent.OffsetY, customYAnimation);

Simlarly you can also provide custom animations to define your crossfade and Scale to accomplish your scenario.

//CrossFade Custom Animation
 ConnectedAnimationService.GetForCurrentView().GetAnimation("ID").SetAnimationComponent(ConnectedAnimationComponent.CrossFade, scalarKeyFrameAnimation);
 
//Scale Custom Animation
ConnectedAnimationService.GetForCurrentView().GetAnimation("ID").SetAnimationComponent(ConnectedAnimationComponent.Scale, scalarKeyFrameAnimation);
 

Coordinate Animations across pages

Connected animations help you connect different pages together with context that animates across pages. Destination page where the element is animating to will have more information about object that user selected to help connect the dots between 2 different pages and with the element animating creators update released a new functionality with ConnectedAnimationService which lets you coordinate elements on the destination page with the animating element. E.g. In the sample below the text on the right is animating with the incoming image where as the text is on the destination page. If item which was clicked was on the right side then text seems to come from right whereas if the image is coming from left the text also animates in from left.

The text panel is only on the destination page and not on source page. Text block on your destination page in XAML looks like this

<StackPanel x:Name="DescriptionRoot" Grid.Column="1" Margin="0,80">
 <TextBlock Foreground="White" FontSize="18">Caption Text</TextBlock>
 <TextBlock Margin="0,10,0,0" Foreground="White" HorizontalAlignment="Left" FontSize="14" MaxWidth="500" TextWrapping="Wrap">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras lobortis nulla sit amet aliquet viverra. Etiam gravida luctus orci, at egestas leo auctor sed.</TextBlock>
 </StackPanel>

Now on your Destination page’s onNavigatedTo Event where you perform TryStart(…) with the incoming animation you provide list of elements that you want to coordinate as parameter and framework will do the work of coordinating animations across pages.

protected override void OnNavigatedTo(NavigationEventArgs e)
 {
 base.OnNavigatedTo(e);
 ConnectedAnimationService
 .GetForCurrentView()
 .GetAnimation("BorderSource")
 .TryStart(DestinationElement, new[] { DescriptionRoot });

 }

Image Interpolation

Sometimes we have different sized images across pages i.e. a square image performs a connected animation to destination where the image is defined as rectangle. With Creators update you don’t have to think about it a bit, platform performs the magic to automatically interpolation between image sizes. Just make sure you TryStart(..) the ConnectedAnimationService in your image control which has defined size _loaded event.

Conclusion

Connected animations provides your users a delightful and seamless experience which also makes them feel in control. Users don’t feel lost as they jump from one page to another. With the new updates in Creators release you can make connected animations your own and have a differentiated experience which delights your users and gets you that 5 star rating.

Resources

You can look at a running sample from Windows UI Sample Gallery – Navigation Flow For Show/Hide and Page transitions in this sample please refer to this post.

Complete Sample Gallery – Windows UI Samples

 

Let me know in the comments section if this was helpful and what else would you want to talk about.

Thanks!

Create custom page transitions with Windows Creators Update

Have you ever wonder what would happen if things suddenly show up and disappear in front of you, you will be surprised right? Similarly Apps consist of so many objects/Controls and pages, if they did the same thing i.e. appear and disappear suddenly then users will be surprised and it will be an awful experience. Apps consist of different pages to create an IA (Information Architecture) that helps users to easily understand different aspects of the app and consume content. Transitioning from one page to another is an experience which in inevitable and is experienced by all users. Page transitions are one of the most critical animation as it helps users to orient themselves and understand new page which is coming into view and what to expect, providing a great user experience and making it seamless will make users feel in control and be in love with your application. Not having any sort of transition leaves users with a bad taste as change is very sudden, not cohesive, jarring and does not provide any help to me as a user to understand what’s next.

As a UWP developer before Creators update could you could only use XAML theme transitions to achieve some page transition. The drawback of theme transitions is that developers cannot customize them to their needs to create their own app personality. There are also apps which don’t use any transition animations and users see a hard cut between pages which is jarring and very sudden.

Transitions

A transition is made up of two main components

  1. From – The state the user is transitioning from. It could be a page or state or value.
  2. To – The state the user is transitioning to. It could be a page or state or value.

Let’s look at a sample which shows some great page transitions and try and break it down

If you look the video closely you will notice few things

  1. There is a fade out animation which happens on the source page as user navigates away from it by clicking an item
  2. There is a connected Animation which helps Item 4 animate across pages – this is not covered in this post
  3. On the destination page there is a dark grey box that is animating down from top to its position
  4. On the destination page there is a light grey box that is animating from bottom to its position

Animating element as they Show

Elements appear on the screen due to multiple reasons, navigation loads a new page, objects appear on the page due to data binding or user added something to the page. In the video above user navigates to a new page to view more details about an item. If you don’t do anything in your UWP app elements will pop on the screen as they are added to the visual tree. It is not a very pleasant experience for the user as objects might appear in random order, asynchronous operations make this problem even worst.

To solve this problem Windows Creators Update has added a new API to the Implicit Animations named SetImplicitShowAnimation(…). This API lives on Windows.UI.XAML.Hosting.ElementCompositionPreview Class.

ElementCompositionPreview.SetImplicitShowAnimation(UIElement element, ICompositionAnimationBase animation);
 

//Sample usage to animate top border to its position as it shows on the screen
// Add a translation animation that will play when this element is shown
 var topBorderOffsetAnimation = _compositor.CreateScalarKeyFrameAnimation();
 topBorderOffsetAnimation.Duration = TimeSpan.FromSeconds(0.45);
 topBorderOffsetAnimation.Target = "Translation.Y";
 topBorderOffsetAnimation.InsertKeyFrame(0, -450.0f);
 topBorderOffsetAnimation.InsertKeyFrame(1, 0);
 ElementCompositionPreview.SetIsTranslationEnabled(TopBorder, true);
 ElementCompositionPreview.SetImplicitShowAnimation(TopBorder, topBorderOffsetAnimation);
 

Let’s talk about the code a bit.

This animation will automatically play on the TopBorder when it appears on the screen. Animation should be associated with UIElement before it is loaded. Animation provided as part of SetImplicitShowAnimation will only trigger at the time when Visuals enter the tree. Associate these animations on page’s constructor or when the object is created but has not yet loaded. If you associate these animations in Loaded Event then the animation will NOT play as the element is already in the tree and framework will not trigger this animation as it has passed the Show stage.

Visibility.Visible is another UIElement property which acts like a trigger to play any show animations associated with the UIElement.

Using Implicit Show animations can help you to create beautiful page transitions and animate elements into view gracefully without popping and flashing. You can also associate Show animation on the Page element itself to create FadeIn type of transitions.

Translation: If you have used Composition or the Visual layer before then you will notice this new property named Translation to help with offset stomping issue. App developers should use Translation to move their Visuals as Framework uses Offset for Layout. More on this topic in a coming post.

Animating elements as they hide

As Frame navigates to a new page users experience a sudden disappearance of objects and the page they are navigating from or in a general case when users delete some object from a page they just suddenly poof/disappear from the page rather than gracefully hiding using animations. In the video above user is navigating from source page to view details of a specific item. It has been very tough for developers to animate objects as they hide as once the object is removed it gets removed from the visual tree without giving any chance and opportunity to run any animation.

To solve this problem Creators Update added a new API to the Implicit Animation named SetImplicitHideAnimation(…), this API again lives on Windows.UI.XAML.Hosting.ElementCompositionPreview.

ElementCompositionPreview.SetImplicitHideAnimation(UIElement element, ICompositionAnimationBase animation);

//This API is very similar to Show animations above. Let's look at an example usage based on our Sample.
// Add a translation animation that will play when this element exits the scene
 var mainContentExitAnimation = _compositor.CreateScalarKeyFrameAnimation();
 mainContentExitAnimation.Target = "Translation.Y";
 mainContentExitAnimation.InsertKeyFrame(1, 30);
 mainContentExitAnimation.Duration = TimeSpan.FromSeconds(0.4);
 ElementCompositionPreview.SetIsTranslationEnabled(MainContent, true);
 ElementCompositionPreview.SetImplicitHideAnimation(MainContent, mainContentExitAnimation);

This code snippet is animating the translation. The animation will start as soon as element is marked to be removed from the tree. Framework does the heavy lifting of understanding that there is an animation associated with the element which is to be removed and hangs on to it till it finishes the animation. From a developers perspective it is removed from the Visual and UIElement collection and will not be returned if you try to enumerate its parent’s children.

Note: Framework retains the complete sub-tree/children of the element which is playing its animation as part of hiding. Another point worth noting here is that Hide animations are recursive in nature i.e. if you remove an element which has hide animation and contains children which have hide animations then removing the parent will kick off animations on children too. E.g. ListView with has ListItems, both have hide animations associated with them. If you remove the ListView then framework will trigger all the hide animations on all the elements under ListView. Developers do not have to go an individually remove items to play their animations framework does this by itself.

Visibility.Collapse also participates in Hide animations. i.e. if UIElement has a hide animation and the visibility is collapsed then element will collapse using the hide animation.

End to End Page Transition

Page Transitions are tricky but also is a great opportunity to bring a great seamless experience to users and help them better understand your application by setting context, helping them focus on things and areas where they need to pay attention or take action, orienting the users as they see a new page in front of them will increase your apps engagement and satisfaction.

Implicit show and hide animations, makes it easy for you to create these beautiful seamless experiences by just defining animations that you want to play and let the framework and platform do the rest of animating objects as they show and hide on the screen.

Resources

You can look at a running sample from Windows UI Sample Gallery – Navigation Flow It also has some connected animations which is posted here.

Complete Sample Gallery – Windows UI Samples

Thanks!