forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an implementation of Animated.subtract
Summary: Fixes facebook#18451 I've added another example to NativeAnimationsExample, which makes use of `Animated.substract()`, let me know if the example is not desired / doesn't add much value. Below two GIFs of the new method working on iOS and Android: <img width="320" src="https://user-images.githubusercontent.com/1437605/38154748-165cc5f8-3474-11e8-8b31-504444271896.gif" /> <img width="320" src="https://user-images.githubusercontent.com/1437605/38154749-1679bff0-3474-11e8-80b1-b558d44e0494.gif" /> <!-- Required: Write your test plan here. If you changed any code, please provide us with clear instructions on how you verified your changes work. Bonus points for screenshots and videos! --> facebook/react-native-website#276 [GENERAL] [ENHANCEMENT] [Animated] - Implemented Animated.subtract Closes facebook#18630 Differential Revision: D7462867 Pulled By: hramos fbshipit-source-id: 4cb0b8af08bb0c841e44ea2099889b8c02a22a4a
- Loading branch information
1 parent
f7d3249
commit b158190
Showing
10 changed files
with
254 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @providesModule AnimatedSubtraction | ||
* @flow | ||
* @format | ||
*/ | ||
'use strict'; | ||
|
||
const AnimatedInterpolation = require('./AnimatedInterpolation'); | ||
const AnimatedNode = require('./AnimatedNode'); | ||
const AnimatedValue = require('./AnimatedValue'); | ||
const AnimatedWithChildren = require('./AnimatedWithChildren'); | ||
|
||
import type {InterpolationConfigType} from './AnimatedInterpolation'; | ||
|
||
class AnimatedSubtraction extends AnimatedWithChildren { | ||
_a: AnimatedNode; | ||
_b: AnimatedNode; | ||
|
||
constructor(a: AnimatedNode | number, b: AnimatedNode | number) { | ||
super(); | ||
this._a = typeof a === 'number' ? new AnimatedValue(a) : a; | ||
this._b = typeof b === 'number' ? new AnimatedValue(b) : b; | ||
} | ||
|
||
__makeNative() { | ||
this._a.__makeNative(); | ||
this._b.__makeNative(); | ||
super.__makeNative(); | ||
} | ||
|
||
__getValue(): number { | ||
return this._a.__getValue() - this._b.__getValue(); | ||
} | ||
|
||
interpolate(config: InterpolationConfigType): AnimatedInterpolation { | ||
return new AnimatedInterpolation(this, config); | ||
} | ||
|
||
__attach(): void { | ||
this._a.__addChild(this); | ||
this._b.__addChild(this); | ||
} | ||
|
||
__detach(): void { | ||
this._a.__removeChild(this); | ||
this._b.__removeChild(this); | ||
super.__detach(); | ||
} | ||
|
||
__getNativeConfig(): any { | ||
return { | ||
type: 'subtraction', | ||
input: [this._a.__getNativeTag(), this._b.__getNativeTag()], | ||
}; | ||
} | ||
} | ||
|
||
module.exports = AnimatedSubtraction; |
13 changes: 13 additions & 0 deletions
13
Libraries/NativeAnimation/Nodes/RCTSubtractionAnimatedNode.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#import "RCTValueAnimatedNode.h" | ||
|
||
@interface RCTSubtractionAnimatedNode : RCTValueAnimatedNode | ||
|
||
@end | ||
|
27 changes: 27 additions & 0 deletions
27
Libraries/NativeAnimation/Nodes/RCTSubtractionAnimatedNode.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#import "RCTSubtractionAnimatedNode.h" | ||
|
||
@implementation RCTSubtractionAnimatedNode | ||
|
||
- (void)performUpdate | ||
{ | ||
[super performUpdate]; | ||
NSArray<NSNumber *> *inputNodes = self.config[@"input"]; | ||
if (inputNodes.count > 1) { | ||
RCTValueAnimatedNode *parent1 = (RCTValueAnimatedNode *)[self.parentNodes objectForKey:inputNodes[0]]; | ||
RCTValueAnimatedNode *parent2 = (RCTValueAnimatedNode *)[self.parentNodes objectForKey:inputNodes[1]]; | ||
if ([parent1 isKindOfClass:[RCTValueAnimatedNode class]] && | ||
[parent2 isKindOfClass:[RCTValueAnimatedNode class]]) { | ||
self.value = parent1.value - parent2.value; | ||
} | ||
} | ||
} | ||
|
||
@end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
ReactAndroid/src/main/java/com/facebook/react/animated/SubtractionAnimatedNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package com.facebook.react.animated; | ||
|
||
import com.facebook.react.bridge.JSApplicationCausedNativeException; | ||
import com.facebook.react.bridge.ReadableArray; | ||
import com.facebook.react.bridge.ReadableMap; | ||
|
||
/** | ||
* Animated node that plays a role of value aggregator. It takes two or more value nodes as an input | ||
* and outputs a difference of values outputted by those nodes. | ||
*/ | ||
/*package*/ class SubtractionAnimatedNode extends ValueAnimatedNode { | ||
|
||
private final NativeAnimatedNodesManager mNativeAnimatedNodesManager; | ||
private final int[] mInputNodes; | ||
|
||
public SubtractionAnimatedNode( | ||
ReadableMap config, | ||
NativeAnimatedNodesManager nativeAnimatedNodesManager) { | ||
mNativeAnimatedNodesManager = nativeAnimatedNodesManager; | ||
ReadableArray inputNodes = config.getArray("input"); | ||
mInputNodes = new int[inputNodes.size()]; | ||
for (int i = 0; i < mInputNodes.length; i++) { | ||
mInputNodes[i] = inputNodes.getInt(i); | ||
} | ||
} | ||
|
||
@Override | ||
public void update() { | ||
for (int i = 0; i < mInputNodes.length; i++) { | ||
AnimatedNode animatedNode = mNativeAnimatedNodesManager.getNodeById(mInputNodes[i]); | ||
if (animatedNode != null && animatedNode instanceof ValueAnimatedNode) { | ||
double value = ((ValueAnimatedNode) animatedNode).getValue(); | ||
if (i == 0) { | ||
mValue = value; | ||
continue; | ||
} | ||
mValue -= ((ValueAnimatedNode) animatedNode).getValue(); | ||
} else { | ||
throw new JSApplicationCausedNativeException("Illegal node ID set as an input for " + | ||
"Animated.subtract node"); | ||
} | ||
} | ||
} | ||
} |