如何在Swift中设置计时器

时间:2022-06-28 02:49:11

I'm trying to figure out the best way to keep track of time for a timer I've made. I want the timer to work while the app isn't open too.

我正在试图找出最好的方法来跟踪我制作的计时器的时间。我希望计时器在应用程序未打开时工作。

I can say what I'm thinking in psuedo code, but I don't know enough Swift to make it happen. When the startStopButton is pressed, I'd probably want to set an NSDate. Then, every second, I'd want a new NSDate to be compared to the original to figure out how many seconds had passed. That way, if the user leaves the app and comes back, it just checks the original time stamp and compares it to the present. Then, I'd put that number of seconds into a variable that I have already set up to manipulate the way I want it. Here's what I have so far:

我可以说我在psuedo代码中的想法,但我不知道Swift能够实现它。当按下startStopButton时,我可能想要设置一个NSDate。然后,每一秒,我都希望将新的NSDate与原始NSDate进行比较,以确定已经过了多少秒。这样,如果用户离开应用程序并返回,它只会检查原始时间戳并将其与当前时间戳进行比较。然后,我将这个秒数放入我已经设置的变量中以操纵我想要的方式。这是我到目前为止所拥有的:

var timer = NSTimer()
var second = 00.0


    func timerResults() {

    second += 1        


    let secondInIntForm = Int(second)

    let (h,m,s) = secondsToHoursMinutesSeconds(secondInIntForm)

}

    @IBAction func startStopButton(sender: AnyObject) {

    date = NSDate()

    moneyEverySecond = (people*wage)/3600

    if updatingSymbol.hidden == true { //Start the timer
        sender.setTitle("STOP", forState: UIControlState.Normal)
        updatingSymbol.hidden = false

        timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("timerResults"), userInfo: nil, repeats: true)

    } else { //Stop the timer
        sender.setTitle("START", forState: UIControlState.Normal)
        updatingSymbol.hidden = true
        //***stop the timer
        timer.invalidate()
    }
}

If anyone can help, that'd be awesome.

如果有人可以提供帮助,那就太棒了。

1 个解决方案

#1


1  

Pass the timer's start time through the userInfo argument:

通过userInfo参数传递计时器的开始时间:

@IBAction func startStopButton(sender : AnyObject) {
    timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(ViewController.timerResults(_:)) , userInfo: NSDate(), repeats: true)
}

func timerResults(timer: NSTimer) {
    let timerStartDate = timer.userInfo as! NSDate
    let seconds = Int(NSDate().timeIntervalSinceDate(timerStartDate))
    print(seconds)
}

(I removed some parts of your functions because they are not relevant to the question)

(我删除了部分功能,因为它们与问题无关)

#1


1  

Pass the timer's start time through the userInfo argument:

通过userInfo参数传递计时器的开始时间:

@IBAction func startStopButton(sender : AnyObject) {
    timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(ViewController.timerResults(_:)) , userInfo: NSDate(), repeats: true)
}

func timerResults(timer: NSTimer) {
    let timerStartDate = timer.userInfo as! NSDate
    let seconds = Int(NSDate().timeIntervalSinceDate(timerStartDate))
    print(seconds)
}

(I removed some parts of your functions because they are not relevant to the question)

(我删除了部分功能,因为它们与问题无关)