Search code examples
iosswifttimernstimer

getTimeElapsed in Swift with NSTimer


I'm a beginner in Swift and I'm facing a problem.

I want to create a class that contains a timer and a button Everytime the button is tapped I have to restart the timer, if the time elapsed between the timer starts and the button is tapped is greater (or equal) to 400 milliseconds, I have to call the function in the selector.

But there isn't a "GetTimeElapsed()" method in swift and I don't know how to do it. If you have some clue/tutorials it could be cool !

Thx guys


Solution

  • Few small steps:

    1. Define the start time: (should happen at the same time you start the timer)

      startTime = (NSDate.timeIntervalSinceReferenceDate())

    2. Measure the time difference

      let elapsed = NSDate.timeIntervalSinceReferenceDate() - startTime