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