簡體   English   中英

iOS 動畫視圖包括子視圖

[英]iOS animating a view including subviews

我正在嘗試制作一個 animation 來折疊包含一些子視圖的視圖。

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

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

[UIView commitAnimations];

這個 animation 運行良好,但僅適用於 aView。 子視圖沒有按預期折疊。 我如何使子視圖也折疊起來? 此外,有沒有辦法在折疊后重新計算原始大小?

謝謝

您可能忘記將一些自動調整遮罩應用於子視圖。 如果你這樣做

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

它應該工作。

但是,正如拉梅拉斯所說,我會親自使用

aView.transform = CGAffineTransformMakeScale(1.0,0.0);

嘗試使用它來動畫:

aView.transform = CGAffineTransformMakeScale(1.0,0.0);

一個有根據的猜測:

[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];

此外,還有一種方法可以在折疊后重新計算原始大小嗎?

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

Swift 設置autoresizingMask的語法與 ObjC 略有不同:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM