Timer - ParkinT/RubyMotion_Life GitHub Wiki

A handy way to handle a Timer

def timerHandler(userInfo)
  @time += 0.1
  @label.text = sprintf("%02d seconds have elapsed", @time)
  if @time >= 19 && @time <= 25.0
    @label.textColor = getColor(153, 31, 0)   #991F00 = 153, 31, 0
  elsif @time > 25.0 && @time < 30.0
    @label.textColor = getColor(209, 25, 25)  #D11919 = 209, 25, 25
  elsif @time >= 30.0
    @timer.invalidate
    @timer = nil
    #trigger event that has expired
  end
end

Elsewhere in the code, call it liked this

# start timer
if @timer == nil
  @timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "timerHandler:", userInfo: nil, repeats: true)