Iphone – UIView beginAnimations with subviews

core-animationiphone

I have a nice and easy "zoom" animation for a view, which starts as a dot and animates up to full screen size:

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView setAnimationDuration:1.0];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        myView.frame = CGRectMake(0,0,320,480);
        myView.transform = CGAffineTransformIdentity;  
        [UIView commitAnimations];

So far so good 🙂

The problem is that when I add subviews to myView, to my surprise, they do not follow their superview's animation scheme!?!?

Btw. subviews are currently added as usual in MyView's initWithFrame. I tried to set their transform property to CGAffineTransformIdentity bu it did not help.

So, what needs to be done to allow a subview of myView to also animate in a nice "zoom" fashion together with its superview?

Thanks in advance!
/John

Best Answer

I just ran in to the same problem, and the solution was surprisingly easy. Whereas modifying the frame size only affects the current view, not the subviews (as you noticed), the transform property applies to the subviews as well.

I'm trying to do the reverse of what you're doing (have a subview that appears to 'drop' on to the top of an existing view when displayed, rather than zoom from the center). This code works for me:

self.transform = CGAffineTransformMakeScale(2,2);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.25];
self.transform = CGAffineTransformMakeScale(1,1);
self.alpha = 1.0;
[UIView commitAnimations];

Try setting self.transform to CGAffineTransformMakeScale(0,0) before beginning the animation, and set it back to (1,1) before committing. Don't modify the frame at all - leave it at the size you want the view to have after the animation completes.