您不能爲動畫的行數設置動畫,您應該動畫動畫的大小,並將行數從一開始就設置爲0。
我沒有添加高度計算代碼,因爲它會隱藏真實的動畫。
self.locationDescription = [[UILabel alloc] init];
self.locationDescription.numberOfLines = 0;
[locationDescription setTranslatesAutoresizingMaskIntoConstraints:NO];
// Say, it starts with 50. In your real code, height should be calculated
// to fit the size you want, rounded to lines
NSDictionary *viewsDict = @{@"locationDescription": self.locationDescription};
[self.locationDescription addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"V:[locationDescription(50)]"
options:0 metrics:nil
views:viewsDict];
然後在行動上,
- (void)buttonPressed:(UIButton *)sender
{
NSLog(@"Pressed");
// Assuming there is only one constraint. If not, assign it to another property
// I put 500, but, again, real size rounded to line height should be here
self.locationDescription.constraints[0].constant = 500;
[UIView animateWithDuration:1 animations:^{
// Layouting superview, as its frame can be updated because of a new size
[self.locationDescription.superview layoutIfNeeded];
}];
}
此外,你應該分配給locationDescription
屬性和訪問它與前self.
。
您設置的動畫體外線的號碼,然後裏面。這是一個錯字嗎? – Putz1103
是的,對不起。代碼編輯。 – sheepgobeep
可能不是問題,但仍然是:'-init'不是UILabel的指定初始化程序 - 使用'-initWithFrame:'代替。 – Caleb