You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.5 KiB

6 years ago
  1. # SwiftyTimer
  2. ![Platforms](https://img.shields.io/badge/platforms-ios%20%7C%20osx%20%7C%20watchos%20%7C%20tvos-lightgrey.svg)
  3. [![CI Status](https://api.travis-ci.org/radex/SwiftyTimer.svg?branch=master)](https://travis-ci.org/radex/SwiftyTimer)
  4. [![CocoaPods](http://img.shields.io/cocoapods/v/SwiftyTimer.svg)](https://cocoapods.org/pods/SwiftyTimer)
  5. [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](#carthage)
  6. ![Swift version](https://img.shields.io/badge/swift-4.2-orange.svg)
  7. #### Modern Swifty API for `NSTimer`
  8. ###### SwiftyTimer allows you to instantly schedule delays and repeating timers using convenient closure syntax. It's time to get rid of Objective-C cruft.
  9. Read [Swifty APIs: NSTimer](http://radex.io/swift/nstimer/) for more information about this project.
  10. ## Usage
  11. You can easily schedule repeating and non-repeating timers (repeats and delays) using `Timer.every` and `Timer.after`:
  12. ```swift
  13. Timer.every(0.7.seconds) {
  14. statusItem.blink()
  15. }
  16. Timer.after(1.minute) {
  17. println("Are you still here?")
  18. }
  19. ```
  20. You can specify time intervals with these intuitive helpers:
  21. ```swift
  22. 100.ms
  23. 1.second
  24. 2.5.seconds
  25. 5.seconds
  26. 10.minutes
  27. 1.hour
  28. 2.days
  29. ```
  30. You can pass method references instead of closures:
  31. ```swift
  32. Timer.every(30.seconds, align)
  33. ```
  34. ### Manual scheduling
  35. If you want to make a timer object without scheduling, use `new(after:)` and `new(every:)`:
  36. ```swift
  37. let timer = Timer.new(every: 1.second) {
  38. println(self.status)
  39. }
  40. ```
  41. (This should be defined as an initializer, but [a bug in Foundation](http://www.openradar.me/18720947) prevents this)
  42. Call `start()` to schedule timers created using `new`. You can optionally pass the run loop and run loop modes:
  43. ```swift
  44. timer.start()
  45. timer.start(modes: .defaultRunLoopMode, .eventTrackingRunLoopMode)
  46. ```
  47. ### Invalidation
  48. If you want to invalidate a repeating timer on some condition, you can take a `Timer` argument in the closure you pass in:
  49. ```swift
  50. Timer.every(5.seconds) { (timer: Timer) in
  51. // do something
  52. if finished {
  53. timer.invalidate()
  54. }
  55. }
  56. ```
  57. ## Installation
  58. **Note:** If you're running Swift 2, use [SwiftyTimer v1.4.1](https://github.com/radex/SwiftyTimer/tree/1.4.1)
  59. #### CocoaPods
  60. If you're using CocoaPods, just add this line to your Podfile:
  61. ```ruby
  62. pod 'SwiftyTimer'
  63. ```
  64. Install by running this command in your terminal:
  65. ```sh
  66. pod install
  67. ```
  68. Then import the library in all files where you use it:
  69. ```swift
  70. import SwiftyTimer
  71. ```
  72. #### Carthage
  73. Just add to your Cartfile:
  74. ```ruby
  75. github "radex/SwiftyTimer"
  76. ```
  77. #### Manually
  78. Simply copy `Sources/SwiftyTimer.swift` to your Xcode project.
  79. ## More like this
  80. If you like SwiftyTimer, check out [SwiftyUserDefaults](https://github.com/radex/SwiftyUserDefaults), which applies the same swifty approach to `NSUserDefaults`.
  81. You might also be interested in my blog posts which explain the design process behind those libraries:
  82. - [Swifty APIs: NSTimer](http://radex.io/swift/nstimer/)
  83. - [Swifty APIs: NSUserDefaults](http://radex.io/swift/nsuserdefaults/)
  84. - [Statically-typed NSUserDefaults](http://radex.io/swift/nsuserdefaults/static)
  85. - [Swifty methods](http://radex.io/swift/methods/)
  86. ### Contributing
  87. If you have comments, complaints or ideas for improvements, feel free to open an issue or a pull request.
  88. ### Author and license
  89. Radek Pietruszewski
  90. * [github.com/radex](http://github.com/radex)
  91. * [twitter.com/radexp](http://twitter.com/radexp)
  92. * [radex.io](http://radex.io)
  93. * this.is@radex.io
  94. SwiftyTimer is available under the MIT license. See the LICENSE file for more info.