Xcode10 - 定时器重复触发函数
Xcode10 - Timer triggers function repeatedly
定时器应该每秒启动一个函数。我写这个,假设定时器在程序启动时启动。哪里出错了?
override func viewDidLoad() {
super.viewDidLoad()
myTimer.invalidate()
myTimer = Timer(timeInterval: 1, target: self, selector: #selector(startAnimation), userInfo: nil, repeats: true)
}
@objc func startAnimation(){
print("Started")
}
}
您创建了一个计时器。但是您从未开始它(也称为安排计时器)。所以什么也没有发生。
如果你想一步创建并启动定时器,你应该调用https://developer.apple.com/documentation/foundation/timer/1412416-scheduledtimer
定时器应该每秒启动一个函数。我写这个,假设定时器在程序启动时启动。哪里出错了?
override func viewDidLoad() {
super.viewDidLoad()
myTimer.invalidate()
myTimer = Timer(timeInterval: 1, target: self, selector: #selector(startAnimation), userInfo: nil, repeats: true)
}
@objc func startAnimation(){
print("Started")
}
}
您创建了一个计时器。但是您从未开始它(也称为安排计时器)。所以什么也没有发生。
如果你想一步创建并启动定时器,你应该调用https://developer.apple.com/documentation/foundation/timer/1412416-scheduledtimer