我有一个带有图像的 View ,其中显示了一些文本。 我需要执行一个逐个字符显示的动画,最后必须显示完整的文本。
我的界面中有一个 Logo View ,上面有一个动画 View 。对于动画 View ,前导、尾随、顶部和底部约束是基于 superView( Logo View )设置的。
我尝试了以下代码,但我指定的持续时间无法正常工作。
- (void) animateLogo {
[UIView animateWithDuration:10.0 animations:^{
NSLog(@"animation started");
if (self.animateViewLeadingConstraint.constant < 94) { //94 is the total width of the logo view
self.animateViewLeadingConstraint.constant = self.animateViewLeadingConstraint.constant + 1;
} else {
self.animateViewLeadingConstraint.constant = 0;
}
} completion:^(BOOL finished) {
NSLog(@"animation ended");
[self.animateView layoutIfNeeded];
[self animateLogo];
}];
}
我附上了持续时间的日志
2016-08-01 17:55:27.317 sample[20361:481531] animation started
2016-08-01 17:55:27.318 sample[20361:481531] animation ended
2016-08-01 17:55:27.318 sample[20361:481531] animation started
2016-08-01 17:55:27.318 sample[20361:481531] animation ended
2016-08-01 17:55:27.318 sample[20361:481531] animation started
2016-08-01 17:55:27.319 sample[20361:481531] animation ended
2016-08-01 17:55:27.319 sample[20361:481531] animation started
2016-08-01 17:55:27.319 sample[20361:481531] animation ended
我不确定我在上面的代码中犯了什么错误或者我误解了 UIView 动画的概念
如有任何帮助或提示,我们将不胜感激。
最佳答案
查看此答案:How do I animate constraint changes?
您应该在动画 block 之前调用动画 View 的 super View 上的layoutIfNeeded
,并在animations:
block 本身中调用。第一次调用确保应用任何布局更改。假设 logoView
是 animateView
的父 View :
- (void) animateLogo {
self.animateViewLeadingConstraint.constant = 0.0;
[self.logoView layoutIfNeeded];
[UIView animateWithDuration:10.0 animations:^{
NSLog(@"animation started");
self.animateViewLeadingConstraint.constant = 94.0;
[self.logoView layoutIfNeeded];
} completion:^(BOOL finished) {
NSLog(@"animation ended");
}];
}
关于ios - UIView 动画发生的速度比指定的秒数快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38698985/