Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I redraw a UIView's sublayers as I'm animating the UIView's bounds?

I have a UIView with several custom-drawed sublayers (a few CAGradientLayers with CAShapeLayer masks, etc.). I'm using the UIView animation method to animate it's bounds (increasing both its width and height).

[UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveEaseOut|UIViewAnimationOptionAutoreverse|UIViewAnimationOptionRepeat animations:^{
    CGRect bounds = myView.bounds;
    bounds.size.width += 20;
    bounds.size.height += 20;
    myView.bounds = bounds;
} completion:nil];

This works fine, except for the fact that the sublayers don't get redrawn as its animating. What the best way to do this? Do I need to setup some key-value observing detect bounds change and call setNeedsDisplay on all the sublayers?

like image 909
zakdances Avatar asked Apr 13 '13 14:04

zakdances


2 Answers

Set contentMode to UIViewContentModeRedraw on the view you are animating.

like image 132
sixthcent Avatar answered Oct 15 '22 15:10

sixthcent


Layers don't work like views. If you call setNeedsDisplay (which happends if you have set needsDisplayOnBoundsChange to YES) on the parent layer it will not affect the child layers. You need to call setNeedsDisplay them as well.

If your sublayers need to be resized as well when the parent layer is resized then implement layoutSublayers in the parent (or layoutSublayersOfLayer: in its delegate).

like image 4
aLevelOfIndirection Avatar answered Oct 15 '22 15:10

aLevelOfIndirection