ios - Animate background when entering foreground -
i working on stopwatch app begins flashing when approaching limit , again when overtime. works great except when app pushed background , brought foreground again. @ time animation not start.
i have tried moving code applicationwillenterforeground
, applicationdidbecomeactive
produces fatal error:
unexpectedly found nil while unwrapping optional value
am missing basic here?
there more code of course (calculations, etc.), relevant section. (edit: i've expanded provide better view of i'm doing.)
@ibaction func start(_ sender: anyobject) { if timeron == 0 { mytimer = timer.scheduledtimer(timeinterval: 0.1, target:self, selector: #selector(viewcontroller.updatecounter), userinfo: nil, repeats: true) starttime = nsdate.timeintervalsincereferencedate timeron = 1 redoryellow = "white" startbutton.superview?.sendsubview(toback: startbutton) background.superview?.sendsubview(toback: background) pickermask.ishidden = false } } .... func animatelight() { if redoryellow == "red" { print("red") //background.superview?.sendsubview(toback: background) uiview.animate(withduration: 0.3, delay: 0.01, options:[uiviewanimationoptions.repeat, uiviewanimationoptions.allowuserinteraction], animations: { self.background.backgroundcolor = uicolor.red self.background.backgroundcolor = uicolor.white }, completion: nil) } else if redoryellow == "yellow" { print("yellow") //background.superview?.sendsubview(toback: background) uiview.animate(withduration: 1, delay: 0.1, options:[uiviewanimationoptions.repeat, uiviewanimationoptions.allowuserinteraction], animations: { self.background.backgroundcolor = uicolor.yellow self.background.backgroundcolor = uicolor.white }, completion: nil) } } func updatecounter() { let currenttime = nsdate.timeintervalsincereferencedate var elapsedtime: timeinterval = currenttime - starttime totaltime = elapsedtime if totaltime >= partbasetimes[numberofselectedpart] { if redoryellow != "red" { redoryellow = "red" animatelight() } } else if totaltime >= (partbasetimes[numberofselectedpart] - 30) { if redoryellow != "yellow" { redoryellow = "yellow" animatelight() } } let minutes = uint8(elapsedtime / 60.0) elapsedtime -= (timeinterval(minutes) * 60) let seconds = uint8(elapsedtime) elapsedtime -= timeinterval(seconds) let fractionofsecond = uint8(elapsedtime * 10) let strminutes = string(format: "%02d", minutes) let strseconds = string(format: "%02d", seconds) let strfractionofsecond = string(format: "%01d", fractionofsecond) talktimer.text = "\(strminutes):\(strseconds).\(strfractionofsecond)" }
Comments
Post a Comment