簡體   English   中英

Swift - USTrogress的UIProgressView不順暢

[英]Swift - UIProgressView is not smooth with NSTimer

所以我使用NSTimer讓用戶知道應用程序正在運行。 進度條設置為持續3秒,但在運行時,它以“滴答”運動顯示,並且它不應該像應該的那樣平滑。 無論如何我可以讓它更順利 - 我確信我的計算錯誤....

如果有人可以看看那將是偉大的。 這是代碼:

import UIKit

class LoadingScreen: UIViewController {


    var time : Float = 0.0
    var timer: NSTimer?

    @IBOutlet weak var progressView: UIProgressView!


    override func viewDidLoad() {
        super.viewDidLoad()

// Do stuff

timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector:Selector("setProgress"), userInfo: nil, repeats: true)

}//close viewDidLoad

  func setProgress() {
        time += 0.1
        progressView.progress = time / 3
        if time >= 3 {
            timer!.invalidate()
        }
    }

}

編輯:一個簡單的3秒UIView動畫(推薦)

如果您的欄只是平滑移動以指示活動,可以考慮使用UIActivityIndicatorView或自定義UIView動畫:

override func viewDidAppear(animated: Bool)
{
    super.viewDidAppear(animated)

    UIView.animateWithDuration(3, animations: { () -> Void in
        self.progressView.setProgress(1.0, animated: true)
    })
}

確保您的progressView進度設置為零開始。 這將導致進度的平滑3秒動畫。

簡單的動畫進度(工作但仍然跳躍一點)

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIProgressView_Class/#//apple_ref/occ/instm/UIProgressView/setProgress:animated :

func setProgress() {
    time += 0.1
    progressView.setProgress(time / 3, animated: true)
    if time >= 3 {
        timer!.invalidate()
    }
}

間隔較小的選項。 (不建議)

將計時器設置為較小的間隔:

timer = NSTimer.scheduledTimerWithTimeInterval(0.001, target: self, selector:Selector("setProgress"), userInfo: nil, repeats: true)

然后更新你的功能

func setProgress() {
    time += 0.001
    progressView.setProgress(time / 3, animated: true)
    if time >= 3 {
        timer!.invalidate()
    }
}

很難確切地說出問題所在。 如果你在setProgress中打印一行打印時間戳,我想查看輸出。 它實際上每十分之一秒發射一次嗎? 我的猜測是不是。

為什么不? 好吧,計時器在主線程中調度運行循環任務以執行setProgress中的代碼。 在隊列中的任務執行之前,此任務無法運行。 因此,如果您的主線程中發生了長時間運行的任務,您的計時器將非常不精確地觸發。 我的第一個建議是,這可能是正在發生的事情。

這是一個例子:

  1. 你開始計時器每秒做一些事情。
  2. 緊接着,您啟動一​​個長時間運行的主線程任務(例如,您嘗試將大量數據寫入文件)。 此任務將需要五秒鍾才能完成。
  3. 你的計時器想要在一秒鍾之后開火,但你的文件寫入會在接下來的四秒鍾內占用主線程,所以計時器不能再開火四秒鍾。

如果是這種情況,那么要解決問題,您需要將主線程工作移動到后台線程,或者在定期返回運行循環時找出一種方法。 例如,在長時間運行的主線程操作期間,您可以定期在運行循環上調用runUntilDate以允許其他運行循環任務執行。

請注意,在長時間運行的主線程任務期間,您不能僅定期增加進度條填充,因為在返回到運行循環之前,進度條實際上不會為其填充設置動畫。

如何動畫更改的正確方法: animateWithDuration:動畫:CABasicAnimation 您可以使用它來創建流暢的動畫

繼續裝載機

timer = Timer.scheduledTimer(timeInterval: 0.001, target: self, selector: #selector(setProgress), userInfo: nil, repeats: true)

func setProgress() {
        time += 0.001
        downloadProgressBar.setProgress(time / 3, animated: true)
        if time >= 3 {
            self.time = 0.001
            downloadProgressBar.progress = 0
            let color = self.downloadProgressBar.progressTintColor
            self.downloadProgressBar.progressTintColor = self.downloadProgressBar.trackTintColor
            self.downloadProgressBar.trackTintColor = color
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM