i have problem 2 timers running in same view controller. need 1 of them launch when other invalidated , go on after start button first tapped again. tried creating 2 variables , builds successfully, behavior erratic. right approach? thanks
@ibaction func start(_ sender: anyobject) { timer2.invalidate() timer = timer.scheduledtimer(timeinterval: 1.0, target: self, selector: #selector(viewcontroller.update), userinfo: nil, repeats: true) } @ibaction func pausetimer(_ sender: anyobject) { timer.invalidate() timer2 = timer.scheduledtimer(timeinterval: 1.0, target: self, selector: selector(("increasetimer")), userinfo: nil, repeats: true) }
so need sort of "flip flop" timer effect?
hav thought replacing var timerhasfinishedrunning: bool = false
checking invalidation?
e.g.
// when invalidate, rather timer1.invalidate() timer1 = nil // goes timer2 timer2.invalidate() timer2 = nil
that way, can have timer checking done view computed properties:
var timer1hasfinishedrunning: bool { return self.timer1 == nil } var timer1hasfinishedrunning: bool { return self.timer2 == nil }
also mention behave "erratically", elaborate? timer interval 1 second, if erratic happens, "within 1 second" it's because of long interval. e.g. checking of each done once per second, take long 1.999999 seconds notice timer invalidated.
personally, i'd have interval @ 0.1 rather 1.0 greater accuracy.
Comments
Post a Comment