5

Imagine a small red box (CALayer instance) drawn in the lower left corner of its parent layer (which is the root layer of a layer hosting NSView).

When the frame of the parent view changes, the red box should remain at the same position on the screen. I do this by adjusting it's position relative to the lower left corner of the parent view .

The problem is that in some cases there is flickering and I can see the red box layer being drawn in the lower left corner of the extended frame before it is shown at the correct position.

I assumed that wrapping the frame and position change into one CATransaction would make both changes together, but that doesn't always work (the docs say that by using a transaction the animations will start at the same time, but there still seems to be a race condition at times).

How can I adjust the frame of the parent NSView while keeping the child layer at its perceived position?

Example and code:

NSView and box layer

My own ideas:

  1. Hide red box layer, update the position, show it again

  2. Use constraints to bind it to the right corner. Problem is that this offset could also change and I would have to update the constraint which could lead to the same flickering issue.

Mark
  • 6,647
  • 1
  • 45
  • 88

1 Answers1

0

Try deleting the code you have already made to have the layer move with the view and put this where you are creating the layer.

boxLayer.autoresizingMask = kCALayerMaxXMargin | kCALayerMinYMargin;
Alex Zielenski
  • 3,591
  • 1
  • 26
  • 44