forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: Add native support on iOS and Android for `Animated.diffClamp` that was added in facebook#9419. **Test plan** Tested that it works properly using the native animations UIExplorer example. Closes facebook#9691 Differential Revision: D3813440 fbshipit-source-id: 48a3ecddf3708fa44b408954d3d8133ec8537f21
- Loading branch information
1 parent
31425e3
commit dfca5ba
Showing
7 changed files
with
153 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
14 changes: 14 additions & 0 deletions
14
Libraries/NativeAnimation/Nodes/RCTDiffClampAnimatedNode.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,14 @@ | ||
/** | ||
* 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 "RCTValueAnimatedNode.h" | ||
|
||
@interface RCTDiffClampAnimatedNode : RCTValueAnimatedNode | ||
|
||
@end |
62 changes: 62 additions & 0 deletions
62
Libraries/NativeAnimation/Nodes/RCTDiffClampAnimatedNode.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,62 @@ | ||
/** | ||
* 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 "RCTDiffClampAnimatedNode.h" | ||
#import "RCTLog.h" | ||
|
||
@implementation RCTDiffClampAnimatedNode | ||
{ | ||
NSNumber *_inputNodeTag; | ||
CGFloat _min; | ||
CGFloat _max; | ||
CGFloat _lastValue; | ||
} | ||
|
||
- (instancetype)initWithTag:(NSNumber *)tag | ||
config:(NSDictionary<NSString *, id> *)config | ||
{ | ||
if (self = [super initWithTag:tag config:config]) { | ||
_inputNodeTag = config[@"input"]; | ||
_min = [config[@"min"] floatValue]; | ||
_max = [config[@"max"] floatValue]; | ||
} | ||
|
||
return self; | ||
} | ||
|
||
- (void)onAttachedToNode:(RCTAnimatedNode *)parent | ||
{ | ||
[super onAttachedToNode:parent]; | ||
|
||
self.value = _lastValue = [self inputNodeValue]; | ||
} | ||
|
||
- (void)performUpdate | ||
{ | ||
[super performUpdate]; | ||
|
||
CGFloat value = [self inputNodeValue]; | ||
|
||
CGFloat diff = value - _lastValue; | ||
_lastValue = value; | ||
self.value = MIN(MAX(self.value + diff, _min), _max); | ||
} | ||
|
||
- (CGFloat)inputNodeValue | ||
{ | ||
RCTValueAnimatedNode *inputNode = (RCTValueAnimatedNode *)self.parentNodes[_inputNodeTag]; | ||
if (![inputNode isKindOfClass:[RCTValueAnimatedNode class]]) { | ||
RCTLogError(@"Illegal node ID set as an input for Animated.DiffClamp node"); | ||
return 0; | ||
} | ||
|
||
return inputNode.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
53 changes: 53 additions & 0 deletions
53
ReactAndroid/src/main/java/com/facebook/react/animated/DiffClampAnimatedNode.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,53 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
package com.facebook.react.animated; | ||
|
||
import com.facebook.react.bridge.JSApplicationCausedNativeException; | ||
import com.facebook.react.bridge.ReadableMap; | ||
|
||
/*package*/ class DiffClampAnimatedNode extends ValueAnimatedNode { | ||
private final NativeAnimatedNodesManager mNativeAnimatedNodesManager; | ||
private final int mInputNodeTag; | ||
private final double mMin; | ||
private final double mMax; | ||
|
||
private double mLastValue; | ||
|
||
public DiffClampAnimatedNode( | ||
ReadableMap config, | ||
NativeAnimatedNodesManager nativeAnimatedNodesManager) { | ||
mNativeAnimatedNodesManager = nativeAnimatedNodesManager; | ||
mInputNodeTag = config.getInt("input"); | ||
mMin = config.getDouble("min"); | ||
mMax = config.getDouble("max"); | ||
|
||
mValue = mLastValue = getInputNodeValue(); | ||
} | ||
|
||
@Override | ||
public void update() { | ||
double value = getInputNodeValue(); | ||
|
||
double diff = value - mLastValue; | ||
mLastValue = value; | ||
mValue = Math.min(Math.max(mValue + diff, mMin), mMax); | ||
} | ||
|
||
private double getInputNodeValue() { | ||
AnimatedNode animatedNode = mNativeAnimatedNodesManager.getNodeById(mInputNodeTag); | ||
if (animatedNode == null || !(animatedNode instanceof ValueAnimatedNode)) { | ||
throw new JSApplicationCausedNativeException("Illegal node ID set as an input for " + | ||
"Animated.DiffClamp node"); | ||
|
||
} | ||
|
||
return ((ValueAnimatedNode) animatedNode).mValue; | ||
} | ||
} |
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