2

I am playing around to learn and implemented a progressView that is triggered using a function and reset using another one when buttons are clicked (and feed it with value). For some reason, after the first click, it works fine even though the functions are called also on the following clicks and the progressView is reset, its animation is not restarting (checked functions and values are all correct using 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()
            }
        }
        
    
    }
    
}
MLavoie
  • 9,671
  • 41
  • 36
  • 56
Meir
  • 21
  • 5
  • Seems like it all works, but the first layer of animation is hiding the new one ignoring the reset. I have done some reading and found these two functions (none worked): self.progressView.setNeedsLayout() AND layoutIfNeeded() – Meir Jan 19 '20 at 02:11
  • what do you mean by hiding? it looks ok – Vina Jul 02 '20 at 09:40

0 Answers0