imateWithDuration和UIProgressView

imateWithDuration和UIProgressView

本文介绍了UIView animateWithDuration和UIProgressView setProgress的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在10秒内将我的 UIProgressView 动画从0设置为1。

I want to animate my UIProgressView progression from 0 to 1 during 10 seconds.

代码:

[UIView animateWithDuration:10.0 animations:^{
    [_myProgressView setProgress:1 animated:YES];
} completion:(BOOL finished)^{
    if (finished) NSLog(@"animation finished);
}];

动画工作正常,但完成后总会立即调用 NSLog

Animation is working fine except that completion and NSLog are always called instantly.

我试过 animateWithDuration:withDelay:但延迟没有得到尊重并立即执行。

I tried animateWithDuration: withDelay: but the delay is not respected and executed immediately.

有没有人遇到同样的问题?

Has anyone encountered the same problem?

感谢您的帮助。

推荐答案

这是一个老问题,但我仍然在这里发布解决方案。解决方案结果非常简单。
所有你需要做的是:

Its an old problem but I am still posting the solution here. The solution turned out to be really simple.All you need to do is:

[_myProgressView setProgress:1];
[UIView animateWithDuration:10.0 animations:^{
    [_myProgressView layoutIfNeeded];
} completion:^(BOOL finished){
    if (finished) NSLog(@"animation finished");
}];

这是一个很好的解释,为什么事情工作不正确你:

Here is a good explanation for why things were working incorrectly for you:http://blog.spacemanlabs.com/2012/08/premature-completion-an-embarrassing-problem/

所以你不能使用 setProgress:animated :为进度视图设置动画并获得完成块所需行为的方法。但是,只是在块中设置 progress 将不会为其设置动画,因为 progress 不是可动画的属性。

So you can not use setProgress:animated: method to animate the progress view and get your desired behaviour of completion block as well. However, just setting progress inside the block will not animate it, since progress is not an animatable property.

请参阅苹果文档,其中说明该物业必须具有动画效果:

Refer to apple documentation which says its necessary for the property to be animatable:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/clm/UIView/animateWithDuration:animations:

所以你只需要更新 progress animate块之外的属性,并在animate块内部进行视图布局。

So you just update the value of progress property outside the animate block and do the layout of the view inside the animate block.

这篇关于UIView animateWithDuration和UIProgressView setProgress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 03:30