Last active
October 12, 2018 11:20
-
-
Save rawandahmad698/a0095e584eaa4df278a23c92e71cca62 to your computer and use it in GitHub Desktop.
This file contains 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 characters
import WatchKit | |
import Foundation | |
import NotificationCenter | |
class TimerController: WKInterfaceController { | |
@IBOutlet weak var timerOutlet: WKInterfaceTimer! | |
var myTimer : Timer? | |
var duration : TimeInterval = 45.0 //arbitrary number. 45 seconds | |
var isPaused = false //flag to determine if it is paused or not | |
var elapsedTime : TimeInterval = 0.0 //time that has passed between | |
var startTime = NSDate() | |
override func awake(withContext context: Any?) { | |
super.awake(withContext: context) | |
start_timer() | |
timerOutlet.setTextColor(UIColor.red) | |
// Configure interface objects here. | |
} | |
func start_timer() { | |
myTimer = Timer.scheduledTimer(timeInterval: duration, target: | |
self,selector: Selector(("timerDone")), userInfo: nil, repeats: | |
false) | |
timerOutlet.setDate(NSDate(timeIntervalSinceNow: duration ) as | |
Date) | |
timerOutlet.start() | |
} | |
func timerDone(){ | |
//timer done counting down | |
} | |
@IBAction func pauseResumePressed() { | |
//timer is paused. so unpause it and resume countdown | |
if isPaused{ | |
isPaused = false | |
myTimer = Timer.scheduledTimer(timeInterval: duration - | |
elapsedTime, target: self, selector: | |
Selector(("timerDone")), userInfo: | |
nil, repeats: false) | |
timerOutlet.setDate(NSDate(timeIntervalSinceNow: duration - | |
elapsedTime) as Date) | |
timerOutlet.start() | |
startTime = NSDate() | |
//pauseResumeButton.setTitle("Pause") | |
} | |
//pause the timer | |
else{ | |
isPaused = true | |
//get how much time has passed before they paused it | |
let paused = NSDate() | |
elapsedTime += paused.timeIntervalSince(startTime as Date) | |
//stop watchkit timer on the screen | |
timerOutlet.stop() | |
//stop the ticking of the internal timer | |
myTimer!.invalidate() | |
//do whatever UI changes you need to | |
//pauseResumeButton.setTitle("Resume") | |
} | |
} | |
override func willActivate() { | |
// This method is called when watch view controller is about to | |
super.willActivate() | |
NotificationCenter.default.addObserver(self, selector: #selector(stop_timer(notification:)), name: .stopTimer, object: nil) | |
} | |
override func didDeactivate() { | |
// This method is called when watch view controller is no | |
super.didDeactivate() | |
} | |
@objc func stop_timer(notification:NSNotification) { | |
// Timer is paused. so unpause it and resume countdown | |
if isPaused { | |
isPaused = false | |
timerOutlet.start() | |
startTime = NSDate() | |
} else { | |
isPaused = true | |
// Stop watchkit timer on the screen | |
timerOutlet.stop() | |
// Stop the ticking of the internal timer | |
myTimer!.invalidate() | |
// Do whatever UI changes you need to | |
} | |
} | |
} | |
extension Notification.Name { | |
static let stopTimer = Notification.Name("stopTimer") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment