|
| 1 | +// |
| 2 | +// SwiftyTimer |
| 3 | +// |
| 4 | +// Copyright (c) 2015 Radosław Pietruszewski |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +// of this software and associated documentation files (the "Software"), to deal |
| 8 | +// in the Software without restriction, including without limitation the rights |
| 9 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +// copies of the Software, and to permit persons to whom the Software is |
| 11 | +// furnished to do so, subject to the following conditions: |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in all |
| 14 | +// copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +// SOFTWARE. |
| 23 | +// |
| 24 | + |
| 25 | +import Foundation |
| 26 | + |
| 27 | +private class NSTimerActor { |
| 28 | + var block: () -> () |
| 29 | + |
| 30 | + init(_ block: () -> ()) { |
| 31 | + self.block = block |
| 32 | + } |
| 33 | + |
| 34 | + dynamic func fire() { |
| 35 | + block() |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +extension NSTimer { |
| 40 | + // NOTE: `new` class functions are a workaround for a crashing bug when using convenience initializers (18720947) |
| 41 | + |
| 42 | + /// Create a timer that will call `block` once after the specified time. |
| 43 | + /// |
| 44 | + /// **Note:** the timer won't fire until it's scheduled on the run loop. |
| 45 | + /// Use `NSTimer.after` to create and schedule a timer in one step. |
| 46 | + |
| 47 | + class func new(after interval: NSTimeInterval, _ block: () -> ()) -> NSTimer { |
| 48 | + let actor = NSTimerActor(block) |
| 49 | + return self.init(timeInterval: interval, target: actor, selector: "fire", userInfo: nil, repeats: false) |
| 50 | + } |
| 51 | + |
| 52 | + /// Create a timer that will call `block` repeatedly in specified time intervals. |
| 53 | + /// |
| 54 | + /// **Note:** the timer won't fire until it's scheduled on the run loop. |
| 55 | + /// Use `NSTimer.every` to create and schedule a timer in one step. |
| 56 | + |
| 57 | + class func new(every interval: NSTimeInterval, _ block: () -> ()) -> NSTimer { |
| 58 | + let actor = NSTimerActor(block) |
| 59 | + return self.init(timeInterval: interval, target: actor, selector: "fire", userInfo: nil, repeats: true) |
| 60 | + } |
| 61 | + |
| 62 | + /// Create and schedule a timer that will call `block` once after the specified time. |
| 63 | + |
| 64 | + class func after(interval: NSTimeInterval, _ block: () -> ()) -> NSTimer { |
| 65 | + let timer = NSTimer.new(after: interval, block) |
| 66 | + NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSDefaultRunLoopMode) |
| 67 | + return timer |
| 68 | + } |
| 69 | + |
| 70 | + /// Create and schedule a timer that will call `block` repeatedly in specified time intervals. |
| 71 | + |
| 72 | + class func every(interval: NSTimeInterval, _ block: () -> ()) -> NSTimer { |
| 73 | + let timer = NSTimer.new(every: interval, block) |
| 74 | + NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSDefaultRunLoopMode) |
| 75 | + return timer |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +extension Int { |
| 80 | + var second: NSTimeInterval { return NSTimeInterval(self) } |
| 81 | + var seconds: NSTimeInterval { return NSTimeInterval(self) } |
| 82 | + var minute: NSTimeInterval { return NSTimeInterval(self * 60) } |
| 83 | + var minutes: NSTimeInterval { return NSTimeInterval(self * 60) } |
| 84 | + var hour: NSTimeInterval { return NSTimeInterval(self * 3600) } |
| 85 | + var hours: NSTimeInterval { return NSTimeInterval(self * 3600) } |
| 86 | +} |
| 87 | + |
| 88 | +extension Double { |
| 89 | + var second: NSTimeInterval { return self } |
| 90 | + var seconds: NSTimeInterval { return self } |
| 91 | + var minute: NSTimeInterval { return self * 60 } |
| 92 | + var minutes: NSTimeInterval { return self * 60 } |
| 93 | + var hour: NSTimeInterval { return self * 3600 } |
| 94 | + var hours: NSTimeInterval { return self * 3600 } |
| 95 | +} |
0 commit comments