6

I'm trying to make an animation for collapsing a view which includes some subviews.

[UIView beginAnimations:@"advancedAnimations" context:nil];
[UIView setAnimationDuration:3.0];

CGRect aFrame = aView.frame;
aView.size.height = 0;
aView.frame = aFrame;

[UIView commitAnimations];

This animation is doing fine, but for the aView only. Subviews doesn't collapse as expected. How do I make the subviews collapsing as well? Furthermore, is there a way to recalculate original size after collapsing?

THX

toppless
  • 411
  • 1
  • 6
  • 16

4 Answers4

5

You have probably forgotten to apply some autoresizing mask to your subviews. If you do

for (UIView *subview in aView.subviews) {
    subview.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin);
}

it should work.

BUT, personally I would use, as lamelas said

aView.transform = CGAffineTransformMakeScale(1.0,0.0);
Zoleas
  • 4,869
  • 1
  • 21
  • 33
  • Still good, but the thing I want to do is to collapse the view, hiding its content as it gets smaller. CGAffineTransformMakeScale scales the whole content. BTW, you can't use 0.0 in transformations. – toppless May 16 '12 at 06:53
  • Where did you see that we can't use 0.0 in transforms ? Anyway, I'm not sure what your question is… Do you want your subviews to scale to, or you want them to be cropped as the animation occurs ? – Zoleas May 16 '12 at 08:10
  • Try to set it to 0.0 and look what is animation will look like and compare it to setting to 0.001. I am tying not to resize the subviews, but to have a "slide door" effect, as the main view gets smaller. – toppless May 16 '12 at 10:03
  • Ok, so you just have to set the property clipToBounds of your main view to YES then. – Zoleas May 16 '12 at 10:10
  • Regarding setting a zero value in transforms (CGAffine + CA3D ones), iOS 6+ seems to let it slide and will animate appropriately. The ".001" trick is the way to go though for compatibility. – walkingbrad Apr 05 '13 at 00:10
4

Try using this to animate:

aView.transform = CGAffineTransformMakeScale(1.0,0.0);
lamelas
  • 872
  • 6
  • 15
  • Works better, but the animation is a bit strange - tries to "squeeze" the view into the middle point. – toppless May 15 '12 at 16:01
0

An educated guess:

[UIView beginAnimations:@"advancedAnimations" context:nil];
[UIView setAnimationDuration:3.0];

CGRect aFrame = aView.frame;
aView.size.height = 0;
aView.frame = aFrame;

for (UIView *subview in aView.subviews) {
    CGRect frame = subview.frame;
    frame.size.height = 0;
    subview.frame = frame;
}

[UIView commitAnimations];

Furthermore, is there a way to recalculate original size after collapsing?

You'll probably want to just save the height before collapsing.

Caleb
  • 124,013
  • 19
  • 183
  • 272
0

The Swift syntax for setting autoresizingMask is slightly different than ObjC:

for subview in view.subviews {
    subview.autoresizingMask = [.flexibleWidth, .flexibleLeftMargin, .flexibleRightMargin]
}
WongWray
  • 2,414
  • 1
  • 20
  • 25