Skip to content

Commit 8fbc662

Browse files
author
Ian MacDonald
committed
Updated LayoutAnimation to accept 'translateX' animations.
1 parent bb48c0c commit 8fbc662

File tree

5 files changed

+34
-7
lines changed

5 files changed

+34
-7
lines changed

Libraries/LayoutAnimation/LayoutAnimation.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ var Types = keyMirror(TypesEnum);
3131
var PropertiesEnum = {
3232
opacity: true,
3333
scaleXY: true,
34+
translateX: true,
3435
};
3536
var Properties = keyMirror(PropertiesEnum);
3637

@@ -45,6 +46,7 @@ var animType = PropTypes.shape({
4546
property: PropTypes.oneOf( // Only applies to create/delete
4647
Object.keys(Properties)
4748
),
49+
param: PropTypes.number,
4850
});
4951

5052
type Anim = {
@@ -54,6 +56,7 @@ type Anim = {
5456
initialVelocity?: number,
5557
type?: $Enum<typeof TypesEnum>,
5658
property?: $Enum<typeof PropertiesEnum>,
59+
param?: number,
5760
}
5861

5962
var configType = PropTypes.shape({

React/Modules/RCTUIManager.m

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ @interface RCTAnimation : NSObject
6262
@property (nonatomic, readonly) CGFloat springDamping;
6363
@property (nonatomic, readonly) CGFloat initialVelocity;
6464
@property (nonatomic, readonly) RCTAnimationType animationType;
65+
@property (nonatomic, readonly) CGFloat param;
6566

6667
@end
6768

@@ -140,6 +141,7 @@ - (instancetype)initWithDuration:(NSTimeInterval)duration dictionary:(NSDictiona
140141
_springDamping = [RCTConvert CGFloat:config[@"springDamping"]];
141142
_initialVelocity = [RCTConvert CGFloat:config[@"initialVelocity"]];
142143
}
144+
_param = [RCTConvert CGFloat:config[@"param"]];
143145
}
144146
return self;
145147
}
@@ -651,12 +653,17 @@ - (RCTViewManagerUIBlock)uiBlockWithLayoutUpdateForRootView:(RCTRootShadowView *
651653

652654
CATransform3D finalTransform = view.layer.transform;
653655
CGFloat finalOpacity = view.layer.opacity;
656+
CGPoint finalPos = view.layer.position;
654657

655658
NSString *property = createAnimation.property;
656659
if ([property isEqualToString:@"scaleXY"]) {
657660
view.layer.transform = CATransform3DMakeScale(0, 0, 0);
658661
} else if ([property isEqualToString:@"opacity"]) {
659662
view.layer.opacity = 0.0;
663+
} else if ([property isEqualToString:@"translateX"]) {
664+
CGPoint pos = view.layer.position;
665+
pos.x += createAnimation.param;
666+
view.layer.position = pos;
660667
} else {
661668
RCTLogError(@"Unsupported layout animation createConfig property %@",
662669
createAnimation.property);
@@ -667,6 +674,8 @@ - (RCTViewManagerUIBlock)uiBlockWithLayoutUpdateForRootView:(RCTRootShadowView *
667674
view.layer.transform = finalTransform;
668675
} else if ([property isEqualToString:@"opacity"]) {
669676
view.layer.opacity = finalOpacity;
677+
} else if ([property isEqualToString:@"translateX"]) {
678+
view.layer.position = finalPos;
670679
}
671680
if (updateBlock) {
672681
updateBlock(self, viewRegistry);
@@ -829,6 +838,10 @@ - (void)_removeChildren:(NSArray<UIView *> *)children
829838
removedChild.layer.transform = CATransform3DMakeScale(0.001, 0.001, 0.001);
830839
} else if ([property isEqualToString:@"opacity"]) {
831840
removedChild.layer.opacity = 0.0;
841+
} else if ([property isEqualToString:@"translateX"]) {
842+
CGPoint pos = removedChild.layer.position;
843+
pos.x += deleteAnimation.param;
844+
removedChild.layer.position = pos;
832845
} else {
833846
RCTLogError(@"Unsupported layout animation createConfig property %@",
834847
deleteAnimation.property);
@@ -987,12 +1000,9 @@ - (void)_manageChildren:(NSNumber *)containerTag
9871000
// When performing a delete animation, views are not removed immediately
9881001
// from their container so we need to offset the insertion index if a view
9891002
// that will be removed appears earlier than the view we are inserting.
990-
if (isUIViewRegistry && _viewsToBeDeleted.count > 0) {
991-
for (NSInteger index = 0; index < insertAtIndex; index++) {
992-
UIView *subview = ((UIView *)container).reactSubviews[index];
993-
if ([_viewsToBeDeleted containsObject:subview]) {
994-
insertAtIndex++;
995-
}
1003+
if (isUIViewRegistry && removeAtIndices.count > 0) {
1004+
if ([removeAtIndices containsObject:reactIndex]) {
1005+
insertAtIndex++;
9961006
}
9971007
}
9981008

ReactAndroid/src/main/java/com/facebook/react/uimanager/layoutanimation/AbstractLayoutAnimation.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@ InterpolatorType.EASE_IN_EASE_OUT, new AccelerateDecelerateInterpolator(),
4848

4949
protected @Nullable AnimatedPropertyType mAnimatedProperty;
5050
protected int mDurationMs;
51+
protected double mAnimatedParam;
5152

5253
public void reset() {
5354
mAnimatedProperty = null;
5455
mDurationMs = 0;
5556
mDelayMs = 0;
57+
mAnimatedParam = 0;
5658
mInterpolator = null;
5759
}
5860

@@ -61,6 +63,7 @@ public void initializeFromConfig(ReadableMap data, int globalDuration) {
6163
AnimatedPropertyType.fromString(data.getString("property")) : null;
6264
mDurationMs = data.hasKey("duration") ? data.getInt("duration") : globalDuration;
6365
mDelayMs = data.hasKey("delay") ? data.getInt("delay") : 0;
66+
mAnimatedParam = data.hasKey("param") ? data.getDouble("param") : 0;
6467
if (!data.hasKey("type")) {
6568
throw new IllegalArgumentException("Missing interpolation type.");
6669
}

ReactAndroid/src/main/java/com/facebook/react/uimanager/layoutanimation/AnimatedPropertyType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
*/
99
/* package */ enum AnimatedPropertyType {
1010
OPACITY("opacity"),
11-
SCALE_XY("scaleXY");
11+
SCALE_XY("scaleXY"),
12+
TRANSATE_X("translateX");
1213

1314
private final String mName;
1415

ReactAndroid/src/main/java/com/facebook/react/uimanager/layoutanimation/BaseLayoutAnimation.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.view.View;
66
import android.view.animation.Animation;
77
import android.view.animation.ScaleAnimation;
8+
import android.view.animation.TranslateAnimation;
89

910
import com.facebook.react.uimanager.IllegalViewOperationException;
1011

@@ -42,6 +43,15 @@ Animation createAnimationImpl(View view, int x, int y, int width, int height) {
4243
Animation.RELATIVE_TO_SELF,
4344
.5f);
4445
}
46+
case TRANSFORM_X: {
47+
float fromValue = isReverse() ? 0.0f : mAnimatedParam;
48+
float toValue = isReverse() ? mAnimatedParam : 0.0f;
49+
return new TranslateAnimation(
50+
fromValue,
51+
toValue,
52+
0.0f,
53+
0.0f);
54+
}
4555
default:
4656
throw new IllegalViewOperationException(
4757
"Missing animation for property : " + mAnimatedProperty);

0 commit comments

Comments
 (0)