Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
689 views
in Technique[技术] by (71.8m points)

ios - UILabel does not animate inside UIView, autolayouts used

I have a very simple application. It contains a red view and a green label inside it. There is also a button that runs animation. I animate the red view by changing its width constraint constant's value.

[self.view layoutIfNeeded];
[UIView animateWithDuration:0.4 animations:^{
    self.widthConstraint.constant = width;
    [self.view layoutIfNeeded];
}];

The red view animates as expected, but the green label inside changes its width instantly. I played with different animation flags, tried several tips from SO but did not bring it work.

Please find the sources here.

TIA

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It seems like I've found the root of the problem. Constraints are not a trouble, UILabel is.

If you replace UILabel in my project with simple UIView it will animate smoothly as expected. It's a question of UILabel content animation. You can reach correct animation behaviour set label.contentMode = UIViewContentModeCenter but it looks ugly.

The best way to solve this problem - not try to change size of UILabel during animation. Try to invent some other behaviour. In my case I change UILabel's alpha to 0 and x position to greater then container view's width.

Please find more details about UILabel animating here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...