Home > Net >  how can I add multiple progress views to one timer so they all have separate times and when one goes
how can I add multiple progress views to one timer so they all have separate times and when one goes

Time:01-04

//here is all of the code that I am using for this project
import UIKit
import AVFoundation

class ViewController: UIViewController {

    override func viewDidLoad() {
       super.viewDidLoad()
        // Do any additional setup after loading the view.
    

    
    }

    @IBAction func btnPressed4(_ sender: Any) {
        
            let currentDateTime = Date()
           let formatter = DateFormatter()
           formatter.timeStyle = .short
          let dateTimeString = formatter.string(from: currentDateTime)
        timePrint4.text = dateTimeString
        btnPressed4.titleLabel?.textColor = UIColor.white
    }
    
    
    
    @IBAction func btnPressed3(_ sender: Any) {
        
            let currentDateTime = Date()
           let formatter = DateFormatter()
           formatter.timeStyle = .short
          let dateTimeString = formatter.string(from: currentDateTime)
        timePrint3.text = dateTimeString
        btnPressed3.titleLabel?.textColor = UIColor.white
    }
    
    
    
    
    @IBAction func btnPressed2(_ sender: UIButton) {
    
        let currentDateTime = Date()
       let formatter = DateFormatter()
       formatter.timeStyle = .short
      let dateTimeString = formatter.string(from: currentDateTime)
        timePrint2.text = dateTimeString
        btnPressed2.titleLabel?.textColor = UIColor.green
    }
    
    
    
    
    
    
    
    @IBAction func btnPressed1(_ sender: UIButton) {
         let currentDateTime = Date()
        let formatter = DateFormatter()
        formatter.timeStyle = .short
       let dateTimeString = formatter.string(from: currentDateTime)
    timePrint1.text = dateTimeString
        
    }
    

 

    @IBOutlet weak var btnPressed1: UIButton!
    @IBOutlet weak var btnPressed2: UIButton!
    @IBOutlet weak var btnPressed3: UIButton!
    @IBOutlet weak var btnPressed4: UIButton!
    
    
    @IBOutlet weak var timePrint1: UILabel!
    @IBOutlet weak var timePrint2: UILabel!
    @IBOutlet weak var timePrint4: UILabel!
    
    @IBOutlet weak var timePrint3: UILabel!
    
    
  
    

    @IBOutlet weak var titleLabel: UILabel!
    
    @IBOutlet weak var progressBar1: UIProgressView!
    
    @IBOutlet weak var progressBar2: UIProgressView!
    
    @IBOutlet weak var progressBar3: UIProgressView!
    
    @IBOutlet weak var progressBar4: UIProgressView!
    
    
     let start = 5
    var timer = Timer()
    var player: AVAudioPlayer!
    var totalTime = 0
    var secondsPassed = 0
    
    @IBAction func startButtonPressed(_ sender: UIButton) {
    
   
        let startB = sender.titleLabel?.text
        totalTime = start
           
        
        
        progressBar1.progress = 0.0
        secondsPassed = 0
        titleLabel.text = "coffee timer"
        btnPressed1.titleLabel?.textColor = UIColor.white
        timer = Timer.scheduledTimer(timeInterval: 1.0, target:self, selector: #selector(updateTimer), userInfo:nil, repeats: true)
    }
    
    @objc func updateTimer() {
        if secondsPassed < totalTime {
            secondsPassed  = 1
            progressBar1.progress = Float(secondsPassed) / Float(totalTime)
            print(Float(secondsPassed) / Float(totalTime))
        } else {
            timer.invalidate()
            titleLabel.text = "check coffee"
            btnPressed1.titleLabel?.textColor = UIColor.green
           
            let url = Bundle.main.url(forResource: "alarm_sound", withExtension: "mp3")
            player = try! AVAudioPlayer(contentsOf: url!)
            player.play()
            
        
         
        
        }
               
    
    }
}
//I have four progress views and when the first ends a alarm goes off and then the next starts eminently and the goes to the next and then to the fourth one were the timer ends after that

CodePudding user response:

There's lots of different ways to handle this. Here is an outline of a possible approach:

Have an instance variable currentProgress, of type Int? If it's nil, none of your progress indicators is running.

Have an instance var stepCompletedInterval: Double

Create a struct to hold the different views you use to manage a progress step:

struct ProgressInfo {
   let aButton: UIButton
   let aLabel: UILabel
   let aProgressIndicator: UIProgressIndicator
   let secondsForThisStep: Int
}

Create an array of ProgressInfo, (let's call it progressInfoArray) and populate it with the buttons, labels, and progress indicators you have above. Also give each entry a value for secondsForThisStep.

When the user starts the process, set currentProgress to 0 (The first progress indicator.)

Now, for each step, set a stepCompletedInterval to the current time plus progressInfoArray[currentProgress].secondsForThisStep (The time at which the current step should complete.

Also start a 1-second repeating timer. Each time the timer fires, check to see if the current time is greater than stepCompletedInterval. If it is, increment currentProgress if there are still more steps to complete, and repeat the "now for each step" part above.

That is a rough outline of how you might go about it. I'm not completely clear on what your start buttons and btnPressed1 to btnPressed4 are supposed to do, so I'll leave that for you.

You'll need to adapt the approach I've outlined to your needs. It isn't code, it's an approach. I'm not going to give you code.

  •  Tags:  
  • Related