2016-06-09 17:34:41 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-06-09 17:34:41 +00:00
|
|
|
*/
|
|
|
|
|
2016-10-21 11:20:01 +00:00
|
|
|
#import "RCTFrameAnimation.h"
|
2016-06-09 17:34:41 +00:00
|
|
|
|
|
|
|
#import <UIKit/UIKit.h>
|
|
|
|
|
2016-11-23 15:47:52 +00:00
|
|
|
#import <React/RCTConvert.h>
|
|
|
|
#import <React/RCTDefines.h>
|
|
|
|
|
2016-06-09 17:34:41 +00:00
|
|
|
#import "RCTAnimationUtils.h"
|
|
|
|
#import "RCTValueAnimatedNode.h"
|
|
|
|
|
2016-10-21 11:20:01 +00:00
|
|
|
@interface RCTFrameAnimation ()
|
|
|
|
|
|
|
|
@property (nonatomic, strong) NSNumber *animationId;
|
|
|
|
@property (nonatomic, strong) RCTValueAnimatedNode *valueNode;
|
|
|
|
@property (nonatomic, assign) BOOL animationHasBegun;
|
|
|
|
@property (nonatomic, assign) BOOL animationHasFinished;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTFrameAnimation
|
2016-06-09 17:34:41 +00:00
|
|
|
{
|
|
|
|
NSArray<NSNumber *> *_frames;
|
|
|
|
CGFloat _toValue;
|
|
|
|
CGFloat _fromValue;
|
2018-02-16 19:43:34 +00:00
|
|
|
CGFloat _lastPosition;
|
2016-06-09 17:34:41 +00:00
|
|
|
NSTimeInterval _animationStartTime;
|
|
|
|
NSTimeInterval _animationCurrentTime;
|
|
|
|
RCTResponseSenderBlock _callback;
|
2017-05-26 10:24:29 +00:00
|
|
|
NSInteger _iterations;
|
|
|
|
NSInteger _currentLoop;
|
2016-06-09 17:34:41 +00:00
|
|
|
}
|
|
|
|
|
2016-10-21 11:20:01 +00:00
|
|
|
- (instancetype)initWithId:(NSNumber *)animationId
|
|
|
|
config:(NSDictionary *)config
|
|
|
|
forNode:(RCTValueAnimatedNode *)valueNode
|
|
|
|
callBack:(nullable RCTResponseSenderBlock)callback;
|
2016-06-09 17:34:41 +00:00
|
|
|
{
|
|
|
|
if ((self = [super init])) {
|
|
|
|
_animationId = animationId;
|
2018-02-16 19:43:34 +00:00
|
|
|
_lastPosition = _fromValue = valueNode.value;
|
2016-06-09 17:34:41 +00:00
|
|
|
_valueNode = valueNode;
|
|
|
|
_callback = [callback copy];
|
2018-02-16 19:43:34 +00:00
|
|
|
[self resetAnimationConfig:config];
|
2016-06-09 17:34:41 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2018-02-16 19:43:34 +00:00
|
|
|
- (void)resetAnimationConfig:(NSDictionary *)config
|
|
|
|
{
|
|
|
|
NSNumber *toValue = [RCTConvert NSNumber:config[@"toValue"]] ?: @1;
|
|
|
|
NSArray<NSNumber *> *frames = [RCTConvert NSNumberArray:config[@"frames"]];
|
|
|
|
NSNumber *iterations = [RCTConvert NSNumber:config[@"iterations"]] ?: @1;
|
|
|
|
|
|
|
|
_fromValue = _lastPosition;
|
|
|
|
_toValue = toValue.floatValue;
|
|
|
|
_frames = [frames copy];
|
|
|
|
_animationStartTime = _animationCurrentTime = -1;
|
|
|
|
_animationHasFinished = iterations.integerValue == 0;
|
|
|
|
_iterations = iterations.integerValue;
|
|
|
|
_currentLoop = 1;
|
|
|
|
}
|
|
|
|
|
2016-06-09 17:34:41 +00:00
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
|
|
|
|
|
|
|
- (void)startAnimation
|
|
|
|
{
|
2017-03-28 16:08:51 +00:00
|
|
|
_animationStartTime = _animationCurrentTime = -1;
|
2016-06-09 17:34:41 +00:00
|
|
|
_animationHasBegun = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)stopAnimation
|
|
|
|
{
|
|
|
|
_valueNode = nil;
|
|
|
|
if (_callback) {
|
2016-07-05 17:58:17 +00:00
|
|
|
_callback(@[@{
|
|
|
|
@"finished": @(_animationHasFinished)
|
|
|
|
}]);
|
2016-06-09 17:34:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-28 16:08:51 +00:00
|
|
|
- (void)stepAnimationWithTime:(NSTimeInterval)currentTime
|
2016-06-09 17:34:41 +00:00
|
|
|
{
|
|
|
|
if (!_animationHasBegun || _animationHasFinished || _frames.count == 0) {
|
|
|
|
// Animation has not begun or animation has already finished.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-28 16:08:51 +00:00
|
|
|
if (_animationStartTime == -1) {
|
|
|
|
_animationStartTime = _animationCurrentTime = currentTime;
|
|
|
|
}
|
|
|
|
|
2016-06-09 17:34:41 +00:00
|
|
|
_animationCurrentTime = currentTime;
|
|
|
|
NSTimeInterval currentDuration = _animationCurrentTime - _animationStartTime;
|
|
|
|
|
|
|
|
// Determine how many frames have passed since last update.
|
|
|
|
// Get index of frames that surround the current interval
|
2017-04-24 11:27:37 +00:00
|
|
|
NSUInteger startIndex = floor(currentDuration / RCTSingleFrameInterval);
|
2016-06-09 17:34:41 +00:00
|
|
|
NSUInteger nextIndex = startIndex + 1;
|
|
|
|
|
|
|
|
if (nextIndex >= _frames.count) {
|
2017-05-26 10:24:29 +00:00
|
|
|
if (_iterations == -1 || _currentLoop < _iterations) {
|
|
|
|
// Looping, reset to the first frame value.
|
|
|
|
_animationStartTime = currentTime;
|
|
|
|
_currentLoop++;
|
|
|
|
NSNumber *firstValue = _frames.firstObject;
|
|
|
|
[self updateOutputWithFrameOutput:firstValue.doubleValue];
|
|
|
|
} else {
|
|
|
|
_animationHasFinished = YES;
|
|
|
|
// We are at the end of the animation
|
|
|
|
// Update value and flag animation has ended.
|
|
|
|
NSNumber *finalValue = _frames.lastObject;
|
|
|
|
[self updateOutputWithFrameOutput:finalValue.doubleValue];
|
|
|
|
}
|
2016-06-09 17:34:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-13 06:03:51 +00:00
|
|
|
// Do a linear remap of the two frames to safeguard against variable framerates
|
2016-06-09 17:34:41 +00:00
|
|
|
NSNumber *fromFrameValue = _frames[startIndex];
|
|
|
|
NSNumber *toFrameValue = _frames[nextIndex];
|
2017-04-24 11:27:37 +00:00
|
|
|
NSTimeInterval fromInterval = startIndex * RCTSingleFrameInterval;
|
|
|
|
NSTimeInterval toInterval = nextIndex * RCTSingleFrameInterval;
|
2016-06-09 17:34:41 +00:00
|
|
|
|
|
|
|
// Interpolate between the individual frames to ensure the animations are
|
|
|
|
//smooth and of the proper duration regardless of the framerate.
|
|
|
|
CGFloat frameOutput = RCTInterpolateValue(currentDuration,
|
|
|
|
fromInterval,
|
|
|
|
toInterval,
|
|
|
|
fromFrameValue.doubleValue,
|
2016-10-20 21:12:37 +00:00
|
|
|
toFrameValue.doubleValue,
|
|
|
|
EXTRAPOLATE_TYPE_EXTEND,
|
|
|
|
EXTRAPOLATE_TYPE_EXTEND);
|
2016-06-09 17:34:41 +00:00
|
|
|
|
|
|
|
[self updateOutputWithFrameOutput:frameOutput];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updateOutputWithFrameOutput:(CGFloat)frameOutput
|
|
|
|
{
|
2016-10-20 21:12:37 +00:00
|
|
|
CGFloat outputValue = RCTInterpolateValue(frameOutput,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
_fromValue,
|
|
|
|
_toValue,
|
|
|
|
EXTRAPOLATE_TYPE_EXTEND,
|
|
|
|
EXTRAPOLATE_TYPE_EXTEND);
|
|
|
|
|
2018-02-16 19:43:34 +00:00
|
|
|
_lastPosition = outputValue;
|
2016-06-09 17:34:41 +00:00
|
|
|
_valueNode.value = outputValue;
|
|
|
|
[_valueNode setNeedsUpdate];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|