2014-05-22 58 views
12

我想動畫我的UIProgressView在10秒內從0進步到1。UIView animateWithDuration和UIProgressView setProgress

代碼:除了完成

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

動畫工作正常和NSLog總是立即調用。我想嘗試animateWithDuration: withDelay:但延遲不被尊重並立即執行。

有沒有人遇到同樣的問題?

感謝您的幫助。

+1

我也面臨同樣的問題,我還沒有找到一些解決方案,以一種好的方式來激發進步。你有沒有發現任何不落後的解決方案? – EmilDo

+0

和Emilio一樣。你有一個很好的解決方案 – osrl

+0

http:// seanallen。co/posts/animated-progress-bar – iDev750

回答

0

我發現的是一組UIview動畫和進度視圖動畫,它們工作得更好,並順利地爲progressview生成動畫。如果您只是使用動畫和progressview動畫的組合,則會直接觸發它,而不考慮UIview動畫計時器。

-(void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
    timer = [NSTimer scheduledTimerWithTimeInterval: 1.0f 
              target: self 
              selector: @selector(updateTimer) 
              userInfo: nil 
              repeats: YES]; 
} 


- (void)updateTimer 
{ 
    if (progressView.progress >= 1.0) { 
     [timer invalidate]; 
    } 
    [UIView animateWithDuration:1 animations:^{ 
     float newProgress = [self.progressView progress] + 0.125; 
     [self.progressView setProgress:newProgress animated:YES]; 
    }]; 
} 

隨意調整動畫時間,甚至更好,平穩過渡

+0

用於原型應用的很好的示例 –

1

這是一個老問題,但我也有類似的問題,這裏是我的解決方案(對不起它在斯威夫特)。完成部分在動畫結束時被調用。它位於一個自定義的UIProgressView類中。

override func setProgress(progress: Float, animated: Bool) { 

    self.progress = progress 
    if animated { 
     let duration: NSTimeInterval = 10.0 
     UIView.animateWithDuration(duration, delay: 0.0, options: UIViewAnimationOptions.CurveLinear, animations: { 
      self.layoutIfNeeded() 
      }, completion: { (completed) in 
       self.animationDuration = nil 
     }) 
    } else { 
     self.layoutIfNeeded() 
    } 
} 
3

使簡單而不是使用的NSTimer一樣,

[progressview setProgress:0 animated:NO]; 
NSTimer *t=[NSTimer scheduledTimerWithTimeInterval: 1.0f 
             target: self 
             selector: @selector(progressupdate) 
             userInfo: nil 
             repeats: YES]; 

以下是更新進步的功能,

-(void)progressupdate{ 
    if(progressview.progress<1){ 
     [progressview setProgress:(progressview.progress+=0.2) animated:YES]; 
    } 
    else{ 
     [t invalidate]; 
    } 
} 

希望它可以幫助....

21

它的一個老問題,但我仍然在這裏發佈解決方案。解決方案結果非常簡單。 所有你需要做的是:

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

這裏是爲什麼事情是你正確的工作一個很好的解釋: http://blog.spacemanlabs.com/2012/08/premature-completion-an-embarrassing-problem/

所以你不能用setProgress:animated:方法進行動畫處理的進度視圖,讓你的completion塊的期望行爲。但是,只要在塊內設置progress,就不會生成動畫,因爲progress不是動畫屬性。

請參考蘋果文檔它說其必要的屬性爲動畫: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/clm/UIView/animateWithDuration:animations

所以你只需更新動畫塊外progress屬性的值,做視圖的佈局動畫塊內。

+1

這應該標記爲正確的答案。 – MrBr

+0

非常好。對於這種情況,您可能想要使用線性時序曲線'[UIView animateWithDuration:10.0 delay:0 options:UIViewAnimationOptionCurveLinear animations:^ {' – CupawnTae

+0

正確答案,使用'UIView.animate(:)'動畫動畫是錯誤的,不會在最後重置動畫層。 –

相關問題