我在一個類中創建了一個定時器,並嘗試在定時器工作時在另一個類中執行其他操作,並在定時器停止時執行其他操作。例如,當計時器工作時每秒顯示一次。我簡化了代碼如下。如何實現這一點?如何在IOS中同步Swift語言中的不同動作
import Foundation
import UIView
class TimerCount {
var timer: NSTimer!
var time: Int!
init(){
time = 5
timer = NSTimer.scheduledTimerWithTimeInterval(1.0 , target: self, selector: Selector("update"), userInfo: nil, repeats: true)
}
func update(){
if(time > 0) {
time = time - 1
// do something while timer works
}
else{
timer.invalidate()
timer = nil
time = 5
}
}
}
class Main: UIView {
var Clock: TimerCount!
override func viewDidLoad() {
Clock = TimerCount()
//? do something else while clock works
// ? do other things while clock stops
// FOR EXAMPLE: show every second when timer works
if(Clock.time > 0){
println(Clock.time)
}else{
println("clocker stops")
}
}
}
你問如何從參考變量'time'類TimerCount班主要? –
我知道如何做到這一點,並將其添加到上面的示例代碼中以闡明目的。你知道如何在Main類中顯示每秒,而不是在TimerCount類中嗎? – Peterxwl