首页 > 解决方案 > UIprogressView(progerssView)动画未重新启动

问题描述

我正在玩耍以学习和实现一个progressView使用函数触发的函数,并在单击按钮时使用另一个函数重置(并为其提供值)。出于某种原因,在第一次点击后,即使在随后的点击中也调用了函数并且progressView重置,它的动画没有重新启动(使用 print() 检查的函数和值都是正确的......)

import UIKit

class ViewController: UIViewController {  
    
    let eggTimes = ["Soft" : 5, "Medium" : 7, "Hard": 12]
    
    @IBOutlet weak var progressView: UIProgressView!
   
    //reset the progress bar
    func resetProgress(){
        progressView.setProgress(0, animated: true)
           print("function progress view reset fired")
       }
       
    //starts the prgress bar
    func startProgressView(duration: Double) {
        
        UIView.animate(withDuration: duration) {
            print("progress bar has been initiated")
            self.progressView.setProgress(1.0, animated: true)
        }
    }

    @IBAction func hardnessSelected(_ sender: UIButton) {
       
        resetProgress()
        print("progress bar has been reset")
        let hardness = sender.currentTitle!
        var timerCounter = eggTimes[hardness]! * 10
        let temp1: Double = Double(timerCounter)
        print("temp 1 = \(temp1) ")
        self.startProgressView(duration: temp1)
        print(eggTimes[hardness]!)
        
        let timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { (timer) in
           
            if timerCounter > 0 {
            timerCounter -= 1
           // print(timerCounter)
                
            }
            else {
                print("egg ready")
                timer.invalidate()
            }
        }
        
    
    }
    
}

标签: iosswiftswift5uiprogressview

解决方案


推荐阅读