如何让进度条在iOS应用中正常工作?

wko9yo5t  于 2022-12-15  发布在  iOS
关注(0)|答案(1)|浏览(87)

我在Swift中使用以下代码创建了一个计时器:

import UIKit

class ViewController: UIViewController {
  //  @IBOutlet var ProgressBar: UIProgressView!
    @IBOutlet var label: UILabel!
    
    var hours: Int = 0
    var mins: Int = 0
    var secs: Int = 0
        
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //ProgressBar.progress = 0.0
    }

    @IBAction func didTapAddButton() {
       // var progress: Float = 0.0
        //ProgressBar.progress = progress
    
        let vc = storyboard?.instantiateViewController(identifier:"date_picker") as! DateViewController
        vc.title = "New Event"
        vc.completionHandler = { [weak self] name, date in
            DispatchQueue.main.async {
                self?.didCreateEvent(name: name, targetDate: date)
            }
        }
        navigationController?.pushViewController(vc, animated: true)
    }

    private func didCreateEvent(name: String, targetDate: Date){
        self.title = name
        let difference = floor(targetDate.timeIntervalSince(Date()))
        if difference > 0.0 {
            let computedHours: Int = Int(difference) / 3600
            let remainder: Int = Int(difference) - (computedHours * 3600)
            let minutes: Int = remainder / 60
            let seconds: Int = Int(difference) - (computedHours * 3600) - (minutes * 60)
           
            print("\(computedHours) \(minutes) \(seconds)")
            hours = computedHours
            mins = minutes
            secs = seconds
            
            updateLabel()
            
            startTimer()
        }
        else{
            print("negative interval")
        }
    }

    private func startTimer() {
        Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { _ in
            if self.secs > 0 {
                self.secs = self.secs - 1
            }
            else if self.mins > 0 && self.secs == 0 {
                self.mins = self.mins - 1
                self.secs = 59
            }
            else if self.hours > 0 && self.mins == 0 && self.secs == 0 {
                self.hours = self.hours - 1
                self.mins = 59
                self.secs = 59
            }
            
            self.updateLabel()
        })
    }

    private func updateLabel() {
        label.text = "\(hours):\(mins):\(secs)"
    }
}

我正在尝试制作一个进度条来显示剩余的时间。我希望进度条在没有剩余时间的时候是满的。我应该怎么做呢?
我试着搜索如何做到这一点,但谷歌是没有帮助的。

yvfmudvl

yvfmudvl1#

所以你要做的第一件事就是把所有的hourminutes,和seconds的值合并成一个奇异的组合值,表示为totalSeconds或其他形式。

var hours: Int = XXX
var minutes: Int = XXX
var seconds: Int = XXX
var totalSeconds: Int = 0
totalSeconds = getTotalSecondsFrom(hours, minutes, seconds)

func getTotalSecondsFrom(hours: Int, minutes: Int, seconds: Int) -> Int {
    let hoursToSec = (hours * 60) * 60
    let minutesToSec = minutes * 60
    return hoursToSec + minutesToSec + seconds
}

一旦你把所有的值都归结为一个奇异值,现在你就有了一个计算进度条最大限制的表示。记住,进度条只能有0.01.0之间的值,这是percentage计算的完美选择。为了用一种对学习有意义的方式来解释,让我们从另一个Angular 来看这个问题,我们如何从总最大值中得到percentage,然后将其Map到0.0 -> 1.0范围内的值?

func getCompletedPercent(currentSeconds: Int, totalSeconds: Int) -> Float {
    return Float(currentSeconds) / Float(totalSeconds)
}

为什么会这样呢?假设总秒数为5000,电流为500,计算一下,500 / 5000 = 0.1等于1/10th10%等于5000,这正是我们要找的掌握了这些知识,此时您需要做的就是将进度条最大值设置为1.0,然后在计时器中每隔一秒,使用函数示例getTotalSecondsFrom(...)getCompletedPercent(...)更新当前进度。当然,您的版本可能会略有不同,但概念是相同的。

相关问题