2

I have UIView which has the following structure:

UIView
  |- layer (CALayer)
    |- depthLayer (CALayer)
    |- bodyLayer (CALayer)

For layer I set needsDisplayOnBoundsChange = true. When I change size of UIView, layer redraws immediately. But their sublayers updates with some delay (on the next draw iteration). How to sync draw of layers with sublayers depthLayer and bodyLayer?

UIView:

override func drawRect(rect: CGRect) {
    bodyLayer.frame = rect
    depthLayer.frame = CGRectOffset(rect, 0, 5)
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Evgeny
  • 631
  • 6
  • 17
  • Why wouldn't you try to override the setFrame method and set all the frames inside there, Also please note that CALayers will try to animate the Frame Change with key frame animation (Or whatever it called)--> To disable the animations you should refer to this question: http://stackoverflow.com/questions/226354/how-do-you-move-a-calayer-instantly-w-o-animation – Coldsteel48 Dec 20 '15 at 11:51
  • Thanks! I just wrapped frame updates in drawRect: with CATransaction. – Evgeny Dec 20 '15 at 12:02
  • You are welcome sir :-) – Coldsteel48 Dec 20 '15 at 12:03

1 Answers1

13

The delays you see in your CALayer's frames are cause because the Animations -> when you making a change to the CALayer's appearance It tries to animate it (and it often successes -> well this is why they called animations layers).

To disable this behavior (animations) you should call the disable transactions like this:

CATransaction.setValue(kCFBooleanTrue, forKey:kCATransactionDisableActions) //..Make Change to the frames here CATransaction.commit()

Also I don't know wether you have a real need to override the drawRect for this matter -> if you set the UIView's Frame via setFrame: method, it is better to override the setFrame: method itself and adjust the Frames of subLayers there.

Lot of Luck!

Coldsteel48
  • 3,482
  • 4
  • 26
  • 43
  • I don't use _setFrame:_ method I think, UIView size updated by updating Auto Layout constraint. I just wanted to simplify my question, that's why I didn't mention it in the question. Tell me if I'm wrong. Thanks. – Evgeny Dec 20 '15 at 12:20
  • Ok, I wasn't sure :-) Just said in case that you doing it with setFrame, it would be more sufficient ;-) – Coldsteel48 Dec 20 '15 at 12:23
  • Also I am not sure if AutoLayout calls setFrame - you can check it by overriding the setFrame and putting a print there -> then tell me so i will know too :) – Coldsteel48 Dec 20 '15 at 12:24
  • Works very well! I have written the following CATransation.setValue.... //here I add the sublayer CATransaction.commit() – Nisba Nov 14 '16 at 15:41