How to keep CALayer in the same position when resizing the parent NSView?

Imagine a small red rectangle ( CALayerinstance), depicted in the lower left corner of its parent layer (which is the root layer of the hosting level NSView).

When the frame of the parent view changes, the red frame should remain in the same position on the screen. I do this by adjusting its position relative to the lower left corner of the parent view.

The problem is that in some cases flickering occurs, and I see the red left corner stretch in the lower left corner of the expanded frame before it is displayed in the correct position.

I assumed that moving the frame and changing the position to one CATransactionwill make both changes together, but this does not always work (the documents say that when using a transaction, the animation will start at the same time, but there it still seems like this is a race condition )

How to adjust the parent frame NSViewwhile keeping the child layer in its perceived position?

Example and code:

NSView and box layer

My own ideas:

  • Hide the layer with the red field, update the position, show it again

  • Use constraints to snap to the right corner. The problem is that this offset can also change, and I will have to update the constraint, which can lead to the same problematic issue.

+5
source share
1

, , , .

boxLayer.autoresizingMask = kCALayerMaxXMargin | kCALayerMinYMargin;
0

All Articles