├── .gitignore ├── LICENSE ├── README.md └── PSTimer.swift /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Pete Simpson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PSTimer 2 | A simple countdown timer component for iOS apps written in Swift. Just clone or download the repo and drag the PSTimer.swift file into your project. 3 | 4 | ## Initialization 5 | The `CountdownTimer` class initializer takes a `UILabel`, as well as the minutes and seconds that the timer should start the countdown from. Make sure to adhere to the `CountdownTimerDelegate` protocol and implement the `countdownEnded()` function in order to get notified when the countdown finishes. Example: 6 | 7 | ```swift 8 | class CustomViewController: UIViewController, CountdownTimerDelegate { 9 | 10 | @IBOutlet weak var timeLabel: UILabel! 11 | var timer: CountdownTimer! 12 | 13 | override func viewDidLoad() { 14 | // Timer will start at 15:00 15 | timer = CountdownTimer(timerLabel: timeLabel, startingMin: 15, startingSec: 0) 16 | timer.delegate = self 17 | } 18 | 19 | func countdownEnded() -> Void { 20 | // Handle countdown finishing 21 | } 22 | } 23 | ``` 24 | 25 | ## Start / Stop 26 | You can start and stop the timer with: 27 | 28 | ```swift 29 | timer.start() // Begins countdown 30 | timer.pause() // Pauses countdown 31 | timer.reset() // Pauses countdown and resets to the initial time 32 | ``` 33 | 34 | ## License 35 | MIT all the way. 36 | -------------------------------------------------------------------------------- /PSTimer.swift: -------------------------------------------------------------------------------- 1 | // 2 | // PTSTimer.swift 3 | // Tempo 4 | // 5 | // Created by Peter Simpson on 2/11/15. 6 | // Copyright (c) 2015 Pete Simpson. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | protocol CountdownTimerDelegate { 12 | func countdownEnded() -> Void 13 | } 14 | 15 | class CountdownTimer { 16 | 17 | let timerLabel: UILabel 18 | var timing: Bool = false 19 | var startingMin: Int 20 | var startingSec: Int 21 | var secLeft: Int 22 | var minLeft: Int 23 | var endTime: Int! 24 | var timer: NSTimer! 25 | var delegate: CountdownTimerDelegate! 26 | 27 | init(timerLabel: UILabel, startingMin: Int, startingSec: Int) { 28 | self.timerLabel = timerLabel 29 | self.startingMin = startingMin 30 | self.startingSec = startingSec 31 | self.minLeft = startingMin 32 | self.secLeft = startingSec 33 | 34 | refreshTimerLabel() 35 | } 36 | 37 | func refreshTimerLabel() { 38 | let secString = secLeft < 10 ? "0\(secLeft)" : "\(secLeft)" 39 | timerLabel.text = "\(minLeft):\(secString)" 40 | } 41 | 42 | func start() { 43 | if (!timing) { 44 | timing = true 45 | if minLeft == startingMin { 46 | if secLeft == 0 { 47 | minLeft = startingMin - 1 48 | secLeft = 59 49 | } else { 50 | secLeft-- 51 | } 52 | 53 | refreshTimerLabel() 54 | } 55 | 56 | endTime = Int(round(NSDate().timeIntervalSince1970)) + minLeft*60 + secLeft 57 | timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "updateTimer", userInfo: nil, repeats: true) 58 | } 59 | } 60 | 61 | func pause() { 62 | timing = false 63 | timer.invalidate() 64 | } 65 | 66 | func reset() { 67 | pause() 68 | minLeft = startingMin 69 | secLeft = startingSec 70 | refreshTimerLabel() 71 | } 72 | 73 | // MARK: - Timer Logic 74 | dynamic func updateTimer() { 75 | let timeDiff = endTime - Int(round(NSDate().timeIntervalSince1970)) 76 | minLeft = timeDiff / 60 77 | secLeft = timeDiff % 60 78 | if minLeft <= 0 && secLeft <= 0 { 79 | 80 | reset() 81 | delegate.countdownEnded() 82 | 83 | return 84 | } 85 | refreshTimerLabel() 86 | } 87 | } --------------------------------------------------------------------------------