xcode iOS 动画视图包括子视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10604085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:20:36  来源:igfitidea点击:

iOS animating a view including subviews

iosxcodeanimationuiview

提问by toppless

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?

这个动画效果很好,但仅适用于 aView。子视图没有按预期折叠。如何使子视图也折叠?此外,有没有办法在折叠后重新计算原始大小?

THX

谢谢

采纳答案by Zoleas

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

但是,我个人会使用,正如lamelas所说

aView.transform = CGAffineTransformMakeScale(1.0,0.0);

回答by lamelas

Try using this to animate:

尝试使用它来制作动画:

aView.transform = CGAffineTransformMakeScale(1.0,0.0);

回答by Caleb

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.

您可能只想在折叠之前保存高度。