Skip to content

[iOS] Animate the image view clipping mask along with the view bounds - #60253

Open
dileepapeiris wants to merge 4 commits into
react:mainfrom
dileepapeiris:fix/ios-image-view-mask-animation
Open

dileepapeiris wants to merge 4 commits into
react:mainfrom
dileepapeiris:fix/ios-image-view-mask-animation

Conversation

@dileepapeiris

Copy link
Copy Markdown

Summary:

When a view clips its content, RCTViewComponentView.invalidateLayer puts a CAShapeLayer mask on every UIImageView subview so the image follows the border radius. The mask is a fresh layer whose path is the image view's bounds at layout time. When the layout change is animated by UIKit, for example during a device rotation, the image view's bounds animate but the mask is already at the final size, so the image is clipped to its final size on the first frame while the view is still animating.

This is visible with <Image> directly and with any library that hosts a UIImageView in a clipping view (expo/expo#54040 is the same bug through expo-image).

This change reuses the mask layer that is already installed and, when UIKit has added a bounds.size animation to the image view's layer, copies that animation onto the mask's path so the mask follows the view with the same timing.

Fixes #60252.

Changelog:

[IOS] [FIXED] - Image views in clipping views are no longer clipped to their final size while a layout animation, such as a rotation, is in progress

Test Plan:

Reproducer: https://github.com/dileepapeiris/react-native-image-rotation-mask-repro (community reproducer template, React Native 0.88.1). The screen has a row of three <Image resizeMode="cover"> inside green overflow: 'hidden' items, and a control row with plain views. Rotate the simulator from landscape to portrait with Slow Animations on.

Before: the image rows show the green item background on the right for the whole rotation, because the image is already clipped to its portrait width. After: the image fills its item through the whole animation. The control row is unchanged.

Portrait and landscape at rest render identically before and after.

Before and after frames, 0.2 s into the rotation, iPhone 17 Pro simulator, iOS 26.5:

Before and after, 0.2 s into the rotation to portrait

@meta-cla

meta-cla Bot commented Nov 24, 2026

Copy link
Copy Markdown

Hi @dileepapeiris!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 24, 2026
@meta-cla

meta-cla Bot commented Nov 24, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Nov 24, 2026

@cipolleschi cipolleschi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is overall good, I think that if the layer is not animated [layer animationForKey:@"bounds.size"] will return null.
I wonder if we can add this check earlier, so we can avoid also this work

CGPathRef imageMaskPath = RCTPathCreateWithRoundedRect(subview.bounds, cornerInsets, nil, NO);
          [self setMaskLayer:imageMask path:imageMaskPath followingBoundsAnimationOfLayer:subview.layer];
          CGPathRelease(imageMaskPath);

altogether.

WDYT?

@dileepapeiris
dileepapeiris force-pushed the fix/ios-image-view-mask-animation branch from ffa9aff to cc7f266 Compare December 1, 2026 09:31
@github-actions github-actions Bot added the Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. label Dec 1, 2026
@dileepapeiris

Copy link
Copy Markdown
Author

This is overall good, I think that if the layer is not animated [layer animationForKey:@"bounds.size"] will return null. I wonder if we can add this check earlier, so we can avoid also this work

CGPathRef imageMaskPath = RCTPathCreateWithRoundedRect(subview.bounds, cornerInsets, nil, NO);
          [self setMaskLayer:imageMask path:imageMaskPath followingBoundsAnimationOfLayer:subview.layer];
          CGPathRelease(imageMaskPath);

altogether.

WDYT?

Thanks, I tried this before replying because it sounded like a nice win, but skipping that block when there is no animation breaks two things.

invalidateLayer runs without any animation most of the time. At first mount there is none, and a layout change that comes from JS has none either. Only a view that is resized inside UIKit's rotation animation gets a bounds.size animation (I checked with a log line in the image view loop). So with the early return, an <Image> with borderWidth + borderRadius never gets its mask at first mount and draws over its own border. And after a rotation, where the mask was created, a later layout change from JS does not update it any more, so for example going from three flex: 1 images to two leaves each image cut at its old width.

So "no animation" only means "nothing to animate". The path still has to be set every time. What I did instead is move the animationForKey: lookup to the top of the helper, so when there is no animation it only sets the path and returns.

I recorded the three versions in the same app (React Native 0.88.1 from source, iPhone 17 Pro simulator on iOS 26.5, Slow Animations on). The app rotates itself at 5 s and 13 s, and the row of images goes from 3 to 2 without animation after each rotation.

Upstream, where the gap from #60252 shows during the rotation back to portrait:

upstream-run.mp4

The early return: the bordered image covers its border from the start, and after the rotation the two images stay cut at their old width:

early-return-run.mp4

This PR:

pr-run.mp4

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[iOS] Image in a clipping view is clipped to its final size during a layout animation such as rotation

2 participants