如何在X秒后更改NSTimer的NSTimeInterval?

时间:2016-01-03 04:37:27

标签: ios swift swift2 nstimer nstimeinterval

我正在swift 2制作应用程序,其中有两个计时器。 10秒后,我希望另一个计时器更快。我试过这样做,但它不起作用(我试图将变量time更改为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,但它仍然会每两秒重复一次。请帮忙。为了清楚起见,我希望能够在十秒钟之后将time更改为1而不是2。我也不想让计时器无效。计时器也在repeat = true。在此先感谢...安东

2 个答案:

答案 0 :(得分:3)

一些事情。

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

第二个a> =对字符串的比较不会对你想要达到的目标起作用。

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

@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)
}

答案 1 :(得分:2)

仅设置time = 1不会修改timer

无法更改计时器的timeInterval。你唯一的选择是创建一个新的计时器,或者以timeInterval为1启动它,并且每隔一段时间不做任何事情timer(),直到10秒过去。