简体   繁体   English

使 Swift 中的计时器无效

[英]Invalidate Timer in Swift

I am attempting to create an app in Xcode.我正在尝试在 Xcode 中创建一个应用程序。 I have a screen that switches the label every 30 seconds and that after x number of minutes, will switch to a screen that says Done!.我有一个屏幕,每 30 秒切换一次标签,在 x 分钟后,将切换到一个显示完成的屏幕! There is also a return button to the original view.还有一个返回原始视图的按钮。 Currently, it freezes after completing x minutes and doesn't switch to the Done!目前,它在完成 x 分钟后冻结并且不会切换到完成! screen.屏幕。 Additionally, if I return to the main view and then attempt to generate exercises again, the timer will count down by 2 seconds instead of 1.此外,如果我返回主视图然后再次尝试生成练习,计时器将倒计时 2 秒而不是 1。

Here is my code.这是我的代码。 Any tips would be appreciated!任何提示将不胜感激!

import UIKit

var overallTime = minutes * 60
var timeLeft = 30
let resetTime = 30
var newE = true
var over = false

class ExerciseViewController: UIViewController {


    @IBOutlet weak var time: UILabel!
    @IBOutlet weak var progress: UIProgressView!
    @IBOutlet weak var currExercise: UILabel!

    let exercises = ["crunches", "bicycles", "leg lifts", "left crunches", "right crunches", "left cross crunches", "right cross crunches", "plank",
    "penguins", "wipers", "scissors", "flutter kicks", "right leg climbs", "left leg climbs", "sit ups", "dead bugs", "russian twists", "toe touches",
    "leg lifts with butt bump", "left plank", "right plank", "back plank", "alternating side sit-ups", "butterfly sit ups", "c-curve"]

    var num = 0

    var total = minutes * 2

    var timer: Timer?

    var bigtimer: Timer?

    override func viewDidLoad() {
        super.viewDidLoad()
        resetView()
        // Do any additional setup after loading the view.
        pickExercise()
        self.createTimer()
    }

    func pickExercise() {
        let index = Int.random(in: 0 ..< exercises.count)
        let curr = exercises[index]
        currExercise.text = String(curr)
        updateProgress()
        num += 1
    }

    func updateProgress() {
        let change: Float = Float(num) / Float(total)
        self.progress.progress = change
    }

    func resetView() {
        timer?.invalidate()
        bigtimer?.invalidate()
        timer = nil
        bigtimer = nil
        total = minutes * 2
        num = 0
        timeLeft = resetTime
        overallTime = minutes * 60
    }
}

extension ExerciseViewController {

    @objc func updateTimer() {
        if timeLeft <= 0 {
            timeLeft = resetTime
            time.textColor = UIColor.black
            pickExercise()
            if num > total {
                time.text = ""
            }
            else {
                time.text = String(timeLeft)
            }
        }
        else {
            timeLeft -= 1
            if timeLeft <= 10 {
                time.textColor = UIColor.red
            }
            if num > total {
                time.text = ""
            }
            else {
                time.text = String(timeLeft)
            }
        }

    }

    @objc func updateBigTimer() {
        if overallTime <= 0 {
            resetView()
            performSegue(withIdentifier: "Completed" , sender: self)
        }
        else {
            overallTime -= 1
        }
    }

    func createTimer() {
      // 1
      if timer == nil {
        // 2
        timer = Timer.scheduledTimer(timeInterval: 1.0,
                                     target: self,
                                     selector: #selector(updateTimer),
                                     userInfo: nil,
                                    repeats: true)
      }
        if bigtimer == nil {
            bigtimer = Timer.scheduledTimer(timeInterval: 1.0, target: self,
             selector: #selector(updateBigTimer),
             userInfo: nil,
            repeats: true)
        }
    }
}

is the below "timer = Timer..." intentional?下面的“计时器 = 计时器...”是故意的吗? or by mistake and it should be "bigtimer = Timer..." ?或者错误地应该是“bigtimer = Timer...”?

if bigtimer == nil {
        timer = Timer.scheduledTimer(timeInterval: 1.0, target: self,
         selector: #selector(updateBigTimer),
         userInfo: nil,
        repeats: true)
    }

because initializing "timer" the second time, it will remove the reference of the first one, but it will keep the first timer running with no reference.因为第二次初始化“计时器”,它将删除第一个计时器的引用,但它会保持第一个计时器在没有引用的情况下运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM