toppless
toppless

Reputation: 411

iOS animating a view including subviews

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

Upvotes: 6

Views: 9329

Answers (4)

WongWray
WongWray

Reputation: 2566

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

for subview in view.subviews {
    subview.autoresizingMask = [.flexibleWidth, .flexibleLeftMargin, .flexibleRightMargin]
}

Upvotes: 0

Zoleas
Zoleas

Reputation: 4879

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);

Upvotes: 5

Caleb
Caleb

Reputation: 124997

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.

Upvotes: 0

lamelas
lamelas

Reputation: 872

Try using this to animate:

aView.transform = CGAffineTransformMakeScale(1.0,0.0);

Upvotes: 4

Related Questions