UIView 动画发生的速度比指定的秒数快
UIView Animation happens faster than specified seconds
我有一张图片的视图,其中显示了一些文字。
我需要执行逐字显示的动画,最后必须显示完整的文本。
我的界面中有一个徽标视图,上面有一个动画视图。对于动画视图,前导、尾随、顶部和底部约束是基于 superView(徽标视图)设置的。
我尝试了以下代码,但我指定的持续时间无法正常工作。
- (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?
您应该在 动画块之前 在动画视图的超级视图上调用 layoutIfNeeded
,并在 animations:
块本身中调用。第一次调用确保应用任何布局更改。假设 logoView
是 animateView
的超视图:
- (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");
}];
}
最后,我得到了基于@stevekohls 评论的解决方案
- (void) animateLogo {
self.animateViewLeadingConstraint.constant = 0;
[UIView animateWithDuration:5.0f animations:^{
self.animateViewLeadingConstraint.constant = 94.0;
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.5f animations:^{
self.animateView.alpha = 0;
} completion:^(BOOL finished) {
self.animateViewLeadingConstraint.constant = 0;
[self.view layoutIfNeeded];
self.animateView.alpha = 1.0f;
[self animateLogo];
}];
}];
}
我有一张图片的视图,其中显示了一些文字。 我需要执行逐字显示的动画,最后必须显示完整的文本。
我的界面中有一个徽标视图,上面有一个动画视图。对于动画视图,前导、尾随、顶部和底部约束是基于 superView(徽标视图)设置的。
我尝试了以下代码,但我指定的持续时间无法正常工作。
- (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?
您应该在 动画块之前 在动画视图的超级视图上调用 layoutIfNeeded
,并在 animations:
块本身中调用。第一次调用确保应用任何布局更改。假设 logoView
是 animateView
的超视图:
- (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");
}];
}
最后,我得到了基于@stevekohls 评论的解决方案
- (void) animateLogo {
self.animateViewLeadingConstraint.constant = 0;
[UIView animateWithDuration:5.0f animations:^{
self.animateViewLeadingConstraint.constant = 94.0;
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.5f animations:^{
self.animateView.alpha = 0;
} completion:^(BOOL finished) {
self.animateViewLeadingConstraint.constant = 0;
[self.view layoutIfNeeded];
self.animateView.alpha = 1.0f;
[self animateLogo];
}];
}];
}