views:

8325

answers:

3

Hi, 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

A: 

Are you adding the subviews after setting the transform in the main view? Have you tried hiding the main view instead?

pgb
Yes I have tried to set the transform property of MainView before adding subviews (in the initWithFrame-method) to no help. What would hiding the MainView do to help solven this problem??The problem is that the subviews of MainView slides in during the animation instead of zooming up just like the MainView does.
John Lane
+6  A: 

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.

James Clarke
I can confirm that if you explicitly modify the frame or bounds within the animation, the subviews will not adopt the transform (resulting in a shitty looking zoom)
ohhorob
A: 

It works fine. Thanks! I have some white background during this animation. I just need to see the background view. Do u know how to fix it?

self.view.transform = CGAffineTransformMakeScale(.5,.5);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.9];
self.view.transform = CGAffineTransformMakeScale(1,1);
[UIView commitAnimations];
Dmitry Boyko