I can't figure out how to stop the timer after a specific time interval.
Any help would be greatly appreciated.
Set the repeats
parameter to false which will result your timer to run only once for the time you set. Other than that, you may find move help on other platforms such as StackOverflow or in Apples Dev. Forums.
PS.: There are better overloads for timer out there that make use of Swifts closures instead of selectors.
When you want to stop a timer in a different function, you have to store it (preferably as an instance variable).
But using a Timer
is a bad solution anyways:
There's CoreAnimation, which imho needs a rewrite, but is still much better suited for the task.
As Tino said there is a better alternative CADisplayLink which would smooth out your animation.
You would pass the timer in the userInfo
parameter—it specifically exists to pass some arbitrary piece of data needed by the selector. The selector will receive the second Timer
as its argument; you can access the user info through that timer's userInfo
property.