从计时器停止NSRunLoop,可以通过以下方法实现:
let timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(timerFired), userInfo: nil, repeats: true)
NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)
timer.invalidate()
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(1.0 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
timer.invalidate()
}
let stopTime = NSDate(timeIntervalSinceNow: 5.0)
let timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: #selector(timerFired), userInfo: stopTime, repeats: true)
NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)
func timerFired(timer: NSTimer) {
let stopTime = timer.userInfo as! NSDate
if NSDate().compare(stopTime) == .OrderedDescending {
timer.invalidate()
}
}
以上是一些常见的方法来停止NSRunLoop,具体实现方式可能会因应用场景而有所不同。
领取专属 10元无门槛券
手把手带您无忧上云