-
Notifications
You must be signed in to change notification settings - Fork 1
/
AnimationHelper.cs
64 lines (56 loc) · 2.44 KB
/
AnimationHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media.Animation;
namespace ZoomAndPan
{
/// <summary>
/// A helper class to simplify animation.
/// </summary>
public static class AnimationHelper
{
/// <summary>
/// Starts an animation to a particular value on the specified dependency property.
/// </summary>
public static void StartAnimation(UIElement animatableElement, DependencyProperty dependencyProperty, double toValue, double animationDurationSeconds)
{
StartAnimation(animatableElement, dependencyProperty, toValue, animationDurationSeconds, null);
}
/// <summary>
/// Starts an animation to a particular value on the specified dependency property.
/// You can pass in an event handler to call when the animation has completed.
/// </summary>
public static void StartAnimation(UIElement animatableElement, DependencyProperty dependencyProperty, double toValue, double animationDurationSeconds, EventHandler completedEvent)
{
double fromValue = (double)animatableElement.GetValue(dependencyProperty);
DoubleAnimation animation = new DoubleAnimation();
animation.From = fromValue;
animation.To = toValue;
animation.Duration = TimeSpan.FromSeconds(animationDurationSeconds);
animation.Completed += delegate(object sender, EventArgs e)
{
//
// When the animation has completed bake final value of the animation
// into the property.
//
animatableElement.SetValue(dependencyProperty, animatableElement.GetValue(dependencyProperty));
CancelAnimation(animatableElement, dependencyProperty);
if (completedEvent != null)
{
completedEvent(sender, e);
}
};
animation.Freeze();
animatableElement.BeginAnimation(dependencyProperty, animation);
}
/// <summary>
/// Cancel any animations that are running on the specified dependency property.
/// </summary>
public static void CancelAnimation(UIElement animatableElement, DependencyProperty dependencyProperty)
{
animatableElement.BeginAnimation(dependencyProperty, null);
}
}
}