Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计时器.fire()在Swift中无效后不工作_Swift_Nstimer - Fatal编程技术网

计时器.fire()在Swift中无效后不工作

计时器.fire()在Swift中无效后不工作,swift,nstimer,Swift,Nstimer,使用后 @IBAction func pauseButton(sender: AnyObject) { if isPaused == false { timer.invalidate() isPaused = true displayLabel.text = "\(count)" println("App is paused equals \(isPaused)") } else if isPaused == t

使用后

@IBAction func pauseButton(sender: AnyObject) {
    if isPaused == false {

        timer.invalidate()
        isPaused = true
        displayLabel.text = "\(count)"
        println("App is paused equals \(isPaused)")

    } else if isPaused == true {
        var isPaused = false
        timer.fire()
        //  timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)
    }
}
要暂停应用程序,我想再次点击暂停,在这种情况下,计时器将从停止的位置继续计数

此外,当我多次按“暂停/播放”时,会出现多个计时器实例,这将导致计时器每秒增加几次

请帮忙

//
//  ViewController.swift
//  Navigation Bars
//
//  Created by Alex Ngounou on 8/27/15.
//  Copyright (c) 2015 Alex Ngounou. All rights reserved.
//


import UIKit

class ViewController: UIViewController {

    var timer = NSTimer()
    var count = 0
    var isPaused = false

    func updateTime() {

        switch count {
        case 0, 1:
            count++
        println( "\(count) second.")
        displayLabel.text = "\(count)"

        case 2:
            count++
            println("\(count) seconds.")
            displayLabel.text = "\(count)"

        default:
            count++
            println("\(count) seconds.")
            displayLabel.text = "\(count)"

        }
    }




    **strong text**@IBAction func playButton(sender: AnyObject) {

        var isPaused = false

         timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)
    }


    @IBAction func stopButton(sender: AnyObject) {

        timer.invalidate()
        count = 0
        displayLabel.text = "0"
    }


    // if it's currently paused, pressing on the pause button again should restart the counter from where it originally left off.

    @IBAction func pauseButton(sender: AnyObject) {

        if isPaused == false {

        timer.invalidate()
        isPaused = true
        displayLabel.text = "\(count)"
        println("App is paused equals \(isPaused)")

        } else if isPaused == true {

            var isPaused = false

            timer.fire()

            //  timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)
        }
    }


    @IBAction func resetButton(sender: AnyObject) {

        timer.invalidate()
        count = 0
        displayLabel.text = ""
    }


    @IBOutlet weak var displayLabel: UILabel!



    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

    timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
发件人:

一旦失效,计时器对象就不能重用

因此,本质上,一旦失效,
NSTimer
将什么也不做。您的
timer
属性必须在该点之后分配给新构造的
NSTimer
对象,以使其再次启动。如果你的失效记账是准确的,就不存在多个计时器的“累积”问题

不过,解决实际问题的最简单方法可能是逻辑过滤。也就是说,将
NSTimer
对象无限期地保留在周围,并让它持续激发。当存储的属性
isPaused
true
时,您可以忽略计时器事件(通过立即从处理函数返回),否则您可以处理它们。

否则,“更懒”的方法可能有用:

class ViewController: UIViewController {

    var timer: Timer?

    override func viewDidLoad() {
        super.viewDidLoad()
        startTimer()
    }


    final func killTimer(){
        self.timer?.invalidate()
        self.timer = nil
    }


    final private func startTimer() {

        // make it re-entrant:
        // if timer is running, kill it and start from scratch
        self.killTimer()
        let fire = Date().addingTimeInterval(1)
        let deltaT : TimeInterval = 1.0

        self.timer = Timer(fire: fire, interval: deltaT, repeats: true, block: { (t: Timer) in

            print("hello")

        })

    RunLoop.main.add(self.timer!, forMode: RunLoopMode.commonModes)

    }

将计时器声明为“弱变量”,如下所示:

weak var timer: Timer?

我意识到这是一个两个问题,如果需要的话,我可以分别发布它们。请帮忙!你永远不应该将布尔类型与真类型进行比较。这是多余的。将if isPaused==false{..}更改为if!我的理由是{..}。将if isPaused==true{..}更改为if isPaused{..}。您也可以像下面这样在if语句中添加一行来切换isPaused值。i暂停=!虽然我没有这么做,但是如果比较bool和true/false有助于提高可读性,那也没什么错。然而,不正确的是,暂停按钮操作的else if中的本地声明“var isPaused=false”。这并没有改变全球的情况。。。但是创建另一个范围更有限的变量并将其设置为false,然后在elseif完成时销毁它。哇!“我更为震惊的是,我能真正理解你说的话,”赛尔夫拍拍自己的背。我一定会尝试这种方法!几个小时以来,我一直在寻找问题的答案,而这正是我一直在寻找的。这正好解决了我的问题。恐怕我不明白这到底在做什么。我可以问一下“RunLoop”部分是关于什么的?请在代码中添加更多的解释,以便其他人可以从中学习