简体   繁体   English

Swift 进度条completedUnitCount和totalUnitCount

[英]Swift progress bar completedUnitCount and totalUnitCount

I don't know if I understood completedUnitCount and totalUnitCount well, I will describe you my problem.我不知道我是否理解 completedUnitCount 和 totalUnitCount,我会向您描述我的问题。

Declaration of progress bar:进度条声明:

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

This '5' makes no sense but I put it just to test filling of progress bar.这个“5”没有意义,但我只是为了测试进度条的填充。

I want to when I click on button below, to make that button disappear and make progressbar appear on button's place.我想当我点击下面的按钮时,使该按钮消失并使进度条出现在按钮的位置。 By default, progressbar is hidden and button is not.默认情况下,进度条是隐藏的,按钮不是。
It works well, but problem is because I want to dismiss current VC when completedUnitCount is equal to totalUnitCount.它运行良好,但问题是因为我想在 completedUnitCount 等于 totalUnitCount 时关闭当前 VC。
When I select 5 cardsets, my completedUnitCount is 5 so it is equal to totalUnitCount.当我 select 5 个卡片组时,我的 completedUnitCount 是 5,所以它等于 totalUnitCount。 It starts to fill progressBar and dismisses VC before it is fulfilled.它开始填充 progressBar 并在完成之前关闭 VC。
Here is my code, I think you don't have to pay attention on Realm stuff, and cardsets in just an array loaded from API and selected ones should be posted in realmTable and it works OK:这是我的代码,我认为你不必关注 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)
    }
}

Because the progress bar animates the changes it takes time to reach the end of the progress bar after you call由于进度条会为更改设置动画,因此调用后到达进度条的末尾需要时间

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

The common practice is to delay the processing of related animation (that should be done after progress bar completed its animation).通常的做法是延迟相关动画的处理(应该在进度条完成其动画后进行)。 My experience says that most of the iOS animations take 0.3 seconds, so just delay what you need:我的经验是大多数 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()
    })
}

I just tested your code without the realm part, and it worked for me.我只是在没有领域部分的情况下测试了您的代码,它对我有用。 I believe the problem is that your progress.completedUnitCount never reaches or it goes larger than the expected progress.totalUnitCount , probably because of the if statement and the number of cards in your loop.我认为问题在于您的progress.completedUnitCount永远不会达到或大于预期的progress.totalUnitCount ,这可能是因为 if 语句和循环中的卡片数量。

Try changing the if statement to:尝试将 if 语句更改为:

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

If that does not work, then the value of progress.completedUnitCount never reaches the progress.totalUnitCount .如果这不起作用,则progress.completedUnitCount的值永远不会达到progress.totalUnitCount

for calculate the progress bar to fit any number you want to add: for example you have 10 questions and user answer them.用于计算进度条以适合您要添加的任何数字:例如,您有 10 个问题,用户回答它们。 you have to check how many questions you have and user in which questions: you can use this logic to calculate the progress bar for example :您必须检查您有多少问题以及用户在哪些问题中:您可以使用此逻辑来计算进度条,例如:

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

here (quize.count) is the number of the questions we have这里(quize.count)是我们有问题的数量
and which mean (total questions number).这意味着(总问题数)。 and the (questionsNumber) is user in which questions.而 (questionsNumber) 是用户在哪个问题中。 so to you fit your progress bar your 1000 card of the total card number and you have to make another variable to check user in which card and the result must in float because the progress bar value is between 0 to 1所以为了让你的进度条适合你的 1000 张卡的总卡数,你必须制作另一个变量来检查用户在哪张卡中并且结果必须浮动,因为进度条值在 0 到 1 之间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM