mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-23 11:57:46 +08:00
Native Animated - Support Animated.loop on iOS
Summary: Follow up to #11973 to add support to Animated.loop with useNativeDriver on iOS. **Test plan** Test with new UIExplorer example Run unit tests Closes https://github.com/facebook/react-native/pull/13359 Differential Revision: D4960754 Pulled By: javache fbshipit-source-id: caa840281f1b060df7a2b1c50405fcae1e1b0de6
This commit is contained in:
committed by
Facebook Github Bot
parent
32d35c31f7
commit
11424a8bc6
@@ -34,6 +34,8 @@
|
||||
NSTimeInterval _animationStartTime;
|
||||
NSTimeInterval _animationCurrentTime;
|
||||
RCTResponseSenderBlock _callback;
|
||||
NSInteger _iterations;
|
||||
NSInteger _currentLoop;
|
||||
}
|
||||
|
||||
- (instancetype)initWithId:(NSNumber *)animationId
|
||||
@@ -44,6 +46,7 @@
|
||||
if ((self = [super init])) {
|
||||
NSNumber *toValue = [RCTConvert NSNumber:config[@"toValue"]] ?: @1;
|
||||
NSArray<NSNumber *> *frames = [RCTConvert NSNumberArray:config[@"frames"]];
|
||||
NSNumber *iterations = [RCTConvert NSNumber:config[@"iterations"]] ?: @1;
|
||||
|
||||
_animationId = animationId;
|
||||
_toValue = toValue.floatValue;
|
||||
@@ -51,6 +54,9 @@
|
||||
_valueNode = valueNode;
|
||||
_frames = [frames copy];
|
||||
_callback = [callback copy];
|
||||
_animationHasFinished = iterations.integerValue == 0;
|
||||
_iterations = iterations.integerValue;
|
||||
_currentLoop = 1;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
@@ -93,11 +99,19 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
||||
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];
|
||||
_animationHasFinished = YES;
|
||||
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];
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -41,6 +41,9 @@
|
||||
|
||||
CGFloat _lastPosition;
|
||||
CGFloat _lastVelocity;
|
||||
|
||||
NSInteger _iterations;
|
||||
NSInteger _currentLoop;
|
||||
}
|
||||
|
||||
- (instancetype)initWithId:(NSNumber *)animationId
|
||||
@@ -49,6 +52,8 @@
|
||||
callBack:(nullable RCTResponseSenderBlock)callback
|
||||
{
|
||||
if ((self = [super init])) {
|
||||
NSNumber *iterations = [RCTConvert NSNumber:config[@"iterations"]] ?: @1;
|
||||
|
||||
_animationId = animationId;
|
||||
_toValue = [RCTConvert CGFloat:config[@"toValue"]];
|
||||
_fromValue = valueNode.value;
|
||||
@@ -63,6 +68,10 @@
|
||||
|
||||
_lastPosition = _fromValue;
|
||||
_lastVelocity = _initialVelocity;
|
||||
|
||||
_animationHasFinished = iterations.integerValue == 0;
|
||||
_iterations = iterations.integerValue;
|
||||
_currentLoop = 1;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
@@ -178,7 +187,14 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
||||
[self onUpdate:_toValue];
|
||||
}
|
||||
|
||||
_animationHasFinished = YES;
|
||||
if (_iterations == -1 || _currentLoop < _iterations) {
|
||||
_lastPosition = _fromValue;
|
||||
_lastVelocity = _initialVelocity;
|
||||
_currentLoop++;
|
||||
[self onUpdate:_fromValue];
|
||||
} else {
|
||||
_animationHasFinished = YES;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user