0

I have a timer in a collection view in a timer, and it ticks down, but every time I swipe, in the middle of the swipe the timer just stopsenter image description here

As you see in the picture the timer is still when you swipe,it only starts ticking when you are done with the swipe. Here is the code:

timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(QuestionsViewController.update), userInfo: nil, repeats: true) self.navigationItem.setHidesBackButton(true, animated: true)

func update() {
    if counter >= 0 {
        self.title = "Time Left: \(String(self.counter))"
        counter -= 1
    } else {
        alert()
        timer!.invalidate()
    }

}

What am I doing wrong?

halfer
  • 19,824
  • 17
  • 99
  • 186
josh
  • 145
  • 1
  • 11
  • Could be the same issue as this: https://stackoverflow.com/q/34234939/1630618 – vacawama Aug 04 '17 at 23:46
  • Possible duplicate of [How do I update a UITableView on a timer while scrolling](https://stackoverflow.com/questions/45303189/how-do-i-update-a-uitableview-on-a-timer-while-scrolling) – Adrian Bobrowski Aug 05 '17 at 03:52
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Aug 05 '17 at 07:58

1 Answers1

1

I would recommend adding the timer to the main run loop:

RunLoop.main.add(timer, forMode: .commonModes)

Infinity James
  • 4,667
  • 5
  • 23
  • 36