Last active
July 23, 2016 07:17
-
-
Save AnselmeKotchap/8af2f3cbb7cb9da59f73971efa2e45ec to your computer and use it in GitHub Desktop.
Revisions
-
AnselmeKotchap revised this gist
Jul 23, 2016 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -37,4 +37,6 @@ class Timer { deinit { self.timer.invalidate() } } //source: http://samuelmullen.com/2014/07/using-swifts-closures-with-nstimer/ -
AnselmeKotchap created this gist
Jul 23, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,40 @@ import Foundation class Timer { var timer = NSTimer() var handler: (Int) -> () let duration: Int var elapsedTime: Int = 0 init(duration: Int, handler: (Int) -> ()) { self.duration = duration self.handler = handler } func start() { self.timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("tick"), userInfo: nil, repeats: true) } func stop() { timer.invalidate() } @objc func tick() { self.elapsedTime++ self.handler(elapsedTime) if self.elapsedTime == self.duration { self.stop() } } deinit { self.timer.invalidate() } }