Skip to content

Commit

Permalink
Added two extension methods to create expression animations
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Jun 21, 2017
1 parent c2a77ff commit 23e4491
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
56 changes: 56 additions & 0 deletions UICompositionAnimations/CompositionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,62 @@ public static async Task StartCompositionRollAnimationAsync(this FrameworkElemen

#endregion

#region Expression animations

/// <summary>
/// Creates and starts an animation on the target element that binds either the X or Y axis of the source <see cref="ScrollViewer"/>
/// </summary>
/// <param name="scroller">The source <see cref="ScrollViewer"/> control to use</param>
/// <param name="target">The target <see cref="UIElement"/> that will be animated</param>
/// <param name="sourceXY">The scrolling axis of the source <see cref="ScrollViewer"/></param>
/// <param name="targetXY">The scrolling axis of the target element</param>
/// <param name="invertSourceAxis">Indicates whether or not to invert the animation from the source <see cref="CompositionPropertySet"/></param>
[NotNull]
public static ExpressionAnimation StartExpressionAnimation(
[NotNull] this ScrollViewer scroller, [NotNull] UIElement target,
TranslationAxis sourceXY, TranslationAxis targetXY, bool invertSourceAxis = false)
{
CompositionPropertySet scrollSet = ElementCompositionPreview.GetScrollViewerManipulationPropertySet(scroller);
String sign = invertSourceAxis ? "-" : String.Empty;
ExpressionAnimation animation = scrollSet.Compositor.CreateExpressionAnimation($"{sign}scroll.Translation.{sourceXY}");
animation.SetReferenceParameter("scroll", scrollSet);
target.GetVisual().StartAnimation($"Offset.{targetXY}", animation);
return animation;
}

/// <summary>
/// Creates and starts an animation on the target element, with the addition of a scalar parameter in the resulting <see cref="ExpressionAnimation"/>
/// </summary>
/// <param name="scroller">The source <see cref="ScrollViewer"/> control to use</param>
/// <param name="target">The target <see cref="UIElement"/> that will be animated</param>
/// <param name="sourceXY">The scrolling axis of the source <see cref="ScrollViewer"/></param>
/// <param name="targetXY">The scrolling axis of the target element</param>
/// <param name="parameter">An additional parameter that will be included in the expression animation</param>
/// <param name="invertSourceAxis">Indicates whether or not to invert the animation from the source <see cref="CompositionPropertySet"/></param>
[NotNull]
public static ExpressionAnimation StartExpressionAnimation(
[NotNull] this ScrollViewer scroller, [NotNull] UIElement target,
TranslationAxis sourceXY, TranslationAxis targetXY,
float parameter, bool invertSourceAxis = false)
{
// Get the property set and setup the scroller offset sign
CompositionPropertySet scrollSet = ElementCompositionPreview.GetScrollViewerManipulationPropertySet(scroller);
String sign = invertSourceAxis ? "-" : "+";

// Prepare the second property set to insert the additional parameter
CompositionPropertySet properties = scroller.GetVisual().Compositor.CreatePropertySet();
properties.InsertScalar(nameof(parameter), parameter);

// Create and start the animation
ExpressionAnimation animation = scrollSet.Compositor.CreateExpressionAnimation($"{nameof(properties)}.{nameof(parameter)} {sign} scroll.Translation.{sourceXY}");
animation.SetReferenceParameter("scroll", scrollSet);
animation.SetReferenceParameter(nameof(properties), properties);
target.GetVisual().StartAnimation($"Offset.{targetXY}", animation);
return animation;
}

#endregion

#region Shadows

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion UICompositionAnimations/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.3.3.0")]
[assembly: AssemblyVersion("2.4.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: ComVisible(false)]
4 changes: 2 additions & 2 deletions UICompositionAnimations/UICompositionAnimations.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
<package >
<metadata>
<id>Sergio0694.UWP.UICompositionAnimations</id>
<version>2.3.3.0</version>
<version>2.4.0.0</version>
<title>UICompositionAnimations</title>
<description>A wrapper UWP PCL to work with Windows.UI.Composition and XAML animations, and Win2D effects</description>
<authors>Sergio Pedri</authors>
<owners>Sergio Pedri</owners>
<projectUrl>https://github.com/Sergio0694/UICompositionAnimations</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<releaseNotes>Minor bug fixes</releaseNotes>
<releaseNotes>Added the ability to create and start composition expression animations</releaseNotes>
<copyright>Copyright 2017</copyright>
<tags>uwp composition animations xaml csharp windows winrt universal app ui win2d graphics</tags>
</metadata>
Expand Down

0 comments on commit 23e4491

Please sign in to comment.