2019-10-16 20:03:47 +03:00
|
|
|
/*
|
2018-09-12 01:27:47 +03:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2016-11-28 22:09:46 +03:00
|
|
|
*
|
2018-02-17 05:24:55 +03:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-11-28 22:09:46 +03:00
|
|
|
*/
|
|
|
|
|
CocoaPods frameworks compatibility: Step 2 (#25619)
Summary:
This is my proposal for fixing `use_frameworks!` compatibility without breaking all `<React/*>` imports I outlined in https://github.com/facebook/react-native/pull/25393#issuecomment-508457700. If accepted, it will fix https://github.com/facebook/react-native/issues/25349.
It builds on the changes I made in https://github.com/facebook/react-native/pull/25496 by ensuring each podspec has a unique value for `header_dir` so that framework imports do not conflict. Every podspec which should be included in the `<React/*>` namespace now includes it's headers from `React-Core.podspec`.
The following pods can still be imported with `<React/*>` and so should not have breaking changes: `React-ART`,`React-DevSupport`, `React-CoreModules`, `React-RCTActionSheet`, `React-RCTAnimation`, `React-RCTBlob`, `React-RCTImage`, `React-RCTLinking`, `React-RCTNetwork`, `React-RCTPushNotification`, `React-RCTSettings`, `React-RCTText`, `React-RCTSettings`, `React-RCTVibration`, `React-RCTWebSocket` .
There are still a few breaking changes which I hope will be acceptable:
- `React-Core.podspec` has been moved to the root of the project. Any `Podfile` that references it will need to update the path.
- ~~`React-turbomodule-core`'s headers now live under `<turbomodule/*>`~~ Replaced by https://github.com/facebook/react-native/pull/25619#issuecomment-511091823.
- ~~`React-turbomodulesamples`'s headers now live under `<turbomodulesamples/*>`~~ Replaced by https://github.com/facebook/react-native/pull/25619#issuecomment-511091823.
- ~~`React-TypeSaferty`'s headers now live under `<TypeSafety/*>`~~ Replaced by https://github.com/facebook/react-native/pull/25619#issuecomment-511040967.
- ~~`React-jscallinvoker`'s headers now live under `<jscallinvoker/*>`~~ Replaced by https://github.com/facebook/react-native/pull/25619#issuecomment-511091823.
- Each podspec now uses `s.static_framework = true`. This means that a minimum of CocoaPods 1.5 ([released in April 2018](http://blog.cocoapods.org/CocoaPods-1.5.0/)) is now required. This is needed so that the ` __has_include` conditions can still work when frameworks are enabled.
Still to do:
- ~~Including `React-turbomodule-core` with `use_frameworks!` enabled causes the C++ import failures we saw in https://github.com/facebook/react-native/issues/25349. I'm sure it will be possible to fix this but I need to dig deeper (perhaps a custom modulemap would be needed).~~ Addressed by https://github.com/facebook/react-native/pull/25619/commits/33573511f02f3502a28bad48e085e9a4b8608302.
- I haven't got Fabric working yet. I wonder if it would be acceptable to move Fabric out of the `<React/*>` namespace since it is new? �
## Changelog
[iOS] [Fixed] - Fixed compatibility with CocoaPods frameworks.
Pull Request resolved: https://github.com/facebook/react-native/pull/25619
Test Plan:
### FB
```
buck build catalyst
```
### Sample Project
Everything should work exactly as before, where `use_frameworks!` is not in `Podfile`s. I have a branch on my [sample project](https://github.com/jtreanor/react-native-cocoapods-frameworks) here which has `use_frameworks!` in its `Podfile` to demonstrate this is fixed.
You can see that it works with these steps:
1. `git clone git@github.com:jtreanor/react-native-cocoapods-frameworks.git`
2. `git checkout fix-frameworks-subspecs`
3. `cd ios && pod install`
4. `cd .. && react-native run-ios`
The sample app will build and run successfully. To see that it still works without frameworks, remove `use_frameworks!` from the `Podfile` and do steps 3 and 4 again.
### RNTesterPods
`RNTesterPodsPods` can now work with or without `use_frameworks!`.
1. Go to the `RNTester` directory and run `pod install`.
2. Run the tests in `RNTesterPods.xcworkspace` to see that everything still works fine.
3. Uncomment the `use_frameworks!` line at the top of `RNTester/Podfile` and run `pod install` again.
4. Run the tests again and see that it still works with frameworks enabled.
Reviewed By: PeteTheHeat
Differential Revision: D16465247
Pulled By: PeteTheHeat
fbshipit-source-id: cad837e9cced06d30cc5b372af1c65c7780b9e7a
2019-07-25 08:26:42 +03:00
|
|
|
#import <React/RCTNativeAnimatedNodesManager.h>
|
2016-11-28 22:09:46 +03:00
|
|
|
|
|
|
|
#import <React/RCTConvert.h>
|
|
|
|
|
CocoaPods frameworks compatibility: Step 2 (#25619)
Summary:
This is my proposal for fixing `use_frameworks!` compatibility without breaking all `<React/*>` imports I outlined in https://github.com/facebook/react-native/pull/25393#issuecomment-508457700. If accepted, it will fix https://github.com/facebook/react-native/issues/25349.
It builds on the changes I made in https://github.com/facebook/react-native/pull/25496 by ensuring each podspec has a unique value for `header_dir` so that framework imports do not conflict. Every podspec which should be included in the `<React/*>` namespace now includes it's headers from `React-Core.podspec`.
The following pods can still be imported with `<React/*>` and so should not have breaking changes: `React-ART`,`React-DevSupport`, `React-CoreModules`, `React-RCTActionSheet`, `React-RCTAnimation`, `React-RCTBlob`, `React-RCTImage`, `React-RCTLinking`, `React-RCTNetwork`, `React-RCTPushNotification`, `React-RCTSettings`, `React-RCTText`, `React-RCTSettings`, `React-RCTVibration`, `React-RCTWebSocket` .
There are still a few breaking changes which I hope will be acceptable:
- `React-Core.podspec` has been moved to the root of the project. Any `Podfile` that references it will need to update the path.
- ~~`React-turbomodule-core`'s headers now live under `<turbomodule/*>`~~ Replaced by https://github.com/facebook/react-native/pull/25619#issuecomment-511091823.
- ~~`React-turbomodulesamples`'s headers now live under `<turbomodulesamples/*>`~~ Replaced by https://github.com/facebook/react-native/pull/25619#issuecomment-511091823.
- ~~`React-TypeSaferty`'s headers now live under `<TypeSafety/*>`~~ Replaced by https://github.com/facebook/react-native/pull/25619#issuecomment-511040967.
- ~~`React-jscallinvoker`'s headers now live under `<jscallinvoker/*>`~~ Replaced by https://github.com/facebook/react-native/pull/25619#issuecomment-511091823.
- Each podspec now uses `s.static_framework = true`. This means that a minimum of CocoaPods 1.5 ([released in April 2018](http://blog.cocoapods.org/CocoaPods-1.5.0/)) is now required. This is needed so that the ` __has_include` conditions can still work when frameworks are enabled.
Still to do:
- ~~Including `React-turbomodule-core` with `use_frameworks!` enabled causes the C++ import failures we saw in https://github.com/facebook/react-native/issues/25349. I'm sure it will be possible to fix this but I need to dig deeper (perhaps a custom modulemap would be needed).~~ Addressed by https://github.com/facebook/react-native/pull/25619/commits/33573511f02f3502a28bad48e085e9a4b8608302.
- I haven't got Fabric working yet. I wonder if it would be acceptable to move Fabric out of the `<React/*>` namespace since it is new? �
## Changelog
[iOS] [Fixed] - Fixed compatibility with CocoaPods frameworks.
Pull Request resolved: https://github.com/facebook/react-native/pull/25619
Test Plan:
### FB
```
buck build catalyst
```
### Sample Project
Everything should work exactly as before, where `use_frameworks!` is not in `Podfile`s. I have a branch on my [sample project](https://github.com/jtreanor/react-native-cocoapods-frameworks) here which has `use_frameworks!` in its `Podfile` to demonstrate this is fixed.
You can see that it works with these steps:
1. `git clone git@github.com:jtreanor/react-native-cocoapods-frameworks.git`
2. `git checkout fix-frameworks-subspecs`
3. `cd ios && pod install`
4. `cd .. && react-native run-ios`
The sample app will build and run successfully. To see that it still works without frameworks, remove `use_frameworks!` from the `Podfile` and do steps 3 and 4 again.
### RNTesterPods
`RNTesterPodsPods` can now work with or without `use_frameworks!`.
1. Go to the `RNTester` directory and run `pod install`.
2. Run the tests in `RNTesterPods.xcworkspace` to see that everything still works fine.
3. Uncomment the `use_frameworks!` line at the top of `RNTester/Podfile` and run `pod install` again.
4. Run the tests again and see that it still works with frameworks enabled.
Reviewed By: PeteTheHeat
Differential Revision: D16465247
Pulled By: PeteTheHeat
fbshipit-source-id: cad837e9cced06d30cc5b372af1c65c7780b9e7a
2019-07-25 08:26:42 +03:00
|
|
|
#import <React/RCTAdditionAnimatedNode.h>
|
|
|
|
#import <React/RCTAnimatedNode.h>
|
|
|
|
#import <React/RCTAnimationDriver.h>
|
|
|
|
#import <React/RCTDiffClampAnimatedNode.h>
|
|
|
|
#import <React/RCTDivisionAnimatedNode.h>
|
|
|
|
#import <React/RCTEventAnimation.h>
|
|
|
|
#import <React/RCTFrameAnimation.h>
|
|
|
|
#import <React/RCTDecayAnimation.h>
|
|
|
|
#import <React/RCTInterpolationAnimatedNode.h>
|
|
|
|
#import <React/RCTModuloAnimatedNode.h>
|
|
|
|
#import <React/RCTMultiplicationAnimatedNode.h>
|
|
|
|
#import <React/RCTPropsAnimatedNode.h>
|
|
|
|
#import <React/RCTSpringAnimation.h>
|
|
|
|
#import <React/RCTStyleAnimatedNode.h>
|
|
|
|
#import <React/RCTSubtractionAnimatedNode.h>
|
|
|
|
#import <React/RCTTransformAnimatedNode.h>
|
|
|
|
#import <React/RCTValueAnimatedNode.h>
|
|
|
|
#import <React/RCTTrackingAnimatedNode.h>
|
2016-11-28 22:09:46 +03:00
|
|
|
|
2019-06-21 13:02:41 +03:00
|
|
|
// We do some normalizing of the event names in RCTEventDispatcher#RCTNormalizeInputEventName.
|
|
|
|
// To make things simpler just get rid of the parts we change in the event names we use here.
|
|
|
|
// This is a lot easier than trying to denormalize because there would be multiple possible
|
|
|
|
// denormalized forms for a single input.
|
|
|
|
static NSString *RCTNormalizeAnimatedEventName(NSString *eventName)
|
|
|
|
{
|
|
|
|
if ([eventName hasPrefix:@"on"]) {
|
|
|
|
return [eventName substringFromIndex:2];
|
|
|
|
}
|
|
|
|
if ([eventName hasPrefix:@"top"]) {
|
|
|
|
return [eventName substringFromIndex:3];
|
|
|
|
}
|
|
|
|
return eventName;
|
|
|
|
}
|
|
|
|
|
2016-11-28 22:09:46 +03:00
|
|
|
@implementation RCTNativeAnimatedNodesManager
|
|
|
|
{
|
2019-02-25 23:22:24 +03:00
|
|
|
__weak RCTBridge *_bridge;
|
2016-11-28 22:09:46 +03:00
|
|
|
NSMutableDictionary<NSNumber *, RCTAnimatedNode *> *_animationNodes;
|
2017-03-10 02:16:01 +03:00
|
|
|
// Mapping of a view tag and an event name to a list of event animation drivers. 99% of the time
|
|
|
|
// there will be only one driver per mapping so all code code should be optimized around that.
|
|
|
|
NSMutableDictionary<NSString *, NSMutableArray<RCTEventAnimation *> *> *_eventDrivers;
|
2016-11-28 22:09:46 +03:00
|
|
|
NSMutableSet<id<RCTAnimationDriver>> *_activeAnimations;
|
|
|
|
CADisplayLink *_displayLink;
|
|
|
|
}
|
|
|
|
|
2019-02-25 23:22:24 +03:00
|
|
|
- (instancetype)initWithBridge:(nonnull RCTBridge *)bridge
|
2016-11-28 22:09:46 +03:00
|
|
|
{
|
|
|
|
if ((self = [super init])) {
|
2019-02-25 23:22:24 +03:00
|
|
|
_bridge = bridge;
|
2016-11-28 22:09:46 +03:00
|
|
|
_animationNodes = [NSMutableDictionary new];
|
|
|
|
_eventDrivers = [NSMutableDictionary new];
|
|
|
|
_activeAnimations = [NSMutableSet new];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2019-04-08 19:10:44 +03:00
|
|
|
- (BOOL)isNodeManagedByFabric:(nonnull NSNumber *)tag
|
|
|
|
{
|
|
|
|
RCTAnimatedNode *node = _animationNodes[tag];
|
2019-06-21 00:51:50 +03:00
|
|
|
if (node) {
|
|
|
|
return [node isManagedByFabric];
|
|
|
|
}
|
|
|
|
return false;
|
2019-04-08 19:10:44 +03:00
|
|
|
}
|
|
|
|
|
2016-11-28 22:09:46 +03:00
|
|
|
#pragma mark -- Graph
|
|
|
|
|
|
|
|
- (void)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],
|
|
|
|
@"diffclamp": [RCTDiffClampAnimatedNode class],
|
|
|
|
@"division" : [RCTDivisionAnimatedNode class],
|
|
|
|
@"multiplication" : [RCTMultiplicationAnimatedNode class],
|
|
|
|
@"modulus" : [RCTModuloAnimatedNode class],
|
2018-03-31 06:57:42 +03:00
|
|
|
@"subtraction" : [RCTSubtractionAnimatedNode class],
|
2018-02-16 22:43:34 +03:00
|
|
|
@"transform" : [RCTTransformAnimatedNode class],
|
|
|
|
@"tracking" : [RCTTrackingAnimatedNode class]};
|
2016-11-28 22:09:46 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
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];
|
2018-02-16 22:43:34 +03:00
|
|
|
node.manager = self;
|
2016-11-28 22:09:46 +03:00
|
|
|
_animationNodes[tag] = node;
|
|
|
|
[node setNeedsUpdate];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)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];
|
|
|
|
[childNode setNeedsUpdate];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)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];
|
|
|
|
[childNode setNeedsUpdate];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)connectAnimatedNodeToView:(nonnull NSNumber *)nodeTag
|
|
|
|
viewTag:(nonnull NSNumber *)viewTag
|
2017-01-27 05:14:40 +03:00
|
|
|
viewName:(nonnull NSString *)viewName
|
2016-11-28 22:09:46 +03:00
|
|
|
{
|
|
|
|
RCTAnimatedNode *node = _animationNodes[nodeTag];
|
2017-01-27 05:14:40 +03:00
|
|
|
if ([node isKindOfClass:[RCTPropsAnimatedNode class]]) {
|
2019-02-25 23:22:24 +03:00
|
|
|
[(RCTPropsAnimatedNode *)node connectToView:viewTag viewName:viewName bridge:_bridge];
|
2016-11-28 22:09:46 +03:00
|
|
|
}
|
|
|
|
[node setNeedsUpdate];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)disconnectAnimatedNodeFromView:(nonnull NSNumber *)nodeTag
|
|
|
|
viewTag:(nonnull NSNumber *)viewTag
|
|
|
|
{
|
|
|
|
RCTAnimatedNode *node = _animationNodes[nodeTag];
|
2017-01-27 05:14:40 +03:00
|
|
|
if ([node isKindOfClass:[RCTPropsAnimatedNode class]]) {
|
2016-11-28 22:09:46 +03:00
|
|
|
[(RCTPropsAnimatedNode *)node disconnectFromView:viewTag];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-25 16:02:35 +03:00
|
|
|
- (void)restoreDefaultValues:(nonnull NSNumber *)nodeTag
|
|
|
|
{
|
|
|
|
RCTAnimatedNode *node = _animationNodes[nodeTag];
|
|
|
|
// Restoring default values needs to happen before UIManager operations so it is
|
|
|
|
// possible the node hasn't been created yet if it is being connected and
|
|
|
|
// disconnected in the same batch. In that case we don't need to restore
|
|
|
|
// default values since it will never actually update the view.
|
|
|
|
if (node == nil) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (![node isKindOfClass:[RCTPropsAnimatedNode class]]) {
|
|
|
|
RCTLogError(@"Not a props node.");
|
|
|
|
}
|
|
|
|
[(RCTPropsAnimatedNode *)node restoreDefaultValues];
|
|
|
|
}
|
|
|
|
|
2016-11-28 22:09:46 +03:00
|
|
|
- (void)dropAnimatedNode:(nonnull NSNumber *)tag
|
|
|
|
{
|
|
|
|
RCTAnimatedNode *node = _animationNodes[tag];
|
|
|
|
if (node) {
|
|
|
|
[node detachNode];
|
|
|
|
[_animationNodes removeObjectForKey:tag];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -- Mutations
|
|
|
|
|
|
|
|
- (void)setAnimatedNodeValue:(nonnull NSNumber *)nodeTag
|
|
|
|
value:(nonnull NSNumber *)value
|
|
|
|
{
|
|
|
|
RCTAnimatedNode *node = _animationNodes[nodeTag];
|
|
|
|
if (![node isKindOfClass:[RCTValueAnimatedNode class]]) {
|
|
|
|
RCTLogError(@"Not a value node.");
|
|
|
|
return;
|
|
|
|
}
|
2017-07-21 00:16:06 +03:00
|
|
|
[self stopAnimationsForNode:node];
|
2016-11-28 22:09:46 +03:00
|
|
|
|
|
|
|
RCTValueAnimatedNode *valueNode = (RCTValueAnimatedNode *)node;
|
|
|
|
valueNode.value = value.floatValue;
|
|
|
|
[valueNode setNeedsUpdate];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)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];
|
|
|
|
[valueNode setNeedsUpdate];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)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];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)extractAnimatedNodeOffset:(nonnull NSNumber *)nodeTag
|
|
|
|
{
|
|
|
|
RCTAnimatedNode *node = _animationNodes[nodeTag];
|
|
|
|
if (![node isKindOfClass:[RCTValueAnimatedNode class]]) {
|
|
|
|
RCTLogError(@"Not a value node.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RCTValueAnimatedNode *valueNode = (RCTValueAnimatedNode *)node;
|
|
|
|
[valueNode extractOffset];
|
|
|
|
}
|
|
|
|
|
2020-06-30 03:06:34 +03:00
|
|
|
- (void)getValue:(NSNumber *)nodeTag saveCallback:(RCTResponseSenderBlock)saveCallback
|
|
|
|
{
|
|
|
|
RCTAnimatedNode *node = _animationNodes[nodeTag];
|
|
|
|
if (![node isKindOfClass:[RCTValueAnimatedNode class]]) {
|
|
|
|
RCTLogError(@"Not a value node.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
RCTValueAnimatedNode *valueNode = (RCTValueAnimatedNode *)node;;
|
|
|
|
saveCallback(@[@(valueNode.value)]);
|
|
|
|
}
|
|
|
|
|
2016-11-28 22:09:46 +03:00
|
|
|
#pragma mark -- Drivers
|
|
|
|
|
|
|
|
- (void)startAnimatingNode:(nonnull NSNumber *)animationId
|
|
|
|
nodeTag:(nonnull NSNumber *)nodeTag
|
|
|
|
config:(NSDictionary<NSString *, id> *)config
|
|
|
|
endCallback:(RCTResponseSenderBlock)callBack
|
|
|
|
{
|
2018-02-16 22:43:34 +03:00
|
|
|
// check if the animation has already started
|
|
|
|
for (id<RCTAnimationDriver> driver in _activeAnimations) {
|
|
|
|
if ([driver.animationId isEqual:animationId]) {
|
|
|
|
// if the animation is running, we restart it with an updated configuration
|
|
|
|
[driver resetAnimationConfig:config];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-28 22:09:46 +03:00
|
|
|
RCTValueAnimatedNode *valueNode = (RCTValueAnimatedNode *)_animationNodes[nodeTag];
|
|
|
|
|
|
|
|
NSString *type = config[@"type"];
|
2017-03-10 02:16:01 +03:00
|
|
|
id<RCTAnimationDriver> animationDriver;
|
2016-11-28 22:09:46 +03:00
|
|
|
|
|
|
|
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];
|
|
|
|
|
2017-04-24 14:27:37 +03:00
|
|
|
} else if ([type isEqual:@"decay"]) {
|
|
|
|
animationDriver = [[RCTDecayAnimation alloc] initWithId:animationId
|
|
|
|
config:config
|
|
|
|
forNode:valueNode
|
|
|
|
callBack:callBack];
|
2016-11-28 22:09:46 +03:00
|
|
|
} else {
|
|
|
|
RCTLogError(@"Unsupported animation type: %@", config[@"type"]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
[_activeAnimations addObject:animationDriver];
|
|
|
|
[animationDriver startAnimation];
|
|
|
|
[self startAnimationLoopIfNeeded];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)stopAnimation:(nonnull NSNumber *)animationId
|
|
|
|
{
|
2017-03-10 02:16:01 +03:00
|
|
|
for (id<RCTAnimationDriver> driver in _activeAnimations) {
|
2016-11-28 22:09:46 +03:00
|
|
|
if ([driver.animationId isEqual:animationId]) {
|
2017-03-28 19:08:51 +03:00
|
|
|
[driver stopAnimation];
|
2016-11-28 22:09:46 +03:00
|
|
|
[_activeAnimations removeObject:driver];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-21 00:16:06 +03:00
|
|
|
- (void)stopAnimationsForNode:(nonnull RCTAnimatedNode *)node
|
|
|
|
{
|
|
|
|
NSMutableArray<id<RCTAnimationDriver>> *discarded = [NSMutableArray new];
|
|
|
|
for (id<RCTAnimationDriver> driver in _activeAnimations) {
|
|
|
|
if ([driver.valueNode isEqual:node]) {
|
|
|
|
[discarded addObject:driver];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (id<RCTAnimationDriver> driver in discarded) {
|
|
|
|
[driver stopAnimation];
|
|
|
|
[_activeAnimations removeObject:driver];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-28 22:09:46 +03:00
|
|
|
#pragma mark -- Events
|
|
|
|
|
|
|
|
- (void)addAnimatedEventToView:(nonnull NSNumber *)viewTag
|
|
|
|
eventName:(nonnull NSString *)eventName
|
|
|
|
eventMapping:(NSDictionary<NSString *, id> *)eventMapping
|
|
|
|
{
|
|
|
|
NSNumber *nodeTag = [RCTConvert NSNumber:eventMapping[@"animatedValueTag"]];
|
|
|
|
RCTAnimatedNode *node = _animationNodes[nodeTag];
|
|
|
|
|
|
|
|
if (!node) {
|
|
|
|
RCTLogError(@"Animated node with tag %@ does not exists", nodeTag);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (![node isKindOfClass:[RCTValueAnimatedNode class]]) {
|
|
|
|
RCTLogError(@"Animated node connected to event should be of type RCTValueAnimatedNode");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSArray<NSString *> *eventPath = [RCTConvert NSStringArray:eventMapping[@"nativeEventPath"]];
|
|
|
|
|
|
|
|
RCTEventAnimation *driver =
|
2017-03-10 02:16:01 +03:00
|
|
|
[[RCTEventAnimation alloc] initWithEventPath:eventPath valueNode:(RCTValueAnimatedNode *)node];
|
2016-11-28 22:09:46 +03:00
|
|
|
|
2019-06-21 13:02:41 +03:00
|
|
|
NSString *key = [NSString stringWithFormat:@"%@%@", viewTag, RCTNormalizeAnimatedEventName(eventName)];
|
2017-03-10 02:16:01 +03:00
|
|
|
if (_eventDrivers[key] != nil) {
|
|
|
|
[_eventDrivers[key] addObject:driver];
|
|
|
|
} else {
|
|
|
|
NSMutableArray<RCTEventAnimation *> *drivers = [NSMutableArray new];
|
|
|
|
[drivers addObject:driver];
|
|
|
|
_eventDrivers[key] = drivers;
|
|
|
|
}
|
2016-11-28 22:09:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)removeAnimatedEventFromView:(nonnull NSNumber *)viewTag
|
|
|
|
eventName:(nonnull NSString *)eventName
|
2017-03-10 02:16:01 +03:00
|
|
|
animatedNodeTag:(nonnull NSNumber *)animatedNodeTag
|
2016-11-28 22:09:46 +03:00
|
|
|
{
|
2019-06-21 13:02:41 +03:00
|
|
|
NSString *key = [NSString stringWithFormat:@"%@%@", viewTag, RCTNormalizeAnimatedEventName(eventName)];
|
2017-03-10 02:16:01 +03:00
|
|
|
if (_eventDrivers[key] != nil) {
|
|
|
|
if (_eventDrivers[key].count == 1) {
|
|
|
|
[_eventDrivers removeObjectForKey:key];
|
|
|
|
} else {
|
|
|
|
NSMutableArray<RCTEventAnimation *> *driversForKey = _eventDrivers[key];
|
|
|
|
for (NSUInteger i = 0; i < driversForKey.count; i++) {
|
|
|
|
if (driversForKey[i].valueNode.nodeTag == animatedNodeTag) {
|
|
|
|
[driversForKey removeObjectAtIndex:i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-28 22:09:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)handleAnimatedEvent:(id<RCTEvent>)event
|
|
|
|
{
|
|
|
|
if (_eventDrivers.count == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-21 13:02:41 +03:00
|
|
|
NSString *key = [NSString stringWithFormat:@"%@%@", event.viewTag, RCTNormalizeAnimatedEventName(event.eventName)];
|
2017-03-10 02:16:01 +03:00
|
|
|
NSMutableArray<RCTEventAnimation *> *driversForKey = _eventDrivers[key];
|
|
|
|
if (driversForKey) {
|
|
|
|
for (RCTEventAnimation *driver in driversForKey) {
|
2017-07-21 00:16:06 +03:00
|
|
|
[self stopAnimationsForNode:driver.valueNode];
|
2017-03-10 02:16:01 +03:00
|
|
|
[driver updateWithEvent:event];
|
|
|
|
}
|
|
|
|
|
2016-11-28 22:09:46 +03:00
|
|
|
[self updateAnimations];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -- Listeners
|
|
|
|
|
|
|
|
- (void)startListeningToAnimatedNodeValue:(nonnull NSNumber *)tag
|
|
|
|
valueObserver:(id<RCTValueAnimatedNodeObserver>)valueObserver
|
|
|
|
{
|
|
|
|
RCTAnimatedNode *node = _animationNodes[tag];
|
2017-01-27 05:14:40 +03:00
|
|
|
if ([node isKindOfClass:[RCTValueAnimatedNode class]]) {
|
2016-11-28 22:09:46 +03:00
|
|
|
((RCTValueAnimatedNode *)node).valueObserver = valueObserver;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)stopListeningToAnimatedNodeValue:(nonnull NSNumber *)tag
|
|
|
|
{
|
|
|
|
RCTAnimatedNode *node = _animationNodes[tag];
|
2017-01-27 05:14:40 +03:00
|
|
|
if ([node isKindOfClass:[RCTValueAnimatedNode class]]) {
|
2017-03-28 19:08:51 +03:00
|
|
|
((RCTValueAnimatedNode *)node).valueObserver = nil;
|
2016-11-28 22:09:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#pragma mark -- Animation Loop
|
|
|
|
|
|
|
|
- (void)startAnimationLoopIfNeeded
|
|
|
|
{
|
|
|
|
if (!_displayLink && _activeAnimations.count > 0) {
|
2017-03-28 19:08:51 +03:00
|
|
|
_displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(stepAnimations:)];
|
2016-11-28 22:09:46 +03:00
|
|
|
[_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)stopAnimationLoopIfNeeded
|
|
|
|
{
|
2017-01-06 08:48:10 +03:00
|
|
|
if (_activeAnimations.count == 0) {
|
|
|
|
[self stopAnimationLoop];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)stopAnimationLoop
|
|
|
|
{
|
|
|
|
if (_displayLink) {
|
2016-11-28 22:09:46 +03:00
|
|
|
[_displayLink invalidate];
|
|
|
|
_displayLink = nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-28 19:08:51 +03:00
|
|
|
- (void)stepAnimations:(CADisplayLink *)displaylink
|
2016-11-28 22:09:46 +03:00
|
|
|
{
|
2017-03-28 19:08:51 +03:00
|
|
|
NSTimeInterval time = displaylink.timestamp;
|
2017-03-10 02:16:01 +03:00
|
|
|
for (id<RCTAnimationDriver> animationDriver in _activeAnimations) {
|
2017-03-28 19:08:51 +03:00
|
|
|
[animationDriver stepAnimationWithTime:time];
|
2016-11-28 22:09:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[self updateAnimations];
|
|
|
|
|
2017-03-10 02:16:01 +03:00
|
|
|
for (id<RCTAnimationDriver> animationDriver in [_activeAnimations copy]) {
|
2016-11-28 22:09:46 +03:00
|
|
|
if (animationDriver.animationHasFinished) {
|
2017-03-28 19:08:51 +03:00
|
|
|
[animationDriver stopAnimation];
|
2016-11-28 22:09:46 +03:00
|
|
|
[_activeAnimations removeObject:animationDriver];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[self stopAnimationLoopIfNeeded];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#pragma mark -- Updates
|
|
|
|
|
|
|
|
- (void)updateAnimations
|
|
|
|
{
|
|
|
|
[_animationNodes enumerateKeysAndObjectsUsingBlock:^(NSNumber *key, RCTAnimatedNode *node, BOOL *stop) {
|
|
|
|
if (node.needsUpdate) {
|
|
|
|
[node updateNodeIfNecessary];
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|