Repository navigation
Expand file tree
/
Copy pathFluentAnimate.cs
More file actions
266 lines (231 loc) · 8.63 KB
/
Copy pathFluentAnimate.cs
File metadata and controls
266 lines (231 loc) · 8.63 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.UIKit;
namespace AccidentalFish.UIKit
{
public interface IFluentAnimateTail
{
IFluentAnimateTail Repeat();
IFluentAnimateTail AllowUserInteraction();
IFluentAnimateTail WhenComplete(Action action);
void Start();
}
public interface IFluentAnimate : IFluentAnimateTail
{
IFluentAnimate Then { get; }
IFluentAnimate Do(Action action);
IFluentAnimate After(double delay);
IFluentAnimate AutoReverse();
IFluentAnimate EaseIn(Action action);
IFluentAnimate EaseIn(double duration, Action action);
IFluentAnimate EaseOut(double duration, Action action);
IFluentAnimate EaseOut(Action action);
IFluentAnimate Linear(double duration, Action action);
IFluentAnimate Linear(Action action);
IFluentAnimate EaseInOut(double duration, Action action);
IFluentAnimate EaseInOut(Action action);
}
public class FluentAnimate : IFluentAnimate
{
private struct Animation
{
public bool IsActionOnly;
public double Duration;
public Action Action;
public double Delay;
public UIViewAnimationOptions AnimationOptions;
public bool AutoReverses;
}
private readonly Stack<Animation> _actions = new Stack<Animation>();
private bool _repeats;
private double? _nextDelay;
private double? _repeaterDelay;
private Action _repeaterAction;
private bool _allowsUserInteraction;
private Action _whenCompleteAction;
public static IFluentAnimate EaseOut(double duration, Action action)
{
IFluentAnimate animator = new FluentAnimate();
animator.EaseOut(duration, action);
return animator;
}
public static IFluentAnimate EaseIn(double duration, Action action)
{
IFluentAnimate animator = new FluentAnimate();
animator.EaseIn(duration, action);
return animator;
}
public static IFluentAnimate EaseInOut(double duration, Action action)
{
IFluentAnimate animator = new FluentAnimate();
animator.EaseIn(duration, action);
return animator;
}
public static IFluentAnimate Linear(double duration, Action action)
{
IFluentAnimate animator = new FluentAnimate();
animator.EaseIn(duration, action);
return animator;
}
#region IFluentAnimate implementation
IFluentAnimate IFluentAnimate.After(double delay)
{
_nextDelay = delay;
return this;
}
IFluentAnimate IFluentAnimate.Then { get { return this; } }
IFluentAnimate IFluentAnimate.EaseIn(Action action)
{
AddActionWithPreviousDuration(action, UIViewAnimationOptions.CurveEaseIn);
return this;
}
IFluentAnimate IFluentAnimate.EaseIn(double duration, Action action)
{
AddActionWithDuration(action, duration, UIViewAnimationOptions.CurveEaseIn);
return this;
}
IFluentAnimate IFluentAnimate.EaseOut(Action action)
{
AddActionWithPreviousDuration(action, UIViewAnimationOptions.CurveEaseOut);
return this;
}
IFluentAnimate IFluentAnimate.EaseOut(double duration, Action action)
{
AddActionWithDuration(action, duration, UIViewAnimationOptions.CurveEaseOut);
return this;
}
IFluentAnimate IFluentAnimate.EaseInOut(Action action)
{
AddActionWithPreviousDuration(action, UIViewAnimationOptions.CurveEaseInOut);
return this;
}
IFluentAnimate IFluentAnimate.EaseInOut(double duration, Action action)
{
AddActionWithDuration(action, duration, UIViewAnimationOptions.CurveEaseInOut);
return this;
}
IFluentAnimate IFluentAnimate.Linear(Action action)
{
AddActionWithPreviousDuration(action, UIViewAnimationOptions.CurveLinear);
return this;
}
IFluentAnimate IFluentAnimate.Linear(double duration, Action action)
{
AddActionWithDuration(action, duration, UIViewAnimationOptions.CurveLinear);
return this;
}
IFluentAnimate IFluentAnimate.Do(Action action)
{
AddDoAction(action);
return this;
}
IFluentAnimate IFluentAnimate.AutoReverse()
{
Animation animation = _actions.Pop();
animation.AutoReverses = true;
_actions.Push(animation);
return this;
}
#endregion
#region IFluentAnimateTail implementation
IFluentAnimateTail IFluentAnimateTail.Repeat()
{
_repeaterDelay = _nextDelay;
_repeats = true;
return this;
}
IFluentAnimateTail IFluentAnimateTail.AllowUserInteraction()
{
_allowsUserInteraction = true;
return this;
}
IFluentAnimateTail IFluentAnimateTail.WhenComplete(Action action)
{
_whenCompleteAction = action;
return this;
}
void IFluentAnimateTail.Start()
{
Action nextAction = null;
bool stackIsEmpty;
do
{
Animation animation = _actions.Pop();
stackIsEmpty = !_actions.Any();
UIViewAnimationOptions options = animation.AnimationOptions;
if (_allowsUserInteraction) options = options | UIViewAnimationOptions.AllowUserInteraction;
if (animation.AutoReverses) options |= UIViewAnimationOptions.Autoreverse;
nextAction = CreateChainedAnimationAction(animation, options, nextAction);
if (stackIsEmpty && _repeats)
{
if (_repeaterDelay.HasValue)
{
animation.Delay = _repeaterDelay.Value;
_repeaterAction = CreateChainedAnimationAction(animation, options, nextAction);
}
else
{
_repeaterAction = nextAction;
}
_repeaterDelay = null;
}
_nextDelay = null;
} while (!stackIsEmpty);
nextAction();
}
#endregion
#region Helpers
private Action CreateChainedAnimationAction(Animation animation, UIViewAnimationOptions options, Action nextAction)
{
if (animation.IsActionOnly)
{
return () =>
{
animation.Action();
if (nextAction != null) nextAction();
else
{
if (_whenCompleteAction != null) _whenCompleteAction();
if (_repeaterAction != null) _repeaterAction();
}
};
}
return () =>
UIView.Animate(animation.Duration, animation.Delay, options,
() => animation.Action(),
() =>
{
if (nextAction != null) nextAction();
else
{
if (_whenCompleteAction != null) _whenCompleteAction();
if (_repeaterAction != null) _repeaterAction();
}
});
}
private void AddActionWithPreviousDuration(Action action, UIViewAnimationOptions animationOptions)
{
AddActionWithDuration(action, _actions.Last().Duration, animationOptions);
}
private void AddActionWithDuration(Action action, double duration, UIViewAnimationOptions animationOptions)
{
_actions.Push(new Animation
{
Action = action,
AnimationOptions = animationOptions,
Duration = duration,
Delay = _nextDelay.HasValue ? _nextDelay.Value : 0
});
}
private void AddDoAction(Action action)
{
_actions.Push(new Animation
{
Action = action,
IsActionOnly = true
});
}
#endregion
}
}