11

Help me. I tried to make NSTimer with UIScrollView. but NSTimer stop during Scrolling on UIScroll View..

How can I keep work NSTimer during scrolling?

vacawama
  • 150,663
  • 30
  • 266
  • 294
Kyu
  • 139
  • 2
  • 13

1 Answers1

52

I created a simple project with a scrollView and a label that is updated with an NSTimer. When creating the timer with scheduledTimerWithInterval, the timer does not run when scrolling.

The solution is to create the timer with NSTimer:timeInterval:target:selector:userInfo:repeats and then call addTimer on NSRunLoop.mainRunLoop() with mode NSRunLoopCommonModes. This allows the timer to update while scrolling.

Here it is running:

Demo .gif

Here is my demo code:

class ViewController: UIViewController {

    @IBOutlet weak var timerLabel: UILabel!
    var count = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        timerLabel.text = "0"

        // This doesn't work when scrolling
        // let timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "update", userInfo: nil, repeats: true)

        // Do these two lines instead:
        let timer = NSTimer(timeInterval: 1, target: self, selector: "update", userInfo: nil, repeats: true)

        NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)
    }

    func update() {
        count += 1
        timerLabel.text = "\(count)"
    }
}

Swift 3:

let timer = Timer(timeInterval: 1, target: self, selector: #selector(update), userInfo: nil, repeats: true)
RunLoop.main.add(timer, forMode: RunLoopMode.commonModes)

Swift 4, 5:

let timer = Timer(timeInterval: 1, target: self, selector: #selector(update), userInfo: nil, repeats: true)
RunLoop.main.add(timer, forMode: RunLoop.Mode.common)
Community
  • 1
  • 1
vacawama
  • 150,663
  • 30
  • 266
  • 294
  • i used this code-> // pushButtonCountTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: ("pushButtonThreeMinuete"), userInfo: nil, repeats: true) pushButtonCountRunning = true // i change that code like your code like this -> pushButtonCountTimer = NSTimer(timeInterval: 1, target: self, selector: "pushButtonThreeMinuete", userInfo: nil, repeats: true) // but not working timer.. i am a starter about programming...it is very hard.. ;( any way thanks very much mr.Vacawama – Kyu Dec 13 '15 at 16:11
  • You need to also do after that: `if let timer = pushButtonCountTimer { NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes) }` so that the timer runs. – vacawama Dec 13 '15 at 16:39
  • i entered that code but.. i can see red error message-> initializer for conditional binding must have Optional type, not 'NSTimer' .. what is this message.. ;-> – Kyu Dec 13 '15 at 17:06
  • Sorry about that. In my code I had declared `timer` as an optional `NSTimer?`, but you didn't. So, ignore the `if let` part and just do `NSRunLoop.mainRunLoop().addTimer(pushButtonCountTimer, forMode: NSRunLoopCommonModes)` – vacawama Dec 13 '15 at 17:11