Created
March 16, 2017 18:21
-
-
Save mikeplate/c98382e06d4f6deb7d83a427f2e61540 to your computer and use it in GitHub Desktop.
WPF Animation Helpers when switching UI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Windows; | |
| using System.Windows.Media; | |
| using System.Windows.Media.Animation; | |
| public class AnimationHelpers | |
| { | |
| public static void Fade(UIElement incoming, UIElement outgoing, int seconds) | |
| { | |
| Storyboard story = new Storyboard(); | |
| story.Children.Add(CreateAnimation(incoming, 0.0, 1.0, "Opacity", seconds)); | |
| story.Children.Add(CreateAnimation(outgoing, 1.0, 0.0, "Opacity", seconds)); | |
| story.Begin(); | |
| } | |
| public static void SlideLeft(UIElement incoming, UIElement outgoing, int seconds) | |
| { | |
| Move(incoming, outgoing, seconds, "X", incoming.RenderSize.Width, 0, 0, -outgoing.RenderSize.Width); | |
| } | |
| public static void SlideRight(UIElement incoming, UIElement outgoing, int seconds) | |
| { | |
| Move(incoming, outgoing, seconds, "X", -incoming.RenderSize.Width, 0, 0, outgoing.RenderSize.Width); | |
| } | |
| public static void SlideDown(UIElement incoming, UIElement outgoing, int seconds) | |
| { | |
| Move(incoming, outgoing, seconds, "Y", -incoming.RenderSize.Height, 0, 0, outgoing.RenderSize.Height); | |
| } | |
| public static void SlideUp(UIElement incoming, UIElement outgoing, int seconds) | |
| { | |
| Move(incoming, outgoing, seconds, "Y", incoming.RenderSize.Height, 0, 0, -outgoing.RenderSize.Height); | |
| } | |
| private static DoubleAnimation CreateAnimation(UIElement dst, double from, double to, string propertyPath, int seconds) | |
| { | |
| DoubleAnimation anim = new DoubleAnimation(from, to, TimeSpan.FromSeconds(seconds)); | |
| anim.FillBehavior = FillBehavior.HoldEnd; | |
| Storyboard.SetTarget(anim, dst); | |
| Storyboard.SetTargetProperty(anim, new PropertyPath(propertyPath)); | |
| return anim; | |
| } | |
| private static void Move(UIElement incoming, UIElement outgoing, int seconds, string xOrY, double inFrom, double inTo, double outFrom, double outTo) | |
| { | |
| Storyboard story = new Storyboard(); | |
| TranslateTransform incomingTransform = new TranslateTransform(); | |
| incoming.RenderTransform = incomingTransform; | |
| story.Children.Add(CreateAnimation(incoming, inFrom, inTo, "RenderTransform.(TranslateTransform." + xOrY + ")", seconds)); | |
| TranslateTransform outgoingTransform = new TranslateTransform(); | |
| outgoing.RenderTransform = outgoingTransform; | |
| story.Children.Add(CreateAnimation(outgoing, outFrom, outTo, "RenderTransform.(TranslateTransform." + xOrY + ")", seconds)); | |
| story.Begin(); | |
| } | |
| public static void Size(UIElement incoming, UIElement outgoing, int seconds) | |
| { | |
| Storyboard story = new Storyboard(); | |
| ScaleTransform incomingTransform = new ScaleTransform(); | |
| incomingTransform.CenterX = incoming.RenderSize.Width / 2; | |
| incomingTransform.CenterY = incoming.RenderSize.Height/ 2; | |
| incoming.RenderTransform = incomingTransform; | |
| story.Children.Add(CreateAnimation(incoming, 0.0, 1.0, "RenderTransform.(ScaleTransform.ScaleX)", seconds)); | |
| story.Children.Add(CreateAnimation(incoming, 0.0, 1.0, "RenderTransform.(ScaleTransform.ScaleY)", seconds)); | |
| ScaleTransform outgoingTransform = new ScaleTransform(); | |
| outgoingTransform.CenterX = outgoing.RenderSize.Width / 2; | |
| outgoingTransform.CenterY = outgoing.RenderSize.Height / 2; | |
| outgoing.RenderTransform = outgoingTransform; | |
| story.Children.Add(CreateAnimation(outgoing, 1.0, 0.0, "RenderTransform.(ScaleTransform.ScaleX)", seconds)); | |
| story.Children.Add(CreateAnimation(outgoing, 1.0, 0.0, "RenderTransform.(ScaleTransform.ScaleY)", seconds)); | |
| story.Begin(); | |
| } | |
| public static void SqueezeDown(UIElement incoming, UIElement outgoing, int seconds) | |
| { | |
| Storyboard story = new Storyboard(); | |
| TranslateTransform incomingTransform = new TranslateTransform(); | |
| incoming.RenderTransform = incomingTransform; | |
| story.Children.Add(CreateAnimation(incoming, -incoming.RenderSize.Height, 0, "RenderTransform.(TranslateTransform.Y)", seconds)); | |
| ScaleTransform outgoingTransform = new ScaleTransform(); | |
| outgoingTransform.CenterY = outgoing.RenderSize.Height; | |
| outgoing.RenderTransform = outgoingTransform; | |
| story.Children.Add(CreateAnimation(outgoing, 1.0, 0.0, "RenderTransform.(ScaleTransform.ScaleY)", seconds)); | |
| story.Begin(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment