RCTNativeAnimatedNodesManager.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import <React/RCTNativeAnimatedNodesManager.h>
  8. #import <React/RCTConvert.h>
  9. #import <React/RCTAdditionAnimatedNode.h>
  10. #import <React/RCTAnimatedNode.h>
  11. #import <React/RCTAnimationDriver.h>
  12. #import <React/RCTDiffClampAnimatedNode.h>
  13. #import <React/RCTDivisionAnimatedNode.h>
  14. #import <React/RCTEventAnimation.h>
  15. #import <React/RCTFrameAnimation.h>
  16. #import <React/RCTDecayAnimation.h>
  17. #import <React/RCTInterpolationAnimatedNode.h>
  18. #import <React/RCTModuloAnimatedNode.h>
  19. #import <React/RCTMultiplicationAnimatedNode.h>
  20. #import <React/RCTPropsAnimatedNode.h>
  21. #import <React/RCTSpringAnimation.h>
  22. #import <React/RCTStyleAnimatedNode.h>
  23. #import <React/RCTSubtractionAnimatedNode.h>
  24. #import <React/RCTTransformAnimatedNode.h>
  25. #import <React/RCTValueAnimatedNode.h>
  26. #import <React/RCTTrackingAnimatedNode.h>
  27. // We do some normalizing of the event names in RCTEventDispatcher#RCTNormalizeInputEventName.
  28. // To make things simpler just get rid of the parts we change in the event names we use here.
  29. // This is a lot easier than trying to denormalize because there would be multiple possible
  30. // denormalized forms for a single input.
  31. static NSString *RCTNormalizeAnimatedEventName(NSString *eventName)
  32. {
  33. if ([eventName hasPrefix:@"on"]) {
  34. return [eventName substringFromIndex:2];
  35. }
  36. if ([eventName hasPrefix:@"top"]) {
  37. return [eventName substringFromIndex:3];
  38. }
  39. return eventName;
  40. }
  41. @implementation RCTNativeAnimatedNodesManager
  42. {
  43. __weak RCTBridge *_bridge;
  44. NSMutableDictionary<NSNumber *, RCTAnimatedNode *> *_animationNodes;
  45. // Mapping of a view tag and an event name to a list of event animation drivers. 99% of the time
  46. // there will be only one driver per mapping so all code code should be optimized around that.
  47. NSMutableDictionary<NSString *, NSMutableArray<RCTEventAnimation *> *> *_eventDrivers;
  48. NSMutableSet<id<RCTAnimationDriver>> *_activeAnimations;
  49. CADisplayLink *_displayLink;
  50. }
  51. - (instancetype)initWithBridge:(nonnull RCTBridge *)bridge
  52. {
  53. if ((self = [super init])) {
  54. _bridge = bridge;
  55. _animationNodes = [NSMutableDictionary new];
  56. _eventDrivers = [NSMutableDictionary new];
  57. _activeAnimations = [NSMutableSet new];
  58. }
  59. return self;
  60. }
  61. - (BOOL)isNodeManagedByFabric:(nonnull NSNumber *)tag
  62. {
  63. RCTAnimatedNode *node = _animationNodes[tag];
  64. if (node) {
  65. return [node isManagedByFabric];
  66. }
  67. return false;
  68. }
  69. #pragma mark -- Graph
  70. - (void)createAnimatedNode:(nonnull NSNumber *)tag
  71. config:(NSDictionary<NSString *, id> *)config
  72. {
  73. static NSDictionary *map;
  74. static dispatch_once_t mapToken;
  75. dispatch_once(&mapToken, ^{
  76. map = @{@"style" : [RCTStyleAnimatedNode class],
  77. @"value" : [RCTValueAnimatedNode class],
  78. @"props" : [RCTPropsAnimatedNode class],
  79. @"interpolation" : [RCTInterpolationAnimatedNode class],
  80. @"addition" : [RCTAdditionAnimatedNode class],
  81. @"diffclamp": [RCTDiffClampAnimatedNode class],
  82. @"division" : [RCTDivisionAnimatedNode class],
  83. @"multiplication" : [RCTMultiplicationAnimatedNode class],
  84. @"modulus" : [RCTModuloAnimatedNode class],
  85. @"subtraction" : [RCTSubtractionAnimatedNode class],
  86. @"transform" : [RCTTransformAnimatedNode class],
  87. @"tracking" : [RCTTrackingAnimatedNode class]};
  88. });
  89. NSString *nodeType = [RCTConvert NSString:config[@"type"]];
  90. Class nodeClass = map[nodeType];
  91. if (!nodeClass) {
  92. RCTLogError(@"Animated node type %@ not supported natively", nodeType);
  93. return;
  94. }
  95. RCTAnimatedNode *node = [[nodeClass alloc] initWithTag:tag config:config];
  96. node.manager = self;
  97. _animationNodes[tag] = node;
  98. [node setNeedsUpdate];
  99. }
  100. - (void)connectAnimatedNodes:(nonnull NSNumber *)parentTag
  101. childTag:(nonnull NSNumber *)childTag
  102. {
  103. RCTAssertParam(parentTag);
  104. RCTAssertParam(childTag);
  105. RCTAnimatedNode *parentNode = _animationNodes[parentTag];
  106. RCTAnimatedNode *childNode = _animationNodes[childTag];
  107. RCTAssertParam(parentNode);
  108. RCTAssertParam(childNode);
  109. [parentNode addChild:childNode];
  110. [childNode setNeedsUpdate];
  111. }
  112. - (void)disconnectAnimatedNodes:(nonnull NSNumber *)parentTag
  113. childTag:(nonnull NSNumber *)childTag
  114. {
  115. RCTAssertParam(parentTag);
  116. RCTAssertParam(childTag);
  117. RCTAnimatedNode *parentNode = _animationNodes[parentTag];
  118. RCTAnimatedNode *childNode = _animationNodes[childTag];
  119. RCTAssertParam(parentNode);
  120. RCTAssertParam(childNode);
  121. [parentNode removeChild:childNode];
  122. [childNode setNeedsUpdate];
  123. }
  124. - (void)connectAnimatedNodeToView:(nonnull NSNumber *)nodeTag
  125. viewTag:(nonnull NSNumber *)viewTag
  126. viewName:(nonnull NSString *)viewName
  127. {
  128. RCTAnimatedNode *node = _animationNodes[nodeTag];
  129. if ([node isKindOfClass:[RCTPropsAnimatedNode class]]) {
  130. [(RCTPropsAnimatedNode *)node connectToView:viewTag viewName:viewName bridge:_bridge];
  131. }
  132. [node setNeedsUpdate];
  133. }
  134. - (void)disconnectAnimatedNodeFromView:(nonnull NSNumber *)nodeTag
  135. viewTag:(nonnull NSNumber *)viewTag
  136. {
  137. RCTAnimatedNode *node = _animationNodes[nodeTag];
  138. if ([node isKindOfClass:[RCTPropsAnimatedNode class]]) {
  139. [(RCTPropsAnimatedNode *)node disconnectFromView:viewTag];
  140. }
  141. }
  142. - (void)restoreDefaultValues:(nonnull NSNumber *)nodeTag
  143. {
  144. RCTAnimatedNode *node = _animationNodes[nodeTag];
  145. // Restoring default values needs to happen before UIManager operations so it is
  146. // possible the node hasn't been created yet if it is being connected and
  147. // disconnected in the same batch. In that case we don't need to restore
  148. // default values since it will never actually update the view.
  149. if (node == nil) {
  150. return;
  151. }
  152. if (![node isKindOfClass:[RCTPropsAnimatedNode class]]) {
  153. RCTLogError(@"Not a props node.");
  154. }
  155. [(RCTPropsAnimatedNode *)node restoreDefaultValues];
  156. }
  157. - (void)dropAnimatedNode:(nonnull NSNumber *)tag
  158. {
  159. RCTAnimatedNode *node = _animationNodes[tag];
  160. if (node) {
  161. [node detachNode];
  162. [_animationNodes removeObjectForKey:tag];
  163. }
  164. }
  165. #pragma mark -- Mutations
  166. - (void)setAnimatedNodeValue:(nonnull NSNumber *)nodeTag
  167. value:(nonnull NSNumber *)value
  168. {
  169. RCTAnimatedNode *node = _animationNodes[nodeTag];
  170. if (![node isKindOfClass:[RCTValueAnimatedNode class]]) {
  171. RCTLogError(@"Not a value node.");
  172. return;
  173. }
  174. [self stopAnimationsForNode:node];
  175. RCTValueAnimatedNode *valueNode = (RCTValueAnimatedNode *)node;
  176. valueNode.value = value.floatValue;
  177. [valueNode setNeedsUpdate];
  178. }
  179. - (void)setAnimatedNodeOffset:(nonnull NSNumber *)nodeTag
  180. offset:(nonnull NSNumber *)offset
  181. {
  182. RCTAnimatedNode *node = _animationNodes[nodeTag];
  183. if (![node isKindOfClass:[RCTValueAnimatedNode class]]) {
  184. RCTLogError(@"Not a value node.");
  185. return;
  186. }
  187. RCTValueAnimatedNode *valueNode = (RCTValueAnimatedNode *)node;
  188. [valueNode setOffset:offset.floatValue];
  189. [valueNode setNeedsUpdate];
  190. }
  191. - (void)flattenAnimatedNodeOffset:(nonnull NSNumber *)nodeTag
  192. {
  193. RCTAnimatedNode *node = _animationNodes[nodeTag];
  194. if (![node isKindOfClass:[RCTValueAnimatedNode class]]) {
  195. RCTLogError(@"Not a value node.");
  196. return;
  197. }
  198. RCTValueAnimatedNode *valueNode = (RCTValueAnimatedNode *)node;
  199. [valueNode flattenOffset];
  200. }
  201. - (void)extractAnimatedNodeOffset:(nonnull NSNumber *)nodeTag
  202. {
  203. RCTAnimatedNode *node = _animationNodes[nodeTag];
  204. if (![node isKindOfClass:[RCTValueAnimatedNode class]]) {
  205. RCTLogError(@"Not a value node.");
  206. return;
  207. }
  208. RCTValueAnimatedNode *valueNode = (RCTValueAnimatedNode *)node;
  209. [valueNode extractOffset];
  210. }
  211. #pragma mark -- Drivers
  212. - (void)startAnimatingNode:(nonnull NSNumber *)animationId
  213. nodeTag:(nonnull NSNumber *)nodeTag
  214. config:(NSDictionary<NSString *, id> *)config
  215. endCallback:(RCTResponseSenderBlock)callBack
  216. {
  217. // check if the animation has already started
  218. for (id<RCTAnimationDriver> driver in _activeAnimations) {
  219. if ([driver.animationId isEqual:animationId]) {
  220. // if the animation is running, we restart it with an updated configuration
  221. [driver resetAnimationConfig:config];
  222. return;
  223. }
  224. }
  225. RCTValueAnimatedNode *valueNode = (RCTValueAnimatedNode *)_animationNodes[nodeTag];
  226. NSString *type = config[@"type"];
  227. id<RCTAnimationDriver> animationDriver;
  228. if ([type isEqual:@"frames"]) {
  229. animationDriver = [[RCTFrameAnimation alloc] initWithId:animationId
  230. config:config
  231. forNode:valueNode
  232. callBack:callBack];
  233. } else if ([type isEqual:@"spring"]) {
  234. animationDriver = [[RCTSpringAnimation alloc] initWithId:animationId
  235. config:config
  236. forNode:valueNode
  237. callBack:callBack];
  238. } else if ([type isEqual:@"decay"]) {
  239. animationDriver = [[RCTDecayAnimation alloc] initWithId:animationId
  240. config:config
  241. forNode:valueNode
  242. callBack:callBack];
  243. } else {
  244. RCTLogError(@"Unsupported animation type: %@", config[@"type"]);
  245. return;
  246. }
  247. [_activeAnimations addObject:animationDriver];
  248. [animationDriver startAnimation];
  249. [self startAnimationLoopIfNeeded];
  250. }
  251. - (void)stopAnimation:(nonnull NSNumber *)animationId
  252. {
  253. for (id<RCTAnimationDriver> driver in _activeAnimations) {
  254. if ([driver.animationId isEqual:animationId]) {
  255. [driver stopAnimation];
  256. [_activeAnimations removeObject:driver];
  257. break;
  258. }
  259. }
  260. }
  261. - (void)stopAnimationsForNode:(nonnull RCTAnimatedNode *)node
  262. {
  263. NSMutableArray<id<RCTAnimationDriver>> *discarded = [NSMutableArray new];
  264. for (id<RCTAnimationDriver> driver in _activeAnimations) {
  265. if ([driver.valueNode isEqual:node]) {
  266. [discarded addObject:driver];
  267. }
  268. }
  269. for (id<RCTAnimationDriver> driver in discarded) {
  270. [driver stopAnimation];
  271. [_activeAnimations removeObject:driver];
  272. }
  273. }
  274. #pragma mark -- Events
  275. - (void)addAnimatedEventToView:(nonnull NSNumber *)viewTag
  276. eventName:(nonnull NSString *)eventName
  277. eventMapping:(NSDictionary<NSString *, id> *)eventMapping
  278. {
  279. NSNumber *nodeTag = [RCTConvert NSNumber:eventMapping[@"animatedValueTag"]];
  280. RCTAnimatedNode *node = _animationNodes[nodeTag];
  281. if (!node) {
  282. RCTLogError(@"Animated node with tag %@ does not exists", nodeTag);
  283. return;
  284. }
  285. if (![node isKindOfClass:[RCTValueAnimatedNode class]]) {
  286. RCTLogError(@"Animated node connected to event should be of type RCTValueAnimatedNode");
  287. return;
  288. }
  289. NSArray<NSString *> *eventPath = [RCTConvert NSStringArray:eventMapping[@"nativeEventPath"]];
  290. RCTEventAnimation *driver =
  291. [[RCTEventAnimation alloc] initWithEventPath:eventPath valueNode:(RCTValueAnimatedNode *)node];
  292. NSString *key = [NSString stringWithFormat:@"%@%@", viewTag, RCTNormalizeAnimatedEventName(eventName)];
  293. if (_eventDrivers[key] != nil) {
  294. [_eventDrivers[key] addObject:driver];
  295. } else {
  296. NSMutableArray<RCTEventAnimation *> *drivers = [NSMutableArray new];
  297. [drivers addObject:driver];
  298. _eventDrivers[key] = drivers;
  299. }
  300. }
  301. - (void)removeAnimatedEventFromView:(nonnull NSNumber *)viewTag
  302. eventName:(nonnull NSString *)eventName
  303. animatedNodeTag:(nonnull NSNumber *)animatedNodeTag
  304. {
  305. NSString *key = [NSString stringWithFormat:@"%@%@", viewTag, RCTNormalizeAnimatedEventName(eventName)];
  306. if (_eventDrivers[key] != nil) {
  307. if (_eventDrivers[key].count == 1) {
  308. [_eventDrivers removeObjectForKey:key];
  309. } else {
  310. NSMutableArray<RCTEventAnimation *> *driversForKey = _eventDrivers[key];
  311. for (NSUInteger i = 0; i < driversForKey.count; i++) {
  312. if (driversForKey[i].valueNode.nodeTag == animatedNodeTag) {
  313. [driversForKey removeObjectAtIndex:i];
  314. break;
  315. }
  316. }
  317. }
  318. }
  319. }
  320. - (void)handleAnimatedEvent:(id<RCTEvent>)event
  321. {
  322. if (_eventDrivers.count == 0) {
  323. return;
  324. }
  325. NSString *key = [NSString stringWithFormat:@"%@%@", event.viewTag, RCTNormalizeAnimatedEventName(event.eventName)];
  326. NSMutableArray<RCTEventAnimation *> *driversForKey = _eventDrivers[key];
  327. if (driversForKey) {
  328. for (RCTEventAnimation *driver in driversForKey) {
  329. [self stopAnimationsForNode:driver.valueNode];
  330. [driver updateWithEvent:event];
  331. }
  332. [self updateAnimations];
  333. }
  334. }
  335. #pragma mark -- Listeners
  336. - (void)startListeningToAnimatedNodeValue:(nonnull NSNumber *)tag
  337. valueObserver:(id<RCTValueAnimatedNodeObserver>)valueObserver
  338. {
  339. RCTAnimatedNode *node = _animationNodes[tag];
  340. if ([node isKindOfClass:[RCTValueAnimatedNode class]]) {
  341. ((RCTValueAnimatedNode *)node).valueObserver = valueObserver;
  342. }
  343. }
  344. - (void)stopListeningToAnimatedNodeValue:(nonnull NSNumber *)tag
  345. {
  346. RCTAnimatedNode *node = _animationNodes[tag];
  347. if ([node isKindOfClass:[RCTValueAnimatedNode class]]) {
  348. ((RCTValueAnimatedNode *)node).valueObserver = nil;
  349. }
  350. }
  351. #pragma mark -- Animation Loop
  352. - (void)startAnimationLoopIfNeeded
  353. {
  354. if (!_displayLink && _activeAnimations.count > 0) {
  355. _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(stepAnimations:)];
  356. [_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
  357. }
  358. }
  359. - (void)stopAnimationLoopIfNeeded
  360. {
  361. if (_activeAnimations.count == 0) {
  362. [self stopAnimationLoop];
  363. }
  364. }
  365. - (void)stopAnimationLoop
  366. {
  367. if (_displayLink) {
  368. [_displayLink invalidate];
  369. _displayLink = nil;
  370. }
  371. }
  372. - (void)stepAnimations:(CADisplayLink *)displaylink
  373. {
  374. NSTimeInterval time = displaylink.timestamp;
  375. for (id<RCTAnimationDriver> animationDriver in _activeAnimations) {
  376. [animationDriver stepAnimationWithTime:time];
  377. }
  378. [self updateAnimations];
  379. for (id<RCTAnimationDriver> animationDriver in [_activeAnimations copy]) {
  380. if (animationDriver.animationHasFinished) {
  381. [animationDriver stopAnimation];
  382. [_activeAnimations removeObject:animationDriver];
  383. }
  384. }
  385. [self stopAnimationLoopIfNeeded];
  386. }
  387. #pragma mark -- Updates
  388. - (void)updateAnimations
  389. {
  390. [_animationNodes enumerateKeysAndObjectsUsingBlock:^(NSNumber *key, RCTAnimatedNode *node, BOOL *stop) {
  391. if (node.needsUpdate) {
  392. [node updateNodeIfNecessary];
  393. }
  394. }];
  395. }
  396. @end