forked from react/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRCTSpringAnimation.m
More file actions
200 lines (166 loc) · 5.69 KB
/
Copy pathRCTSpringAnimation.m
File metadata and controls
200 lines (166 loc) · 5.69 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
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import "RCTSpringAnimation.h"
#import <UIKit/UIKit.h>
#import "RCTConvert.h"
#import "RCTAnimationUtils.h"
#import "RCTDefines.h"
#import "RCTValueAnimatedNode.h"
@interface RCTSpringAnimation ()
@property (nonatomic, strong) NSNumber *animationId;
@property (nonatomic, strong) RCTValueAnimatedNode *valueNode;
@property (nonatomic, assign) BOOL animationHasBegun;
@property (nonatomic, assign) BOOL animationHasFinished;
@end
@implementation RCTSpringAnimation
{
CGFloat _toValue;
CGFloat _fromValue;
BOOL _overshootClamping;
CGFloat _restDisplacementThreshold;
CGFloat _restSpeedThreshold;
CGFloat _tension;
CGFloat _friction;
CGFloat _initialVelocity;
NSTimeInterval _animationStartTime;
NSTimeInterval _animationCurrentTime;
RCTResponseSenderBlock _callback;
CGFloat _lastPosition;
CGFloat _lastVelocity;
}
- (instancetype)initWithId:(NSNumber *)animationId
config:(NSDictionary *)config
forNode:(RCTValueAnimatedNode *)valueNode
callBack:(nullable RCTResponseSenderBlock)callback
{
if ((self = [super init])) {
_animationId = animationId;
_toValue = [RCTConvert CGFloat:config[@"toValue"]];
_fromValue = valueNode.value;
_valueNode = valueNode;
_overshootClamping = [RCTConvert BOOL:config[@"overshootClamping"]];
_restDisplacementThreshold = [RCTConvert CGFloat:config[@"restDisplacementThreshold"]];
_restSpeedThreshold = [RCTConvert CGFloat:config[@"restSpeedThreshold"]];
_tension = [RCTConvert CGFloat:config[@"tension"]];
_friction = [RCTConvert CGFloat:config[@"friction"]];
_initialVelocity = [RCTConvert CGFloat:config[@"initialVelocity"]];
_callback = [callback copy];
_lastPosition = _fromValue;
_lastVelocity = _initialVelocity;
}
return self;
}
RCT_NOT_IMPLEMENTED(- (instancetype)init)
- (void)startAnimation
{
_animationStartTime = CACurrentMediaTime();
_animationCurrentTime = _animationStartTime;
_animationHasBegun = YES;
}
- (void)stopAnimation
{
_animationHasFinished = YES;
}
- (void)removeAnimation
{
[self stopAnimation];
_valueNode = nil;
if (_callback) {
_callback(@[@{
@"finished": @(_animationHasFinished)
}]);
}
}
- (void)stepAnimation
{
if (!_animationHasBegun || _animationHasFinished) {
// Animation has not begun or animation has already finished.
return;
}
// We are using a fixed time step and a maximum number of iterations.
// The following post provides a lot of thoughts into how to build this
// loop: http://gafferongames.com/game-physics/fix-your-timestep/
CGFloat TIMESTEP_MSEC = 1;
// Velocity is based on seconds instead of milliseconds
CGFloat step = TIMESTEP_MSEC / 1000;
NSTimeInterval currentTime = CACurrentMediaTime();
NSInteger numSteps = floorf((currentTime - _animationCurrentTime) / step);
_animationCurrentTime = currentTime;
if (numSteps == 0) {
return;
}
CGFloat position = _lastPosition;
CGFloat velocity = _lastVelocity;
CGFloat tempPosition = _lastPosition;
CGFloat tempVelocity = _lastVelocity;
for (NSInteger i = 0; i < numSteps; ++i) {
// This is using RK4. A good blog post to understand how it works:
// http://gafferongames.com/game-physics/integration-basics/
CGFloat aVelocity = velocity;
CGFloat aAcceleration = _tension * (_toValue - tempPosition) - _friction * tempVelocity;
tempPosition = position + aVelocity * step / 2;
tempVelocity = velocity + aAcceleration * step / 2;
CGFloat bVelocity = tempVelocity;
CGFloat bAcceleration = _tension * (_toValue - tempPosition) - _friction * tempVelocity;
tempPosition = position + bVelocity * step / 2;
tempVelocity = velocity + bAcceleration * step / 2;
CGFloat cVelocity = tempVelocity;
CGFloat cAcceleration = _tension * (_toValue - tempPosition) - _friction * tempVelocity;
tempPosition = position + cVelocity * step / 2;
tempVelocity = velocity + cAcceleration * step / 2;
CGFloat dVelocity = tempVelocity;
CGFloat dAcceleration = _tension * (_toValue - tempPosition) - _friction * tempVelocity;
tempPosition = position + cVelocity * step / 2;
tempVelocity = velocity + cAcceleration * step / 2;
CGFloat dxdt = (aVelocity + 2 * (bVelocity + cVelocity) + dVelocity) / 6;
CGFloat dvdt = (aAcceleration + 2 * (bAcceleration + cAcceleration) + dAcceleration) / 6;
position += dxdt * step;
velocity += dvdt * step;
}
_lastPosition = position;
_lastVelocity = velocity;
[self onUpdate:position];
if (_animationHasFinished) {
return;
}
// Conditions for stopping the spring animation
BOOL isOvershooting = NO;
if (_overshootClamping && _tension != 0) {
if (_fromValue < _toValue) {
isOvershooting = position > _toValue;
} else {
isOvershooting = position < _toValue;
}
}
BOOL isVelocity = ABS(velocity) <= _restSpeedThreshold;
BOOL isDisplacement = YES;
if (_tension != 0) {
isDisplacement = ABS(_toValue - position) <= _restDisplacementThreshold;
}
if (isOvershooting || (isVelocity && isDisplacement)) {
if (_tension != 0) {
// Ensure that we end up with a round value
if (_animationHasFinished) {
return;
}
[self onUpdate:_toValue];
}
[self stopAnimation];
}
}
- (void)onUpdate:(CGFloat)outputValue
{
_valueNode.value = outputValue;
[_valueNode setNeedsUpdate];
}
- (void)cleanupAnimationUpdate
{
[_valueNode cleanupAnimationUpdate];
}
@end