|
| 1 | +# SwiftyTimer |
| 2 | + |
| 3 | +SwiftyTimer is a set of extensions to make the `NSTimer` API cleaner, nicer to use, and at home with Swift's syntax. |
| 4 | + |
| 5 | +Read [Swifty APIs: NSTimer](http://radex.io/swift/nstimer/) for more information about this project. |
| 6 | + |
| 7 | +### Scheduling a timer |
| 8 | + |
| 9 | +You can easily schedule repeating and non-repeating timers (repeats and delays) using `NSTimer.every` and `NSTimer.after`: |
| 10 | + |
| 11 | +```swift |
| 12 | +NSTimer.every(0.7.seconds) { |
| 13 | + statusItem.blink() |
| 14 | +} |
| 15 | + |
| 16 | +NSTimer.after(1.minute) { |
| 17 | + println("Are you still here?") |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +SwiftyTimer uses closures instead of target/selector/userInfo. |
| 22 | + |
| 23 | +You can specify time intervals with intuitive [Ruby on Rails](http://rubyonrails.org)-like helpers: |
| 24 | + |
| 25 | +```swift |
| 26 | +1.second |
| 27 | +2.5.seconds |
| 28 | +5.seconds |
| 29 | +10.minutes |
| 30 | +1.hour |
| 31 | +``` |
| 32 | + |
| 33 | +You can pass method references instead of closures: |
| 34 | + |
| 35 | +```swift |
| 36 | +NSTimer.every(30.seconds, align) |
| 37 | +``` |
| 38 | + |
| 39 | +If you want to make a timer object without scheduling, use `new(after:)` and `new(every:)`: |
| 40 | + |
| 41 | +```swift |
| 42 | +let timer = NSTimer.new(every: 1.second) { |
| 43 | + println(self.status) |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +(This should be defined as an initializer, but [a bug in Swift](http://www.openradar.me/18720947) prevents this) |
| 48 | + |
| 49 | +### Installation |
| 50 | + |
| 51 | +The simplest way to install this library is to copy `Src/SwiftyTimer.swift` to your project. There's no step two! |
| 52 | + |
| 53 | +#### CocoaPods |
| 54 | + |
| 55 | +You can also install this library using CocoaPods. Just add this line to your Podfile: |
| 56 | + |
| 57 | +```ruby |
| 58 | +pod 'SwiftyTimer' |
| 59 | +``` |
| 60 | + |
| 61 | +Then import library module like so: |
| 62 | + |
| 63 | +```swift |
| 64 | +import SwiftyTimer |
| 65 | +``` |
| 66 | + |
| 67 | +Note that this requires CocoaPods 0.36+, as well as iOS 8 or OS X 10.9+ |
| 68 | + |
| 69 | +### Contributing |
| 70 | + |
| 71 | +If you have comments, complaints or ideas for improvements, feel free to open an issue or a pull request. |
| 72 | + |
| 73 | +### Author and license |
| 74 | + |
| 75 | +Radek Pietruszewski |
| 76 | + |
| 77 | +* [github.com/radex](http://github.com/radex) |
| 78 | +* [twitter.com/radexp](http://twitter.com/radexp) |
| 79 | +* [radex.io](http://radex.io) |
| 80 | +* this.is@radex.io |
| 81 | + |
| 82 | +SwiftyTimer is available under the MIT license. See the LICENSE file for more info. |
0 commit comments