Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate UIView frame with CALayer as subview

What I have:

I have a a hierarchy of UIView's and a CAGradientLayer that I use to set the "background colour" of this hierarchy. So I have something like this:

ViewA -> ViewB -> CAGradientLayer

What I am doing:

At one point, I want to animate the frame (only the height of it) of the ViewA. So it would be something like this:

[UIView animateWithDuration:timeInterval
                      delay:0
                    options:UIViewAnimationOptionCurveEaseIn
                 animations:^{
                              viewA.frame = newFrame;
}];

The issue:

The problem with this is that the CAGradientLayer won't animate it's frame, it simply change it's frame, and then the viewA starts animating as intended (this is clearly visible by setting the simulator to slow-motion animations).

What I tried:

I tried to do what Apple recommends here, by doing it inside the UIView animation block, but it didn't actually work, because I am still animating the frame here:

viewA.frame = newFrame;

I also understand that the I should animate the bounds, and not the frame of the CALayer, the problem is that, how can I animate the bounds and when animating the frame of the ViewA leave that CALayer out of the animation?

What I want:

I just want to animate the change of its height, so I would see both the viewA and the CAGradientLayer animating at the same time.

like image 594
Rui Peres Avatar asked Jul 30 '13 12:07

Rui Peres


1 Answers

First off, read this.

Try encapsulating your view animation code along with layer animation explicitly. Do not expect layer to animate automatically. Since you are dealing with positioning, you would need to change graidentlayer.bounds.

If that does not work, try this before your animation code:

[CATransaction begin];    
[CATransaction setDisableActions: NO];    
[CATransaction commit];
like image 84
Nirav Bhatt Avatar answered Sep 27 '22 18:09

Nirav Bhatt