2016-06-09 17:34:41 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2016-10-21 11:20:01 +00:00
|
|
|
#import "RCTFrameAnimation.h"
|
2016-06-09 17:34:41 +00:00
|
|
|
|
|
|
|
#import <UIKit/UIKit.h>
|
|
|
|
|
2016-10-21 11:20:01 +00:00
|
|
|
#import "RCTConvert.h"
|
2016-06-09 17:34:41 +00:00
|
|
|
#import "RCTAnimationUtils.h"
|
|
|
|
#import "RCTDefines.h"
|
|
|
|
#import "RCTValueAnimatedNode.h"
|
|
|
|
|
|
|
|
const double SINGLE_FRAME_INTERVAL = 1.0 / 60.0;
|
|
|
|
|
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;
|
|
|
|
NSTimeInterval _delay;
|
|
|
|
NSTimeInterval _animationStartTime;
|
|
|
|
NSTimeInterval _animationCurrentTime;
|
|
|
|
RCTResponseSenderBlock _callback;
|
|
|
|
}
|
|
|
|
|
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])) {
|
2016-10-21 11:20:01 +00:00
|
|
|
NSNumber *toValue = [RCTConvert NSNumber:config[@"toValue"]] ?: @1;
|
|
|
|
NSTimeInterval delay = [RCTConvert double:config[@"delay"]];
|
|
|
|
NSArray<NSNumber *> *frames = [RCTConvert NSNumberArray:config[@"frames"]];
|
|
|
|
|
2016-06-09 17:34:41 +00:00
|
|
|
_animationId = animationId;
|
2016-10-21 11:20:01 +00:00
|
|
|
_toValue = toValue.floatValue;
|
2016-06-09 17:34:41 +00:00
|
|
|
_fromValue = valueNode.value;
|
|
|
|
_valueNode = valueNode;
|
|
|
|
_delay = delay;
|
|
|
|
_frames = [frames copy];
|
|
|
|
_callback = [callback copy];
|
|
|
|
}
|
|
|
|
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) {
|
2016-07-05 17:58:17 +00:00
|
|
|
_callback(@[@{
|
|
|
|
@"finished": @(_animationHasFinished)
|
|
|
|
}]);
|
2016-06-09 17:34:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)stepAnimation
|
|
|
|
{
|
|
|
|
if (!_animationHasBegun || _animationHasFinished || _frames.count == 0) {
|
|
|
|
// Animation has not begun or animation has already finished.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSTimeInterval currentTime = CACurrentMediaTime();
|
|
|
|
NSTimeInterval stepInterval = currentTime - _animationCurrentTime;
|
|
|
|
_animationCurrentTime = currentTime;
|
|
|
|
NSTimeInterval currentDuration = _animationCurrentTime - _animationStartTime;
|
|
|
|
|
|
|
|
if (_delay > 0) {
|
|
|
|
// Decrement delay
|
|
|
|
_delay -= stepInterval;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine how many frames have passed since last update.
|
|
|
|
// Get index of frames that surround the current interval
|
|
|
|
NSUInteger startIndex = floor(currentDuration / SINGLE_FRAME_INTERVAL);
|
|
|
|
NSUInteger nextIndex = startIndex + 1;
|
|
|
|
|
|
|
|
if (nextIndex >= _frames.count) {
|
|
|
|
// We are at the end of the animation
|
|
|
|
// Update value and flag animation has ended.
|
|
|
|
NSNumber *finalValue = _frames.lastObject;
|
|
|
|
[self updateOutputWithFrameOutput:finalValue.doubleValue];
|
|
|
|
[self stopAnimation];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do a linear remap of the two frames to safegaurd against variable framerates
|
|
|
|
NSNumber *fromFrameValue = _frames[startIndex];
|
|
|
|
NSNumber *toFrameValue = _frames[nextIndex];
|
|
|
|
NSTimeInterval fromInterval = startIndex * SINGLE_FRAME_INTERVAL;
|
|
|
|
NSTimeInterval toInterval = nextIndex * SINGLE_FRAME_INTERVAL;
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2016-06-09 17:34:41 +00:00
|
|
|
_valueNode.value = outputValue;
|
|
|
|
[_valueNode setNeedsUpdate];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)cleanupAnimationUpdate
|
|
|
|
{
|
|
|
|
[_valueNode cleanupAnimationUpdate];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|