Search code examples
swiftxcodeimageuiimageview

How to change image in UIimage view from counter


really struggling on how I would change the image every 2 minutes based on the counter/ timer I have created. I would like the UIImage view to display an image for 2 minutes and then switch to another image, then another image , then another image, based off my counter. Here is the counter code.

@objc func runTimer() {
    counter += 0.1
    // HH:MM:SS:
    let flooredCounter = Int(floor(counter))
    let hour = flooredCounter / 3600
    let minute = (flooredCounter % 3600) / 60
    var minuteString = "\(minute)"
    if minute < 10 {
        minuteString = "0\(minute)"
    }
    let second = (flooredCounter % 3600) % 60
    var secondString = "\(second)"
    if second < 10 {
        secondString = "0\(second)"
    }
    _ = String(format: "%.1f", counter).components(separatedBy: ".").last!

    timerLabel.text = "\(hour):\(minuteString):\(secondString)"

here is the start, pause and reset buttons

@IBAction func startWorkingAction(_ sender: Any)
{
    if !isTimerRunning {
        timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(runTimer), userInfo: nil, repeats: true)

        isTimerRunning = true

        resetButton.isEnabled = false
        resetButton.alpha = 0.2

        pauseButton.isEnabled = true
        pauseButton.alpha = 1.0

        startWorkingButton.isEnabled = false
        startWorkingButton.alpha = 0.2

        AudioServicesPlaySystemSound(1519)
    }

}
@IBAction func pauseAction(_ sender: Any)
{
    resetButton.isEnabled = true
    resetButton.alpha = 1.0

    startWorkingButton.isEnabled = true
    startWorkingButton.alpha = 1.0

    pauseButton.isEnabled = false
    pauseButton.alpha = 0.2

    isTimerRunning = false
    timer.invalidate()

    AudioServicesPlaySystemSound(1520)
}
@IBAction func resetAction(_ sender: Any)
{
    timer.invalidate()
    isTimerRunning = false
    counter = 0.0

    timerLabel.text = "0:00:00"

    resetButton.isEnabled = false
    resetButton.alpha = 0.0

    pauseButton.isEnabled = false
    pauseButton.alpha = 0.0

    startWorkingButton.isEnabled = true
    startWorkingButton.alpha = 1.0

    AudioServicesPlaySystemSound(1520)

}

and the outlet for the uiimage is

  @IBOutlet weak var treeGrow: UIImageView!

Any help would be greatly appreciated. Thank you!


Solution

  • This is actually quite easy to solve. This is how I use it inside my app:

    //MARK: ImagePreviewAnimation
    
    // timer for imagePreview
        var timer: Timer?
        var currentImage: UIImage?
        var currentImageIndex = 0
    
        func startImagePreviewAnimation(){
            timer = Timer.scheduledTimer(timeInterval: 1.6, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
        }
    
        @objc func timerAction(){
            currentImageIndex = (currentImageIndex + 1) % Constants.ImageList.images.count
            UIView.transition(with: self.imagePreview, duration: 0.5, options: .transitionCrossDissolve, animations: {
                self.imagePreview.image = Constants.ImageList.images[self.currentImageIndex]
                self.currentImage = self.imagePreview.image
            })
        }
    

    This changes the image every 1.6 seconds with a soft transition. An important thing to know is that you should call timer.invalidate() if you go to another ViewController.

    Constants.ImageList is my simply the list that contains all the images. With the trick of calling % the list always restarts when reaching the end.