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.
|
|
|
|
*/
|
|
|
|
#import "RCTNativeAnimatedModule.h"
|
|
|
|
|
|
|
|
#import "RCTAdditionAnimatedNode.h"
|
2016-10-21 11:20:01 +00:00
|
|
|
#import "RCTAnimationDriver.h"
|
|
|
|
#import "RCTFrameAnimation.h"
|
|
|
|
#import "RCTSpringAnimation.h"
|
2016-06-09 17:34:41 +00:00
|
|
|
#import "RCTAnimationUtils.h"
|
|
|
|
#import "RCTBridge.h"
|
|
|
|
#import "RCTConvert.h"
|
|
|
|
#import "RCTInterpolationAnimatedNode.h"
|
|
|
|
#import "RCTLog.h"
|
2016-09-06 22:30:17 +00:00
|
|
|
#import "RCTDiffClampAnimatedNode.h"
|
2016-09-26 23:32:27 +00:00
|
|
|
#import "RCTDivisionAnimatedNode.h"
|
2016-08-31 21:38:53 +00:00
|
|
|
#import "RCTModuloAnimatedNode.h"
|
2016-06-09 17:34:41 +00:00
|
|
|
#import "RCTMultiplicationAnimatedNode.h"
|
|
|
|
#import "RCTPropsAnimatedNode.h"
|
|
|
|
#import "RCTStyleAnimatedNode.h"
|
|
|
|
#import "RCTTransformAnimatedNode.h"
|
|
|
|
#import "RCTValueAnimatedNode.h"
|
|
|
|
|
|
|
|
@implementation RCTNativeAnimatedModule
|
|
|
|
{
|
|
|
|
NSMutableDictionary<NSNumber *, RCTAnimatedNode *> *_animationNodes;
|
2016-10-21 11:20:01 +00:00
|
|
|
NSMutableDictionary<NSNumber *, id<RCTAnimationDriver>> *_animationDrivers;
|
|
|
|
NSMutableSet<id<RCTAnimationDriver>> *_activeAnimations;
|
|
|
|
NSMutableSet<id<RCTAnimationDriver>> *_finishedAnimations;
|
2016-06-09 17:34:41 +00:00
|
|
|
NSMutableSet<RCTValueAnimatedNode *> *_updatedValueNodes;
|
|
|
|
NSMutableSet<RCTPropsAnimatedNode *> *_propAnimationNodes;
|
|
|
|
CADisplayLink *_displayLink;
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
|
|
|
- (void)setBridge:(RCTBridge *)bridge
|
|
|
|
{
|
2016-08-12 01:10:16 +00:00
|
|
|
[super setBridge:bridge];
|
|
|
|
|
2016-06-09 17:34:41 +00:00
|
|
|
_animationNodes = [NSMutableDictionary new];
|
|
|
|
_animationDrivers = [NSMutableDictionary new];
|
|
|
|
_activeAnimations = [NSMutableSet new];
|
|
|
|
_finishedAnimations = [NSMutableSet new];
|
|
|
|
_updatedValueNodes = [NSMutableSet new];
|
|
|
|
_propAnimationNodes = [NSMutableSet new];
|
|
|
|
}
|
|
|
|
|
2016-08-12 01:10:16 +00:00
|
|
|
|
2016-08-07 19:44:28 +00:00
|
|
|
- (dispatch_queue_t)methodQueue
|
|
|
|
{
|
|
|
|
return dispatch_get_main_queue();
|
|
|
|
}
|
|
|
|
|
2016-08-12 01:10:16 +00:00
|
|
|
- (NSArray<NSString *> *)supportedEvents
|
|
|
|
{
|
|
|
|
return @[@"onAnimatedValueUpdate"];
|
|
|
|
}
|
|
|
|
|
2016-06-09 17:34:41 +00:00
|
|
|
RCT_EXPORT_METHOD(createAnimatedNode:(nonnull NSNumber *)tag
|
|
|
|
config:(NSDictionary<NSString *, id> *)config)
|
|
|
|
{
|
|
|
|
static NSDictionary *map;
|
|
|
|
static dispatch_once_t mapToken;
|
|
|
|
dispatch_once(&mapToken, ^{
|
|
|
|
map = @{@"style" : [RCTStyleAnimatedNode class],
|
|
|
|
@"value" : [RCTValueAnimatedNode class],
|
|
|
|
@"props" : [RCTPropsAnimatedNode class],
|
|
|
|
@"interpolation" : [RCTInterpolationAnimatedNode class],
|
|
|
|
@"addition" : [RCTAdditionAnimatedNode class],
|
2016-09-06 22:30:17 +00:00
|
|
|
@"diffclamp": [RCTDiffClampAnimatedNode class],
|
2016-09-26 23:32:27 +00:00
|
|
|
@"division" : [RCTDivisionAnimatedNode class],
|
2016-06-09 17:34:41 +00:00
|
|
|
@"multiplication" : [RCTMultiplicationAnimatedNode class],
|
2016-08-31 21:38:53 +00:00
|
|
|
@"modulus" : [RCTModuloAnimatedNode class],
|
2016-06-09 17:34:41 +00:00
|
|
|
@"transform" : [RCTTransformAnimatedNode class]};
|
|
|
|
});
|
|
|
|
|
|
|
|
NSString *nodeType = [RCTConvert NSString:config[@"type"]];
|
|
|
|
|
|
|
|
Class nodeClass = map[nodeType];
|
|
|
|
if (!nodeClass) {
|
|
|
|
RCTLogError(@"Animated node type %@ not supported natively", nodeType);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RCTAnimatedNode *node = [[nodeClass alloc] initWithTag:tag config:config];
|
2016-10-21 11:20:01 +00:00
|
|
|
|
2016-06-09 17:34:41 +00:00
|
|
|
_animationNodes[tag] = node;
|
|
|
|
|
|
|
|
if ([node isKindOfClass:[RCTPropsAnimatedNode class]]) {
|
|
|
|
[_propAnimationNodes addObject:(RCTPropsAnimatedNode *)node];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(connectAnimatedNodes:(nonnull NSNumber *)parentTag
|
|
|
|
childTag:(nonnull NSNumber *)childTag)
|
|
|
|
{
|
|
|
|
RCTAssertParam(parentTag);
|
|
|
|
RCTAssertParam(childTag);
|
|
|
|
|
|
|
|
RCTAnimatedNode *parentNode = _animationNodes[parentTag];
|
|
|
|
RCTAnimatedNode *childNode = _animationNodes[childTag];
|
|
|
|
|
|
|
|
RCTAssertParam(parentNode);
|
|
|
|
RCTAssertParam(childNode);
|
|
|
|
|
|
|
|
[parentNode addChild:childNode];
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(disconnectAnimatedNodes:(nonnull NSNumber *)parentTag
|
|
|
|
childTag:(nonnull NSNumber *)childTag)
|
|
|
|
{
|
|
|
|
RCTAssertParam(parentTag);
|
|
|
|
RCTAssertParam(childTag);
|
|
|
|
|
|
|
|
RCTAnimatedNode *parentNode = _animationNodes[parentTag];
|
|
|
|
RCTAnimatedNode *childNode = _animationNodes[childTag];
|
|
|
|
|
|
|
|
RCTAssertParam(parentNode);
|
|
|
|
RCTAssertParam(childNode);
|
|
|
|
|
|
|
|
[parentNode removeChild:childNode];
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(startAnimatingNode:(nonnull NSNumber *)animationId
|
|
|
|
nodeTag:(nonnull NSNumber *)nodeTag
|
|
|
|
config:(NSDictionary<NSString *, id> *)config
|
|
|
|
endCallback:(RCTResponseSenderBlock)callBack)
|
|
|
|
{
|
2016-10-21 11:20:01 +00:00
|
|
|
RCTValueAnimatedNode *valueNode = (RCTValueAnimatedNode *)_animationNodes[nodeTag];
|
|
|
|
|
|
|
|
NSString *type = config[@"type"];
|
|
|
|
id<RCTAnimationDriver>animationDriver;
|
|
|
|
|
|
|
|
if ([type isEqual:@"frames"]) {
|
|
|
|
animationDriver = [[RCTFrameAnimation alloc] initWithId:animationId
|
|
|
|
config:config
|
|
|
|
forNode:valueNode
|
|
|
|
callBack:callBack];
|
|
|
|
|
|
|
|
} else if ([type isEqual:@"spring"]) {
|
|
|
|
animationDriver = [[RCTSpringAnimation alloc] initWithId:animationId
|
|
|
|
config:config
|
|
|
|
forNode:valueNode
|
|
|
|
callBack:callBack];
|
|
|
|
|
|
|
|
} else {
|
2016-06-09 17:34:41 +00:00
|
|
|
RCTLogError(@"Unsupported animation type: %@", config[@"type"]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
[_activeAnimations addObject:animationDriver];
|
|
|
|
_animationDrivers[animationId] = animationDriver;
|
|
|
|
[animationDriver startAnimation];
|
|
|
|
[self startAnimation];
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(stopAnimation:(nonnull NSNumber *)animationId)
|
|
|
|
{
|
2016-10-21 11:20:01 +00:00
|
|
|
id<RCTAnimationDriver>driver = _animationDrivers[animationId];
|
2016-06-09 17:34:41 +00:00
|
|
|
if (driver) {
|
|
|
|
[driver removeAnimation];
|
|
|
|
[_animationDrivers removeObjectForKey:animationId];
|
|
|
|
[_activeAnimations removeObject:driver];
|
|
|
|
[_finishedAnimations removeObject:driver];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(setAnimatedNodeValue:(nonnull NSNumber *)nodeTag
|
|
|
|
value:(nonnull NSNumber *)value)
|
|
|
|
{
|
|
|
|
RCTAnimatedNode *node = _animationNodes[nodeTag];
|
|
|
|
if (![node isKindOfClass:[RCTValueAnimatedNode class]]) {
|
|
|
|
RCTLogError(@"Not a value node.");
|
|
|
|
return;
|
|
|
|
}
|
2016-10-21 11:20:01 +00:00
|
|
|
|
2016-06-09 17:34:41 +00:00
|
|
|
RCTValueAnimatedNode *valueNode = (RCTValueAnimatedNode *)node;
|
|
|
|
valueNode.value = value.floatValue;
|
|
|
|
[valueNode setNeedsUpdate];
|
2016-10-31 21:21:58 +00:00
|
|
|
|
|
|
|
[self updateViewsProps];
|
|
|
|
|
|
|
|
[valueNode cleanupAnimationUpdate];
|
2016-06-09 17:34:41 +00:00
|
|
|
}
|
|
|
|
|
2016-09-26 17:17:29 +00:00
|
|
|
RCT_EXPORT_METHOD(setAnimatedNodeOffset:(nonnull NSNumber *)nodeTag
|
|
|
|
offset:(nonnull NSNumber *)offset)
|
|
|
|
{
|
|
|
|
RCTAnimatedNode *node = _animationNodes[nodeTag];
|
|
|
|
if (![node isKindOfClass:[RCTValueAnimatedNode class]]) {
|
|
|
|
RCTLogError(@"Not a value node.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RCTValueAnimatedNode *valueNode = (RCTValueAnimatedNode *)node;
|
|
|
|
[valueNode setOffset:offset.floatValue];
|
|
|
|
[_updatedValueNodes addObject:valueNode];
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(flattenAnimatedNodeOffset:(nonnull NSNumber *)nodeTag)
|
|
|
|
{
|
|
|
|
RCTAnimatedNode *node = _animationNodes[nodeTag];
|
|
|
|
if (![node isKindOfClass:[RCTValueAnimatedNode class]]) {
|
|
|
|
RCTLogError(@"Not a value node.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RCTValueAnimatedNode *valueNode = (RCTValueAnimatedNode *)node;
|
|
|
|
[valueNode flattenOffset];
|
|
|
|
}
|
|
|
|
|
2016-06-09 17:34:41 +00:00
|
|
|
RCT_EXPORT_METHOD(connectAnimatedNodeToView:(nonnull NSNumber *)nodeTag
|
|
|
|
viewTag:(nonnull NSNumber *)viewTag)
|
|
|
|
{
|
|
|
|
RCTAnimatedNode *node = _animationNodes[nodeTag];
|
|
|
|
if (viewTag && [node isKindOfClass:[RCTPropsAnimatedNode class]]) {
|
|
|
|
[(RCTPropsAnimatedNode *)node connectToView:viewTag animatedModule:self];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(disconnectAnimatedNodeFromView:(nonnull NSNumber *)nodeTag
|
|
|
|
viewTag:(nonnull NSNumber *)viewTag)
|
|
|
|
{
|
|
|
|
RCTAnimatedNode *node = _animationNodes[nodeTag];
|
|
|
|
if (viewTag && node && [node isKindOfClass:[RCTPropsAnimatedNode class]]) {
|
|
|
|
[(RCTPropsAnimatedNode *)node disconnectFromView:viewTag];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(dropAnimatedNode:(nonnull NSNumber *)tag)
|
|
|
|
{
|
|
|
|
RCTAnimatedNode *node = _animationNodes[tag];
|
|
|
|
if (node) {
|
|
|
|
[node detachNode];
|
|
|
|
[_animationNodes removeObjectForKey:tag];
|
|
|
|
if ([node isKindOfClass:[RCTValueAnimatedNode class]]) {
|
|
|
|
[_updatedValueNodes removeObject:(RCTValueAnimatedNode *)node];
|
|
|
|
} else if ([node isKindOfClass:[RCTPropsAnimatedNode class]]) {
|
|
|
|
[_propAnimationNodes removeObject:(RCTPropsAnimatedNode *)node];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-12 01:10:16 +00:00
|
|
|
RCT_EXPORT_METHOD(startListeningToAnimatedNodeValue:(nonnull NSNumber *)tag)
|
|
|
|
{
|
|
|
|
RCTAnimatedNode *node = _animationNodes[tag];
|
|
|
|
if (node && [node isKindOfClass:[RCTValueAnimatedNode class]]) {
|
|
|
|
((RCTValueAnimatedNode *)node).valueObserver = self;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(stopListeningToAnimatedNodeValue:(nonnull NSNumber *)tag)
|
|
|
|
{
|
|
|
|
RCTAnimatedNode *node = _animationNodes[tag];
|
|
|
|
if (node && [node isKindOfClass:[RCTValueAnimatedNode class]]) {
|
|
|
|
((RCTValueAnimatedNode *)node).valueObserver = nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)animatedNode:(RCTValueAnimatedNode *)node didUpdateValue:(CGFloat)value
|
|
|
|
{
|
|
|
|
[self sendEventWithName:@"onAnimatedValueUpdate"
|
|
|
|
body:@{@"tag": node.nodeTag, @"value": @(value)}];
|
|
|
|
}
|
|
|
|
|
2016-10-31 21:21:58 +00:00
|
|
|
- (void)updateViewsProps
|
|
|
|
{
|
|
|
|
for (RCTPropsAnimatedNode *propsNode in _propAnimationNodes) {
|
|
|
|
[propsNode updateNodeIfNecessary];
|
|
|
|
}
|
|
|
|
}
|
2016-08-12 01:10:16 +00:00
|
|
|
|
2016-06-09 17:34:41 +00:00
|
|
|
#pragma mark -- Animation Loop
|
|
|
|
|
|
|
|
- (void)startAnimation
|
|
|
|
{
|
|
|
|
if (!_displayLink && _activeAnimations.count > 0) {
|
|
|
|
_displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateAnimations)];
|
|
|
|
[_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updateAnimations
|
|
|
|
{
|
|
|
|
// Step Current active animations
|
|
|
|
// This also recursively marks children nodes as needing update
|
2016-10-21 11:20:01 +00:00
|
|
|
for (id<RCTAnimationDriver>animationDriver in _activeAnimations) {
|
2016-06-09 17:34:41 +00:00
|
|
|
[animationDriver stepAnimation];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform node updates for marked nodes.
|
|
|
|
// At this point all nodes that are in need of an update are properly marked as such.
|
|
|
|
for (RCTPropsAnimatedNode *propsNode in _propAnimationNodes) {
|
|
|
|
[propsNode updateNodeIfNecessary];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup nodes and prepare for next cycle. Remove updated nodes from bucket.
|
2016-10-21 11:20:01 +00:00
|
|
|
for (id<RCTAnimationDriver>driverNode in _activeAnimations) {
|
2016-06-09 17:34:41 +00:00
|
|
|
[driverNode cleanupAnimationUpdate];
|
|
|
|
}
|
|
|
|
for (RCTValueAnimatedNode *valueNode in _updatedValueNodes) {
|
|
|
|
[valueNode cleanupAnimationUpdate];
|
|
|
|
}
|
|
|
|
[_updatedValueNodes removeAllObjects];
|
|
|
|
|
2016-10-21 11:20:01 +00:00
|
|
|
for (id<RCTAnimationDriver>driverNode in _activeAnimations) {
|
2016-06-09 17:34:41 +00:00
|
|
|
if (driverNode.animationHasFinished) {
|
|
|
|
[driverNode removeAnimation];
|
|
|
|
[_finishedAnimations addObject:driverNode];
|
|
|
|
}
|
|
|
|
}
|
2016-10-21 11:20:01 +00:00
|
|
|
for (id<RCTAnimationDriver>driverNode in _finishedAnimations) {
|
2016-06-09 17:34:41 +00:00
|
|
|
[_activeAnimations removeObject:driverNode];
|
|
|
|
[_animationDrivers removeObjectForKey:driverNode.animationId];
|
|
|
|
}
|
|
|
|
[_finishedAnimations removeAllObjects];
|
|
|
|
|
|
|
|
if (_activeAnimations.count == 0) {
|
|
|
|
[_displayLink invalidate];
|
|
|
|
_displayLink = nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|