繁体   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