简体   繁体   中英

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?

在要动画的视图上将contentMode设置为UIViewContentModeRedraw

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM