Search code examples
core-animationcalayer

Synchronizing CALayer drawing


A drawing application has multiple CALayers. One CALayer displays drawing objects, which are expensive to draw. An overlaid second CALayer displays hover effects - the drawing objects are highlighted on mousemove. This second CALayer is not expensive to draw.

When dragging drawing objects I call [CALayer setNeedsDisplay] on both of these CALayers. Each CALayer retrieves the current state of the drawing objects and displays them. As the drag goes on, the coordinates of the drawing objects change, but because the two layers are not drawn at the same time there's a coordinate mismatch. The difference is very visible in a fast drag.

Is there any CA way to synchronize drawing better between these two layers? It doesn't have to be perfect, but the lag needs to be much smaller. Making both a sublayer of another layer which is updated didn't work. Fiddling with asynchronous drawing didn't help either.


Solution

  • You could try to force the drawing right away by calling displayIfNeeded on the layer right after setNeedsDisplay. For some reason just calling display did not work for me.