如何为多个不同的控制器制作NSTimer?迅速地
我制作基于页面的应用程序。我有多个不同的控制器。每个控制器都有NSTimer
,当控制器viewDidAppear()
时打开。当控制器为viewDidDisappear()
时,我将NSTimer关闭。当我缓慢地在控制器之间滑动时,它可以工作,但如果我快速滑动控制器,一些定时器不会关闭。我尝试了几种不同的方法。第一种方法是向关闭计时器的函数发送通知,但当我在控制器之间缓慢滑动时,它也起作用。第二种方法我创建了公共类Timer,我对每个控制器都使用了它,但它不适用于我。我不知道我怎么才能做到。请帮帮我。我在UIView中也有NSTmer,它位于UIView控制器上,我需要它在UIView控制器消失时关闭两个计时器。我怎样才能做到这一点?
我在ViewController
上的示例
var timer: NSTimer!
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)
notificationCenter.postNotificationName("turnOnMemoryArc", object: nil)
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "showMemory", userInfo: nil, repeats: true)
}
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(true)
if timer != nil {
timer.invalidate()
}
notificationCenter.postNotificationName("turnOffMemoryArc", object: nil)
}
视图中的此代码
override func drawRect(rect: CGRect) {
notificationCenter.addObserver(self, selector: "turnOnMemoryTimer", name: "turnOnMemoryArc", object: nil)
notificationCenter.addObserver(self, selector: "turnOffMemoryTimer", name: "turnOffMemoryArc", object: nil)
// timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "drawMemoryArc", userInfo: nil, repeats: true)
}
func turnOnMemoryTimer() {
print("memory timer is on")
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "drawMemoryArc", userInfo: nil, repeats: true)
}
func turnOffMemoryTimer() {
if timer != nil {
timer.invalidate()
}
print("Memory timer turned")
}
第二种方法
import Foundation
public class GlobalTimer {
public init() { }
public var controllerTimer: NSTimer!
public var viewTimer: NSTimer!
}
视图控制器
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)
notificationCenter.postNotificationName("turnOnBatteryArc", object: nil)
if GlobalTimer().controllerTimer != nil {
GlobalTimer().controllerTimer.invalidate()
GlobalTimer().controllerTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "batteryStateForLabel", userInfo: nil, repeats: true)
} else {
GlobalTimer().controllerTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "batteryStateForLabel", userInfo: nil, repeats: true)
}
}
UIView
func turnOnBatteryTimer() {
print("turnOnBatteryTimer arc")
if GlobalTimer().viewTimer != nil {
GlobalTimer().viewTimer.invalidate()
GlobalTimer().viewTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "drawBatteryArc", userInfo: nil, repeats: true)
} else {
GlobalTimer().viewTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "drawBatteryArc", userInfo: nil, repeats: true)
}
}
解决方案
代码中的主要问题是GlobalTimer()
。这将在您每次写入GlobalTimer()
时创建新的GlobalTimer
实例。
以下代码中的四个不同实例-无共享状态。
if GlobalTimer().viewTimer != nil {
GlobalTimer().viewTimer.invalidate()
GlobalTimer().viewTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "drawBatteryArc", userInfo: nil, repeats: true)
} else {
GlobalTimer().viewTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "drawBatteryArc", userInfo: nil, repeats: true)
}
您可以使用单例模式修复它...
public class GlobalTimer {
public static let sharedInstance = GlobalTimer()
private init() { }
public var controllerTimer: NSTimer!
public var viewTimer: NSTimer!
}
.通过替换所有GlobalTimer()
个实例...
if GlobalTimer().viewTimer != nil {
.使用GlobalTimer.sharedInstace
...
if GlobalTimer.sharedInstance.viewTimer != nil {
下一步是!
删除,这很危险,如果您不知道自己在做什么,可能会导致崩溃。
public class GlobalTimer {
public static let sharedInstance = GlobalTimer()
private init() { } <-- avoid instantiation outside the file
public var controllerTimer: NSTimer? <-- was !
public var viewTimer: NSTimer? <-- was !
}
剩下的一步是使用GlobalTimer
...
if GlobalTimer().viewTimer != nil {
GlobalTimer().viewTimer.invalidate()
GlobalTimer().viewTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "drawBatteryArc", userInfo: nil, repeats: true)
} else {
GlobalTimer().viewTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "drawBatteryArc", userInfo: nil, repeats: true)
}
.使用...
GlobalTimer.sharedInstance.viewTimer?.invalidate()
GlobalTimer.sharedInstance.viewTimer = NSTimer.scheduled...
第一行显示-callinvalidate()
函数onviewTimer
ifviewTimer != nil
否则什么也不发生(类似于nil
在objc,...中的消息传递)。这允许您删除if
条件,因为它会使计时器无效或不执行任何操作。
第二行仅分配新计时器。
相关文章