From feee0764e46d9f5c2d82d245e4c57b7848a26435 Mon Sep 17 00:00:00 2001 From: "K.Dileepa Thushan Peiris" Date: Tue, 24 Nov 2026 11:43:54 +0530 Subject: [PATCH] [iOS] Animate the image view clipping mask along with the view bounds --- .../View/RCTViewComponentView.mm | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm index 8d1295d8db3..f6fb4d3fc22 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm @@ -1505,7 +1505,13 @@ - (void)invalidateLayer // If the subview is an image view, we have to apply the mask directly to the image view's layer, // otherwise the image might overflow with the border radius. - subview.layer.mask = [self createMaskLayer:subview.bounds cornerInsets:cornerInsets]; + CAShapeLayer *imageMask = [subview.layer.mask isKindOfClass:[CAShapeLayer class]] + ? (CAShapeLayer *)subview.layer.mask + : [CAShapeLayer layer]; + CGPathRef imageMaskPath = RCTPathCreateWithRoundedRect(subview.bounds, cornerInsets, nil, NO); + [self setMaskLayer:imageMask path:imageMaskPath followingBoundsAnimationOfLayer:subview.layer]; + CGPathRelease(imageMaskPath); + subview.layer.mask = imageMask; } } } else if ( @@ -1551,6 +1557,29 @@ - (CAShapeLayer *)createMaskLayer:(CGRect)bounds cornerInsets:(RCTCornerInsets)c return maskLayer; } +// Animates the path change together with the bounds animation UIKit may have added to the masked layer, +// so the mask does not snap to the final size while the layer is still animating (e.g. during rotation). +- (void)setMaskLayer:(CAShapeLayer *)maskLayer path:(CGPathRef)path followingBoundsAnimationOfLayer:(CALayer *)layer +{ + CGPathRef previousPath = maskLayer.path != NULL ? CGPathRetain(maskLayer.path) : NULL; + maskLayer.path = path; + + CAAnimation *boundsAnimation = [layer animationForKey:@"bounds.size"]; + if (previousPath != NULL && [boundsAnimation isKindOfClass:[CABasicAnimation class]] && + !CGPathEqualToPath(previousPath, path)) { + CABasicAnimation *pathAnimation = [(CABasicAnimation *)boundsAnimation copy]; + pathAnimation.keyPath = @"path"; + pathAnimation.fromValue = (__bridge id)previousPath; + pathAnimation.toValue = (__bridge id)path; + pathAnimation.additive = NO; + [maskLayer addAnimation:pathAnimation forKey:@"path"]; + } + + if (previousPath != NULL) { + CGPathRelease(previousPath); + } +} + - (void)clearExistingBackgroundImageLayers { if (_backgroundImageLayers == nil) {