如何在X秒后更改NSTimer的NSTimeInterval? [英] How to change the NSTimeInterval of an NSTimer after X seconds?

查看:128
本文介绍了如何在X秒后更改NSTimer的NSTimeInterval?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在swift 2制作应用程序,其中有两个计时器。 10秒后,我希望另一个计时器更快。我试过这样做,但它不起作用(我试图将var 时间更改为1):

I'm making an app in swift 2 where there is two timers. After 10 seconds I would like another timer to go faster. I have tried doing it like this but it didn't work (I'm trying to change the var time to 1):

@IBOutlet var displayTimeLabel: UILabel!
var startTimer = NSTimeInterval()
var timer = NSTimer()
var timer2:NSTimer = NSTimer()    
var time = 2.0
@IBAction func Start(sender: UIButton) {
    if !timer2.valid {
        timer2 = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: "updateTime", userInfo: nil, repeats: true)
        startTimer = NSDate.timeIntervalSinceReferenceDate()
    }
    timer = NSTimer.scheduledTimerWithTimeInterval(time, target: self, selector: "timer:", userInfo: nil, repeats: true)
}
func timer(timer: NSTimer){
//code
}
func updateTime() {        
    if displayTimeLabel.text >= "00:10.00"{
     print("00:10.00") //works
     time = 1 // trying to execute code after 10 seconds(doesn't work)
    }
    let currentTime = NSDate.timeIntervalSinceReferenceDate()
    var elapsedTime: NSTimeInterval = currentTime - startTimer
    let minutes = UInt8(elapsedTime / 60.0)
    elapsedTime -= (NSTimeInterval(minutes) * 60)
    let seconds = UInt8(elapsedTime)
    elapsedTime -= NSTimeInterval(seconds)
    let fraction = UInt8(elapsedTime * 100)
    let strMinutes = String(format: "%02d", minutes)
    let strSeconds = String(format: "%02d", seconds)
    let strFraction = String(format: "%02d", fraction)
    displayTimeLabel.text = "\(strMinutes):\(strSeconds).\(strFraction)"
}

如果我在 func timer 中写下打印(时间),十秒后它会打印1而不是2但它仍然是每两秒重复一次。请帮忙。要清楚,我希望能够在十秒后将时间更改为1而不是2。我也不想让计时器无效。计时器也在 repeat = true 上。在此先感谢... Anton

If I write print(time) in func timer, after ten seconds it will print 1 instead of 2 but it will still be repeating every two second. Please help. To be clear, I want to be able to change time to 1 instead of 2 after ten seconds. I also don't want to invalidate the timers. The timers are also on repeat = true. Thanks in advance... Anton

推荐答案

一些事情。

首先,你不能改变重复 NSTimer的时间间隔 - 你需要使第一个计时器无效并为新的时间间隔安排一个新的。

First, you can't change the time interval of a repeating NSTimer - you need to invalidate the first timer and schedule a new one for the new interval.

第二个a> =字符串上的比较不适用于你想要实现的目标。

Second a >= comparison on a string won't work for what you are trying to achieve.

最后,你已经陷入了与许多Swift程序员相同的坏习惯,并且不必要地初始化了你的计时器变量,只是随后将这些计时器扔掉了。您应该使用可选或隐式展开的可选

Finally, you have fallen into the same bad habit as many Swift programmers and initialised your timer variables needlessly only to subsequently throw those timers away. You should use either an optional or an implicitly unwrapped optional

@IBOutlet var displayTimeLabel: UILabel!
var startTime:NSDate?
var timer : NSTimer?
var timer2: NSTimer?  
var time = 2.0

@IBAction func Start(sender: UIButton) {
    if (self.timer2 == nil) {
        self.time=2.0 
        self.timer2 = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: "updateTime", userInfo: nil, repeats: true)
        self.startTime = NSDate()
        self.timer = NSTimer.scheduledTimerWithTimeInterval(time, target: self, selector: "timer:", userInfo: nil, repeats: true)
    }
}

func timer(timer: NSTimer){
//code
}
func updateTime() {        
    let currentTime = NSDate.timeIntervalSinceNow()
    var elapsedTime = -self.startTime!.timeIntervalSinceNow()
    if (elapsedTime >10.0 && self.time==2.0) {
       timer.invalidate()
       self.time=1.0
       self.timer = NSTimer.scheduledTimerWithTimeInterval(time, target: self, selector: "timer:", userInfo: nil, repeats: true)
    }

    let minutes = UInt8(elapsedTime / 60.0)
    elapsedTime -= (NSTimeInterval(minutes) * 60)
    let seconds = UInt8(elapsedTime)
    elapsedTime -= NSTimeInterval(seconds)
    let fraction = UInt8(elapsedTime * 100)
    displayTimeLabel.text = String(format: "%02d:%02d.%02d", minutes,seconds,fraction)
}

这篇关于如何在X秒后更改NSTimer的NSTimeInterval?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆