Skip to content

Commit 6535858

Browse files
ryangombaFacebook Github Bot
authored andcommitted
Add extractOffset to Animated
Summary: `flattenOffset` has proven extremely useful, especially when dealing with pan responders and other gesture based animations, but I've also found a number of use cases for the inverse. This diff introduces `extractOffset`, which sets the offset value to the base value, and resets the base value to zero. A common use case would be to extractOffset onGrant and flattenOffset onRelease. Closes #10721 Differential Revision: D4145744 fbshipit-source-id: dc2aa31652df0b31450556f611db43548180c7dd
1 parent bf2b435 commit 6535858

File tree

8 files changed

+59
-0
lines changed

8 files changed

+59
-0
lines changed

Libraries/Animated/src/AnimatedImplementation.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,18 @@ class AnimatedValue extends AnimatedWithChildren {
763763
}
764764
}
765765

766+
/**
767+
* Sets the offset value to the base value, and resets the base value to zero.
768+
* The final output of the value is unchanged.
769+
*/
770+
extractOffset(): void {
771+
this._offset += this._value;
772+
this._value = 0;
773+
if (this.__isNative) {
774+
NativeAnimatedAPI.extractAnimatedNodeOffset(this.__getNativeTag());
775+
}
776+
}
777+
766778
/**
767779
* Adds an asynchronous listener to the value so you can observe updates from
768780
* animations. This is useful because there is no way to

Libraries/Animated/src/NativeAnimatedHelper.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ const API = {
7373
assertNativeAnimatedModule();
7474
NativeAnimatedModule.flattenAnimatedNodeOffset(nodeTag);
7575
},
76+
extractAnimatedNodeOffset: function(nodeTag: number): void {
77+
assertNativeAnimatedModule();
78+
NativeAnimatedModule.extractAnimatedNodeOffset(nodeTag);
79+
},
7680
connectAnimatedNodeToView: function(nodeTag: number, viewTag: number): void {
7781
assertNativeAnimatedModule();
7882
NativeAnimatedModule.connectAnimatedNodeToView(nodeTag, viewTag);

Libraries/NativeAnimation/Nodes/RCTValueAnimatedNode.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
- (void)setOffset:(CGFloat)offset;
2424
- (void)flattenOffset;
25+
- (void)extractOffset;
2526

2627
@property (nonatomic, assign) CGFloat value;
2728
@property (nonatomic, weak) id<RCTValueAnimatedNodeObserver> valueObserver;

Libraries/NativeAnimation/Nodes/RCTValueAnimatedNode.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ - (void)flattenOffset
3535
_offset = 0;
3636
}
3737

38+
- (void)extractOffset
39+
{
40+
_offset += _value;
41+
_value = 0;
42+
}
43+
3844
- (CGFloat)value
3945
{
4046
return _value + _offset;

Libraries/NativeAnimation/RCTNativeAnimatedModule.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,18 @@ - (dispatch_queue_t)methodQueue
225225
[valueNode flattenOffset];
226226
}
227227

228+
RCT_EXPORT_METHOD(extractAnimatedNodeOffset:(nonnull NSNumber *)nodeTag)
229+
{
230+
RCTAnimatedNode *node = _animationNodes[nodeTag];
231+
if (![node isKindOfClass:[RCTValueAnimatedNode class]]) {
232+
RCTLogError(@"Not a value node.");
233+
return;
234+
}
235+
236+
RCTValueAnimatedNode *valueNode = (RCTValueAnimatedNode *)node;
237+
[valueNode extractOffset];
238+
}
239+
228240
RCT_EXPORT_METHOD(connectAnimatedNodeToView:(nonnull NSNumber *)nodeTag
229241
viewTag:(nonnull NSNumber *)viewTag)
230242
{

ReactAndroid/src/main/java/com/facebook/react/animated/NativeAnimatedModule.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,16 @@ public void execute(NativeAnimatedNodesManager animatedNodesManager) {
262262
});
263263
}
264264

265+
@ReactMethod
266+
public void extractAnimatedNodeOffset(final int tag) {
267+
mOperations.add(new UIThreadOperation() {
268+
@Override
269+
public void execute(NativeAnimatedNodesManager animatedNodesManager) {
270+
animatedNodesManager.extractAnimatedNodeOffset(tag);
271+
}
272+
});
273+
}
274+
265275
@ReactMethod
266276
public void startAnimatingNode(
267277
final int animationId,

ReactAndroid/src/main/java/com/facebook/react/animated/NativeAnimatedNodesManager.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,15 @@ public void flattenAnimatedNodeOffset(int tag) {
157157
((ValueAnimatedNode) node).flattenOffset();
158158
}
159159

160+
public void extractAnimatedNodeOffset(int tag) {
161+
AnimatedNode node = mAnimatedNodes.get(tag);
162+
if (node == null || !(node instanceof ValueAnimatedNode)) {
163+
throw new JSApplicationIllegalArgumentException("Animated node with tag " + tag +
164+
" does not exists or is not a 'value' node");
165+
}
166+
((ValueAnimatedNode) node).extractOffset();
167+
}
168+
160169
public void startAnimatingNode(
161170
int animationId,
162171
int animatedNodeTag,

ReactAndroid/src/main/java/com/facebook/react/animated/ValueAnimatedNode.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public void flattenOffset() {
4040
mOffset = 0;
4141
}
4242

43+
public void extractOffset() {
44+
mOffset += mValue;
45+
mValue = 0;
46+
}
47+
4348
public void onValueUpdate() {
4449
if (mValueListener == null) {
4550
return;

0 commit comments

Comments
 (0)