ios - When I pause my timer, then try to start it again, it does not run -


i'm building app in swift 3. when press start first time timer begins, when pause , try press start again, timer not budge. give context, timer, amount of time attached it, selected table. each time timer load, start button works initially.

protocol timerviewcontrollerdelegate: class { func viewcontroller(_ controller: viewcontroller, didfinishediting item: taskdata) }  class viewcontroller: uiviewcontroller, uitextfielddelegate {  @iboutlet weak var timerlabel: uilabel! @iboutlet weak var pausebutton: uibutton! @iboutlet weak var startbutton: uibutton! @iboutlet weak var timertaskname: uilabel! @iboutlet weak var timertimesetting: uilabel! @iboutlet weak var progressview: uiprogressview!  weak var delegate: timerviewcontrollerdelegate?  var timertask: taskdata? var timer: timer? var progressviewspeed: double = 0.0  @ibaction func cancel(_ sender: any) {     timer?.invalidate()     dismiss(animated: true, completion: nil)     delegate?.viewcontroller(self, didfinishediting: timertask!) }  @ibaction func startbuttontapped(_ sender: any) {     timertask?.starttime = date()     runtimer()     if timertask?.istaskrunning == true {         runtimer()         self.startbutton.isenabled = false         self.pausebutton.isenabled = true     } else {         //retrieve start time , run         timertask?.starttime = date()         runtimer()         self.startbutton.isenabled = false         self.pausebutton.isenabled = true     } }  func runtimer() {     guard timer == nil else {         return     }      timer = timer.scheduledtimer(timeinterval: 1, target: self, selector: (#selector(viewcontroller.updatetimer)), userinfo: nil, repeats: true) }  @ibaction func pausebuttontapped(_ sender: uibutton) {     if timertask?.istaskrunning == true {         timer?.invalidate()         if let timertask = timertask, timertask.istaskrunning {              // calculate difference between , when timertask started             let difference = int(date().timeintervalsince(timertask.starttime!))             timertask.taskremaining -= difference             if timertask.taskremaining == 0 {                 // when there's no time remaining on task?             }              timertask.starttime = nil         }     }      else {          timertask?.starttime = date()         runtimer()         self.pausebutton.settitle("pause",for: .normal)     }      self.startbutton.isenabled = true     self.pausebutton.isenabled = false }  /* @ibaction func resetbuttontapped(_ sender: any) {     timer.invalidate()     seconds = 60     self.timerlabel.text = timestring(time: timeinterval(seconds))     if self.resumetapped == true {         self.resumetapped = false         self.pausebutton.settitle("pause",for: .normal)     }     istimerrunning = false     pausebutton.isenabled = false     startbutton.isenabled = true } */  func updatetimer() {     guard let timertask = timertask else {         return     }      if timertask.taskremaining < 1 {         timer?.invalidate()         timer = nil         //send alert indicate "time's up!"     } else {         updatetime()     }     progressviewspeed = 1 / double(timertask.taskremaining)     progressview.progress += float(progressviewspeed) }  func timestring(time:timeinterval) -> string {     let hours = int(time) / 3600     let minutes = int(time) / 60 % 60     let seconds = int(time) % 60     return string(format:"%02i:%02i:%02i", hours, minutes, seconds) }  override func viewdidload() {     super.viewdidload()      guard let timertask = timertask else {         return     }      if timertask.istaskrunning {         startbutton.isenabled = false         pausebutton.isenabled = true         runtimer()     } else {         startbutton.isenabled = true         pausebutton.isenabled = false     }      timertaskname.text = timertask.task     updatetime()      self.progressview.transform = cgaffinetransform.identity.rotated(by: cgfloat.pi / 2).scaledby(x: 1, y: 150)  }  func updatetime() {     guard let timertask = timertask else {         return     }      if let starttime = timertask.starttime {         // calculate difference between , when timertask started         let difference = int(date().timeintervalsince(starttime))          if timertask.taskremaining == difference {             // when there's no time remaining on task             timer?.invalidate()             timer = nil         }          timerlabel.text = timestring(time: timeinterval(timertask.taskremaining - difference))     } else {         timerlabel.text = timestring(time: timeinterval(timertask.taskremaining))     }  } } 

once you've invalidated nstimer, can't use again. should create new object.

see here more from nstimer docs

calling method requests removal of timer current run loop; result, should call invalidate method same thread on timer installed. invalidating timer disables no longer affects run loop. run loop removes , releases timer, either before invalidate method returns or @ later point. once invalidated, timer objects cannot reused.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -