1

I have the steps below to reproduce this issue. As I scroll a UIPickerView, my timers pause. How do I prevent this?

I setup my timer within my class:

var testTimer:Timer = Timer()

In this example I define the timer in viewDidLoad:

testTimer = Timer.scheduledTimer(timeInterval: 0.10, target: self, selector: #selector(self.testFunction), userInfo: nil, repeats: true)

Timer function. The line does not print as UIPickerView is scrolled:

@objc func testFunction(){
    print("testFunction called")
}

This is the function that's called every time UIPickerView is scrolled. According to a previous answer about reusing views with UIPickerView, it is not possible, and the documents are wrong, so I'm not checking for that here:

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {

    let customWidth = 300
    let customHeight = 300

    let label = UILabel(frame:CGRect(x:0, y:0, width:customWidth, height:customHeight))

    label.text = "\(row)"

    label.textColor = UIColor.black
    label.font = UIFont(name:"HelveticaNeue-Bold", size: 18.0)
    label.textAlignment = .center

    return label

}
Chewie The Chorkie
  • 4,896
  • 9
  • 46
  • 90
  • 2
    See: https://stackoverflow.com/questions/605027/uiscrollview-pauses-nstimer-until-scrolling-finishes – dan Mar 27 '19 at 16:59
  • @dan Thank you, that was it. Go ahead and add the answer, and I will mark it as correct. – Chewie The Chorkie Mar 27 '19 at 17:05
  • This is not a duplicate because this has to do with UIPickerView, not UIScrollView. – Chewie The Chorkie Mar 27 '19 at 17:06
  • @ChewieTheChorkie - But I believe it really is the same issue, namely that `scheduledTimer` adds timers to the run loop using `.default` run loop mode, which will not run the timer when interactive UIKit stuff is going on, whereas `.common` will. – Rob Mar 27 '19 at 17:20

0 Answers0