How can I make a countdown with NSTimer?

后端 未结 15 1573
遇见更好的自我
遇见更好的自我 2020-11-30 03:17

How can I make a countdown with an NSTimer using Swift?

相关标签:
15条回答
  • 2020-11-30 03:39

    Swift4

        @IBOutlet weak var actionButton: UIButton!
        @IBOutlet weak var timeLabel: UILabel!
        var timer:Timer?
        var timeLeft = 60
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupTimer()
    }
    
    func setupTimer() {
        timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(onTimerFires), userInfo: nil, repeats: true)
    }
    
    @objc func onTimerFires() {
        timeLeft -= 1
        timeLabel.text = "\(timeLeft) seconds left"
    
        if timeLeft <= 0 {
            actionButton.isEnabled = true
            actionButton.setTitle("enabled", for: .normal)
            timer?.invalidate()
            timer = nil
        }
    }
    
    @IBAction func btnClicked(_ sender: UIButton) {
        print("API Fired")
    }
    
    0 讨论(0)
  • 2020-11-30 03:43

    In Swift 5.1 this will work:

    var counter = 30
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateCounter), userInfo: nil, repeats: true)
    }
    
    
    
    @objc func updateCounter() {
        //example functionality
        if counter > 0 {
            print("\(counter) seconds to the end of the world")
            counter -= 1
        }
    }
    
    0 讨论(0)
  • 2020-11-30 03:44

    Variable for your timer

    var timer = 60
    

    NSTimer with 1.0 as interval

    var clock = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "countdown", userInfo: nil, repeats: true)
    

    Here you can decrease the timer

    func countdown() {
        timer--
    }
    
    0 讨论(0)
  • 2020-11-30 03:46

    Question 1:

    @IBOutlet var countDownLabel: UILabel!
    
    var count = 10
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        var timer = Timer.scheduledTimer(timeInterval: 0.4, target: self, selector: #selector(UIMenuController.update), userInfo: nil, repeats: true)
    }
    
    func update() {
        if(count > 0) {
            countDownLabel.text = String(count--)
        }
    }
    

    Question 2:

    You can do both. SpriteKit is the SDK you use for scene, motion, etc. Simple View Application is the project template. They should not conflict

    0 讨论(0)
  • 2020-11-30 03:48

    Swift 4.1 and Swift 5. The updatetime method will called after every second and seconds will display on UIlabel.

         var timer: Timer?
         var totalTime = 60
        
         private func startOtpTimer() {
                self.totalTime = 60
                self.timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
            }
        
        @objc func updateTimer() {
                print(self.totalTime)
                self.lblTimer.text = self.timeFormatted(self.totalTime) // will show timer
                if totalTime != 0 {
                    totalTime -= 1  // decrease counter timer
                } else {
                    if let timer = self.timer { 
                        timer.invalidate()
                        self.timer = nil
                    }
                }
            }
        func timeFormatted(_ totalSeconds: Int) -> String {
            let seconds: Int = totalSeconds % 60
            let minutes: Int = (totalSeconds / 60) % 60
            return String(format: "%02d:%02d", minutes, seconds)
        }
    
    0 讨论(0)
  • 2020-11-30 03:48

    this for the now swift 5.0 and newst

    var secondsRemaining = 60
    
    
    Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateCounter), userInfo: nil, repeats: true)
    }
    @objc func updateCounter(){
        if secondsRemaining > 0 {
        print("\(secondsRemaining) seconds.")
        secondsRemaining -= 1
                }
            }
    
    0 讨论(0)
提交回复
热议问题