簡體   English   中英

Swift 進度條completedUnitCount和totalUnitCount

[英]Swift progress bar completedUnitCount and totalUnitCount

我不知道我是否理解 completedUnitCount 和 totalUnitCount,我會向您描述我的問題。

進度條聲明:

@IBOutlet weak var progressBar: UIProgressView!
let progress = Progress(totalUnitCount: 5)

這個“5”沒有意義,但我只是為了測試進度條的填充。

我想當我點擊下面的按鈕時,使該按鈕消失並使進度條出現在按鈕的位置。 默認情況下,進度條是隱藏的,按鈕不是。
它運行良好,但問題是因為我想在 completedUnitCount 等於 totalUnitCount 時關閉當前 VC。
當我 select 5 個卡片組時,我的 completedUnitCount 是 5,所以它等於 totalUnitCount。 它開始填充 progressBar 並在完成之前關閉 VC。
這是我的代碼,我認為你不必關注 Realm 的東西,並且只是從 API 加載的數組中的卡片集,並且選擇的卡片集應該發布在 realmTable 中並且它工作正常:

@IBAction func btnDownload(_ sender: Any) {
    print("button download pressed")

    let realm = try! Realm()

    for id in cardsetIds {
        for cardset in cardsets {
            if id == cardset.id && cardset.cards != "0" {
                let newSet = CardsetTable()
                newSet.cards = cardset.cards
                newSet.size = cardset.size
                newSet.title = cardset.title
                newSet.id = cardset.id

                try? realm.write { realm.add(newSet) }

                btnDownload.isHidden = true
                progressBar.isHidden = false

                progress.completedUnitCount += 1
                let progressFloat = Float(self.progress.fractionCompleted)

                self.progressBar.setProgress(progressFloat, animated: true)
            }
        }
    }
    if progress.completedUnitCount == progress.totalUnitCount {

        self.dismiss(animated: true, completion: nil)
    }
}

由於進度條會為更改設置動畫,因此調用后到達進度條的末尾需要時間

self.progressBar.setProgress(progressFloat, animated: true)

通常的做法是延遲相關動畫的處理(應該在進度條完成其動畫后進行)。 我的經驗是大多數 iOS 動畫需要 0.3 秒,所以只需延遲你需要的:

if progress.completedUnitCount >= progress.totalUnitCount {
    delay(0.3) { // delay to let progress bar to complete the animation
        self.dismiss(animated: true, completion: nil)
    }
}


/// Delays given callback invocation 
///
/// - Parameters:
///   - delay: the delay in seconds
///   - callback: the callback to invoke after 'delay' seconds
func delay(_ delay: TimeInterval, callback: @escaping ()->()) {
    let delay = delay * Double(NSEC_PER_SEC)
    let popTime = DispatchTime.now() + Double(Int64(delay)) / Double(NSEC_PER_SEC);
    DispatchQueue.main.asyncAfter(deadline: popTime, execute: {
        callback()
    })
}

我只是在沒有領域部分的情況下測試了您的代碼,它對我有用。 我認為問題在於您的progress.completedUnitCount永遠不會達到或大於預期的progress.totalUnitCount ,這可能是因為 if 語句和循環中的卡片數量。

嘗試將 if 語句更改為:

if progress.completedUnitCount >= progress.totalUnitCount {
    self.dismiss(animated: true, completion: nil)
}

如果這不起作用,則progress.completedUnitCount的值永遠不會達到progress.totalUnitCount

用於計算進度條以適合您要添加的任何數字:例如,您有 10 個問題,用戶回答它們。 您必須檢查您有多少問題以及用戶在哪些問題中:您可以使用此邏輯來計算進度條,例如:

func getProgress() -> Float{
    return Float(questionsNumber + 1) / Float(quize.count)
}

這里(quize.count)是我們有問題的數量
這意味着(總問題數)。 而 (questionsNumber) 是用戶在哪個問題中。 所以為了讓你的進度條適合你的 1000 張卡的總卡數,你必須制作另一個變量來檢查用戶在哪張卡中並且結果必須浮動,因為進度條值在 0 到 1 之間

暫無
暫無

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

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