Skip to content

Commit d979491

Browse files
sammy-SChurali97
authored andcommitted
remove animation from borderLayer to stop unwanted animations (#42922)
Summary: Pull Request resolved: #42922 changelog: [fix][ios] prevent unwanted border animation The problem: CALayer and its properties are animatable. If RN applies mutations inside an animation block, it will animate. In this particular example, it was animated because of a transition applied by the library and because we were not creating new views, but recycling views from previous screen. This caused size of _borderLayer to change from value A to value B inside of animation block. To resolve this, call removeAllAnimations on borderLayer. Reviewed By: cipolleschi Differential Revision: D53566886 fbshipit-source-id: 98e0b01a9185046e1ee500665c1832060ecc8884
1 parent 02f163e commit d979491

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
@implementation RCTViewComponentView {
3030
UIColor *_backgroundColor;
31-
CALayer *_borderLayer;
31+
__weak CALayer *_borderLayer;
3232
BOOL _needsInvalidateLayer;
3333
BOOL _isJSResponder;
3434
BOOL _removeClippedSubviews;
@@ -397,9 +397,7 @@ - (void)updateLayoutMetrics:(const LayoutMetrics &)layoutMetrics
397397
_layoutMetrics = layoutMetrics;
398398
_needsInvalidateLayer = YES;
399399

400-
if (_borderLayer) {
401-
_borderLayer.frame = self.layer.bounds;
402-
}
400+
_borderLayer.frame = self.layer.bounds;
403401

404402
if (_contentView) {
405403
_contentView.frame = RCTCGRectFromRect(_layoutMetrics.getContentFrame());
@@ -601,10 +599,7 @@ - (void)invalidateLayer
601599

602600
if (useCoreAnimationBorderRendering) {
603601
layer.mask = nil;
604-
if (_borderLayer) {
605-
[_borderLayer removeFromSuperlayer];
606-
_borderLayer = nil;
607-
}
602+
[_borderLayer removeFromSuperlayer];
608603

609604
layer.borderWidth = (CGFloat)borderMetrics.borderWidths.left;
610605
CGColorRef borderColor = RCTCreateCGColorRefFromSharedColor(borderMetrics.borderColors.left);
@@ -617,11 +612,12 @@ - (void)invalidateLayer
617612
layer.backgroundColor = _backgroundColor.CGColor;
618613
} else {
619614
if (!_borderLayer) {
620-
_borderLayer = [CALayer new];
621-
_borderLayer.zPosition = -1024.0f;
622-
_borderLayer.frame = layer.bounds;
623-
_borderLayer.magnificationFilter = kCAFilterNearest;
624-
[layer addSublayer:_borderLayer];
615+
CALayer *borderLayer = [CALayer new];
616+
borderLayer.zPosition = -1024.0f;
617+
borderLayer.frame = layer.bounds;
618+
borderLayer.magnificationFilter = kCAFilterNearest;
619+
[layer addSublayer:borderLayer];
620+
_borderLayer = borderLayer;
625621
}
626622

627623
layer.backgroundColor = nil;
@@ -662,6 +658,10 @@ - (void)invalidateLayer
662658
}
663659
}
664660

661+
// If mutations are applied inside of Animation block, it may cause _borderLayer to be animated.
662+
// To stop that, imperatively remove all animations from _borderLayer.
663+
[_borderLayer removeAllAnimations];
664+
665665
// Stage 2.5. Custom Clipping Mask
666666
CAShapeLayer *maskLayer = nil;
667667
CGFloat cornerRadius = 0;

0 commit comments

Comments
 (0)