-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAnimations.cs
267 lines (218 loc) · 9.54 KB
/
Animations.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
using System;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
namespace AnimationsCS
{
public static class Animations
{
#region EnterContent animation metrics
private static readonly TimeSpan EnterContentTranslateAnimationDuration = new TimeSpan(0, 0, 0, 0, 1000);
private static readonly TimeSpan EnterContentOpacityAnimationDelay = new TimeSpan(0, 0, 0, 0, 160);
private static readonly TimeSpan EnterContentOpacityAnimationDuration = new TimeSpan(0, 0, 0, 0, 400);
#endregion
public static Storyboard EnterContent(FrameworkElement element, int offsetX, int offsetY)
{
var storyboard = new Storyboard();
element.RenderTransform = new TranslateTransform
{
X = offsetX,
Y = offsetY
};
//First animation - Translation
storyboard.AddTranslateAnimations(element.RenderTransform,
EnterContentTranslateAnimationDuration,
offsetX, offsetY);
//Second animation - Opacity
storyboard.AddOpacityAnimation(element, EnterContentOpacityAnimationDuration,
EnterContentOpacityAnimationDelay);
storyboard.Begin();
return storyboard;
}
//overloaded method for animation without opacity
public static Storyboard EnterContent(FrameworkElement element, int offsetX, int offsetY, bool animateOpacity)
{
var storyboard = new Storyboard();
element.RenderTransform = new TranslateTransform
{
X = offsetX,
Y = offsetY
};
//First animation - Translation
storyboard.AddTranslateAnimations(element.RenderTransform,
EnterContentTranslateAnimationDuration,
offsetX, offsetY);
//Second animation - Opacity
if (animateOpacity)
storyboard.AddOpacityAnimation(element, EnterContentOpacityAnimationDuration,
EnterContentOpacityAnimationDelay);
storyboard.Begin();
return storyboard;
}
#region EnterPage animation metrics
private static readonly TimeSpan EnterPageOpacityDelay = new TimeSpan(0);
private static readonly TimeSpan EnterPageOpacityDuration = new TimeSpan(0, 0, 0, 0, 170);
#endregion
public static Storyboard EnterPage(FrameworkElement element, int offsetX, int offsetY)
{
var storyboard = new Storyboard();
element.RenderTransform = new TranslateTransform
{
X = offsetX,
Y = offsetY
};
//First animation - Translation
storyboard.AddTranslateAnimations(element.RenderTransform, EnterContentTranslateAnimationDuration,
offsetX, offsetY);
//Second animation - Opacity
storyboard.AddOpacityAnimation(element, EnterPageOpacityDuration, EnterPageOpacityDelay);
storyboard.Begin();
return storyboard;
}
#region PeekAnimation metrics
private static readonly TimeSpan PeekAnimationDuration = new TimeSpan(0, 0, 0, 0, 2000);
#endregion
public static Storyboard PeekAnimation(FrameworkElement element, int offsetX, int offsetY)
{
var storyboard = new Storyboard();
element.RenderTransform = new TranslateTransform
{
X = offsetX,
Y = offsetY
};
storyboard.AddTranslateAnimations(element.RenderTransform, PeekAnimationDuration,
offsetX, offsetY);
storyboard.Begin();
return storyboard;
}
#region ShowEdgeUI metrics
private static readonly TimeSpan ShowEdgeUIDuration = new TimeSpan(0, 0, 0, 0, 367);
#endregion
public static Storyboard ShowEdgeUI(FrameworkElement element, int offsetX, int offsetY)
{
var storyboard = new Storyboard();
element.RenderTransform = new TranslateTransform
{
X = offsetX,
Y = offsetY
};
storyboard.AddTranslateAnimations(element.RenderTransform, ShowEdgeUIDuration,
offsetX, offsetY);
storyboard.Begin();
return storyboard;
}
#region ShowPanel metrics
private static readonly TimeSpan ShowPanelDuration = new TimeSpan(0, 0, 0, 0, 550);
#endregion
public static Storyboard ShowPanel(FrameworkElement element, int offsetX, int offsetY)
{
var storyboard = new Storyboard();
element.RenderTransform = new TranslateTransform
{
X = offsetX,
Y = offsetY
};
storyboard.AddTranslateAnimations(element.RenderTransform, ShowPanelDuration,
offsetX, offsetY);
storyboard.Begin();
return storyboard;
}
#region UpdateBadge metrics
private static readonly TimeSpan UpdateBadgeOpacityDelay = new TimeSpan(0);
private static readonly TimeSpan UpdateBadgeOpacityDuration = new TimeSpan(0, 0, 0, 0, 367);
private static readonly TimeSpan UpdateBadgeTranslateAnimationDuration = new TimeSpan(0, 0, 0, 0, 1333);
#endregion
public static Storyboard UpdateBadge(FrameworkElement element, int offsetX, int offsetY)
{
var storyboard = new Storyboard();
element.RenderTransform = new TranslateTransform
{
X = offsetX,
Y = offsetY
};
//First animation - Translation
storyboard.AddTranslateAnimations(element.RenderTransform, UpdateBadgeTranslateAnimationDuration,
offsetX, offsetY);
//Second animation - Opacity
storyboard.AddOpacityAnimation(element, UpdateBadgeOpacityDuration, UpdateBadgeOpacityDelay);
storyboard.Begin();
return storyboard;
}
#region Animation constructor methods
public static void AddTranslateAnimations(this Storyboard storyboard, DependencyObject target, TimeSpan duration, int offsetX, int offsetY)
{
if (offsetX != 0)
{
var translationAnimationX = CreateEnterContentTranslateAnimationWithKeyFrames(duration);
Storyboard.SetTarget(translationAnimationX, target);
Storyboard.SetTargetProperty(translationAnimationX, "X");
storyboard.Children.Add(translationAnimationX);
}
if (offsetY != 0)
{
var translationAnimationY = CreateEnterContentTranslateAnimationWithKeyFrames(duration);
Storyboard.SetTarget(translationAnimationY, target);
Storyboard.SetTargetProperty(translationAnimationY, "Y");
storyboard.Children.Add(translationAnimationY);
}
}
public static void AddOpacityAnimation(this Storyboard storyboard, FrameworkElement element, TimeSpan duration, TimeSpan delay)
{
var opacityAnimation = new DoubleAnimationUsingKeyFrames
{
EnableDependentAnimation = true,
Duration = duration,
BeginTime = delay
};
opacityAnimation.KeyFrames.Add(new SplineDoubleKeyFrame
{
KeySpline = new KeySpline
{
ControlPoint1 = new Point(
TransitionBezierConrolPoint1X,
TransitionBezierConrolPoint1Y),
ControlPoint2 = new Point(
TransitionBezierConrolPoint2X,
TransitionBezierConrolPoint2Y)
},
KeyTime = KeyTime.FromTimeSpan(duration),
Value = 1
});
Storyboard.SetTarget(opacityAnimation, element);
Storyboard.SetTargetProperty(opacityAnimation, "Opacity");
storyboard.Children.Add(opacityAnimation);
element.Opacity = 0;
}
private static DoubleAnimationUsingKeyFrames CreateEnterContentTranslateAnimationWithKeyFrames(TimeSpan duration)
{
var translationAnimation = new DoubleAnimationUsingKeyFrames
{
EnableDependentAnimation = true,
Duration = new Duration(duration)
};
translationAnimation.KeyFrames.Add(new SplineDoubleKeyFrame
{
KeySpline = new KeySpline
{
ControlPoint1 = new Point(
TransitionBezierConrolPoint1X,
TransitionBezierConrolPoint1Y),
ControlPoint2 = new Point(
TransitionBezierConrolPoint2X,
TransitionBezierConrolPoint2Y)
},
KeyTime = KeyTime.FromTimeSpan(duration),
Value = 0
});
return translationAnimation;
}
#endregion
#region Animation bezier control points //same for all animations
private const double TransitionBezierConrolPoint1X = 0.10000000149011612;
private const double TransitionBezierConrolPoint1Y = 0.89999997615811421;
private const double TransitionBezierConrolPoint2X = 0.20000000298023224;
private const double TransitionBezierConrolPoint2Y = 1;
#endregion
}
}