2015-03-19 40 views
1

我想從0到文本高度(UILabel)的高度進行動畫處理。我正在使用自動佈局,我不知道文本會有多高。我的方法是通過一個高度= 0約束設置爲文本開始,和動畫是這樣的:動畫到文本高度(自動佈局)

//retrieves the height constrain of the clicked item 
NSLayoutConstraint *heightContraint = [heightConstraints objectAtIndex:sender.tag]; 
//activates/deactivates the constraint 
heightContraint.active = !heightContraint.active; 
//animates 
[UIView animateWithDuration:3 animations:^{ 
    [self layoutIfNeeded]; 
}]; 

我的問題是,用這種方法,文字高度不動畫,它的高度變化0立即到達新的高度。只有包含視圖的位置/大小改變是動畫的。如何在不知道文本高度的情況下對文本高度變化進行動畫處理?

+0

嘗試調用layoutIfNeeded爲內部塊該特定視圖 [weakSelf.sampleView layoutIfNeeded]; – erenkabakci 2015-03-19 10:00:12

+0

當您爲約束設置一些確切的值時,動畫是否會發生?只是爲了區分度量和動畫問題。 – VChemezov 2015-03-19 10:00:59

+0

@Akeara我試着調用'[heightContraint.firstItem layoutIfNeeded];'而不是,但我得到了同樣的結果。我認爲這個問題與這樣一個事實有關,它動畫一個BOOL'heightContraint.active',但它不能真正做到這一點,因爲它的值只有YES和NO(類似於動畫isHidden屬性而不是使用不透明度) – Daniel 2015-03-19 10:06:13

回答

0

我找到了解決方案。我沒有設置高度約束,而是設置了一個包含UILabel和上面視圖的包含視圖(裁剪)。然後,我創建了兩個約束,將底部邊緣固定到包含視圖,一個與UILabel相關,另一個與上面的視圖相關。我剛激活/禁用這些約束是這樣的:

//get the constraints 
NSLayoutConstraint *viewAboveTextConstraint = 
     [viewAboveTextConstraints objectAtIndex:sender.tag]; 
NSLayoutConstraint *uilabelContraint = [uilabelContraints objectAtIndex:sender.tag]; 
//flip the active states 
viewAboveTextConstraint.active = !viewAboveTextConstraint.active; 
uilabelContraint.active = !uilabelContraint.active; 
//animate 
[UIView animateWithDuration:.3 animations:^{   
     [self layoutIfNeeded]; 
}];