简体   繁体   中英

UIView beginAnimations with subviews

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

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.

You can always use the auto-sizing in the interface builder . This way you can specify the left/margin which you don't want to change and the axis along which you want the particular sub-view to resize to .

自动调整功能

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