2017-06-06 39 views
-1

當爲ProgressView使用動畫時,如果我讓動畫用完,但如果我嘗試再次運行此代碼,則它工作得很好在動畫仍在繼續時,它將100%添加到當前酒吧並通過酒吧。圖像應該更合理地解釋情況。Swift Progress視圖動畫使酒吧進一步超過100%

進展開始於1.0(100%)

Progress is half-way through
The code was ran again, resulting in the progress going above 100%, although it uses the correct amount of time to finish.

這裏使用的代碼:

  self.progressView.setProgress(1.0, animated: false) 

     DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { 
      UIView.animate(withDuration: 10, delay: 0, options: [.beginFromCurrentState, .allowUserInteraction], animations: { [unowned self] in 
       self.progressView.setProgress(0, animated: true) 
      }) 
     } 

提前感謝!

+0

是否有可能在你正在運行的代碼的其他部分(雖然設置下一個問題)的東西是無意中更改progressView框架或約束? – DonMag

回答

2

一些快速研究......

原來UIProgressView需要一些特別的努力,停止當前的動畫。看看這是否適合你:

// stop any current animation 
    self.progressView.layer.sublayers?.forEach { $0.removeAllAnimations() } 

    // reset progressView to 100% 
    self.progressView.setProgress(1.0, animated: false) 

    DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { 
     // set progressView to 0%, with animated set to false 
     self.progressView.setProgress(0.0, animated: false) 
     // 10-second animation changing from 100% to 0% 
     UIView.animate(withDuration: 10, delay: 0, options: [], animations: { [unowned self] in 
      self.progressView.layoutIfNeeded() 
     }) 
    } 
0

setProgress(_:animated:)方法已經爲您處理動畫。當animated參數設置爲true時,進度更改將動畫化。

嘗試沒有動畫塊:

self.progressView.setProgress(1.0, animated: false) 

DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { 
    self.progressView.setProgress(0.0, animated: true) 
} 

同時確保這不是一個自動佈局問題。檢查進度視圖上的約束,並確保其寬度被正確約束。