简体   繁体   English

X秒后如何更改NSTimer的NSTimeInterval?

[英]How to change the NSTimeInterval of an NSTimer after X seconds?

I'm making an app in swift 2 where there is two timers. 我正在快速2中制作一个具有两个计时器的应用程序。 After 10 seconds I would like another timer to go faster. 10秒后,我希望另一个计时器更快。 I have tried doing it like this but it didn't work (I'm trying to change the var time to 1): 我试图这样做,但没有用(我正在尝试将var 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)"
}

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. 如果我在func timer编写print(time),则十秒钟后它将打印1而不是2,但仍将每两秒重复一次。 Please help. 请帮忙。 To be clear, I want to be able to change time to 1 instead of 2 after ten seconds. 需要明确的是,我希望能够在十秒后将time更改为1而不是2。 I also don't want to invalidate the timers. 我也不想使计时器无效。 The timers are also on repeat = true . 计时器也处于repeat = true Thanks in advance... Anton 在此先感谢...安东

A few of things. 一些事情。

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. 首先,您无法更改重复NSTimer的时间间隔-您需要使第一个定时器无效,并为新间隔安排新的定时器。

Second a >= comparison on a string won't work for what you are trying to achieve. 其次,对字符串进行> =比较对于您要实现的目标不起作用。

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. 最终,您陷入了与许多Swift程序员相同的坏习惯,并不必要地初始化了计时器变量,只是随后将这些计时器扔掉了。 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)
}

Just setting time = 1 will not modify timer . 仅将time = 1设置time = 1不会修改timer

The timeInterval of a timer can not be changed. 定时器的timeInterval不能更改。 Your only options are to create a new timer or start it with a timeInterval of 1 and just do nothing every other time timer() is called until the 10 sec have passed. 您唯一的选择是创建一个新计时器或将其以timeInterval为1启动,并且每隔10秒钟调用一次timer()就不执行任何其他操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM