I want to know how it is possible to grab the time from a UIDatePicker in Count Down Timer mode, and then display that while counting down in a Hour:Minute:Second format. My biggest issue is actually grabbing the time from the Date Picker. As I'm not really to sure how to start that.
override func viewDidLoad() {
super.viewDidLoad()
stopWatchPicker.countDownDuration = 60.0;
if timerCount == 0 {
timerRunning = false
}
}
var timerCount = 60 //set to 60 for testing purposes
var timer = Timer()
var timerRunning = false
#IBOutlet weak var stopWatchPicker: UIDatePicker!
#IBOutlet weak var countDownLabel: UILabel!
#IBAction func startButton(_ sender: AnyObject) {
if timerRunning == false {
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateCounter), userInfo: nil, repeats: true)
timerRunning = true
}
}
#IBAction func stopButton(_ sender: AnyObject) {
timer.invalidate()
timerRunning = false
}
func updateCounter() {
timerCount -= 1
countDownLabel.text = "\(timerCount)"
if timerCount == 0 {
timer.invalidate()
timerRunning = false
}
}
You need to grab the countDownDuration from the stopWatchPicker and assign it to the timerCount variable.
#IBAction func startButton(_ sender: AnyObject) {
if timerRunning == false {
timerCount = stopWatchPicker.countDownDuration
timerRunning = true
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateCounter), userInfo: nil, repeats: true)
}
}
Cheers!
Related
I am a complete beginner and am starting to learn the Swift programming language. I'm following a tutorial on Udemy and am having some problems with setting up a timer.
class ViewController: UIViewController {
#IBOutlet weak var label: UILabel!
var timer1 = Timer()
var counter = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
label.text = String(counter)
}
#IBAction func start(_ sender: Any) {
timer1 = Timer(timeInterval: 1, target: self, selector: #selector(tim), userInfo: nil, repeats: true)
}
#IBAction func pause(_ sender: Any) {
timer1.invalidate()
}
#IBAction func restart(_ sender: Any) {
timer1.invalidate()
counter = 0
label.text = String(counter)
}
#objc func tim() {
counter += 1
label.text = String(counter)
}
}
This is my code but the timer is not working. Please tell me where im going wrong.
You need to add timer to RunLoop
RunLoop.current.add(timer1, forMode: .commonModes)
Or use scheduledTimer
timer1 = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(tim), userInfo: nil, repeats: true)
Also in your restart function you need to do it again, because now you are just invalidating it and not starting again.
When 10 seconds hit the timer. I would like the background color to be change. Right now my code is not working. Nothing is being changed when the code hits 10 seconds. I also dont think the timer is working.
#IBOutlet var espn: UILabel!
var TIMER = Timer()
var seconds = 0
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if seconds == 10{self.view.backgroundColor = UIColor.red
}
}
#IBAction func startTimer() {
TIMER = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(threeVC.clocker), userInfo: nil, repeats: true)
}
#objc func clocker() {
seconds += 1
espn.text = String( seconds)
}
}
Try this code:
#IBOutlet var espn: UILabel!
var TIMER = Timer()
var seconds = 0
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
#IBAction func startTimer() {
TIMER = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(threeVC.clocker), userInfo: nil, repeats: true)
}
#objc func clocker() {
seconds += 1
espn.text = String( seconds)
if seconds == 10{
self.view.backgroundColor = UIColor.red
print("soda")
}
}
if statement should be written in #objc func clocker() not in viewDidAppear
I have the following count up timer, but after i run the program, the UILable generate from 0 then to (). But the counter number is correct because i can see the printing log is correct.
May I know what's going wrong with the code.`
#IBOutlet weak var labelForBinaryCount: UILabel!
var timer = Timer()
var counter = 0
#IBAction func start() {
timer = Timer.scheduledTimer(timeInterval: 1, target:self, selector: #selector(Resting.updateCounter), userInfo: nil, repeats: true)
}
override func viewDidLoad() {
super.viewDidLoad()
labelForBinaryCount.text = String(counter)
}
func updateCounter() {
labelForBinaryCount.text = String(describing: counter += 1)
print(counter)
}
#IBAction func pauseButton(sender: AnyObject) {
timer.invalidate()
}
#IBAction func clearButton(sender: AnyObject) {
timer.invalidate()
counter = 0
labelForBinaryCount.text = String(counter)
}`
Try this:
func updateCounter() {
counter += 1
labelForBinaryCount.text = String(counter)
print(counter)
}
I think you understand. ;)
I'm trying to push the next view controller after a delay, using a timer.
I already made an app with an one second timer, but I'd like it to stop after a period of time.
import UIKit
class Level1: UIViewController {
var timerCount = 0
var timmerRunning = false
var timer = NSTimer()
#IBOutlet weak var timerLabel: UILabel!
func Counting() {
timerCount += 1
timerLabel.text = "\(timerCount)"
}
#IBAction func startButton(sender: UIButton) {
if !timmerRunning {
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("Counting"), userInfo: nil, repeats: true)
timmerRunning = true
}
}
#IBAction func stopButton(sender: UIButton) {
if timmerRunning {
timer.invalidate()
timmerRunning = false
}
}
#IBAction func restartButton(sender: UIButton) {
timerCount = 0
timerLabel.text = "0"
}
}
You can check your timerCount variable in the counting function like that and after it has reached your desired period of time, stop the timer and present the new ViewController:
func Counting() {
timerCount += 1
timerLabel.text = "\(timerCount)"
if timerCount == desiredPeriodOfTime {
self.timer.invalidate()
self.timmerRunning = false
dispatch_async(dispatch_get_main_queue()){
let viewController = NewViewController()
self.presentViewController(viewControllerToPresent: viewController, animated: true, completion: nil)
}
}
}
I'm trying to create a simple countdown/Up timer app for myself. So far I've figured out how to create the timers which can count up and down with a start, stop and reset buttons but I can not create a multiple timer, I'm guessing its to do with the way I'm calling the function. I'm only guessing as its my first go at it.
Variables and Labels
var timerCountUp = 0
var timerCountDown = 45
var timerRunning = false
var timer = NSTimer()
#IBOutlet weak var timerUpLabel: UILabel!
func CountingUp(){
timerCountUp += 1
timerUpLabel.text = "\(timerCountUp)"
}
#IBOutlet weak var timerDownLabel: UILabel!
func CountingDown(){
timerCountDown -= 1
timerDownLabel.text = "\(timerCountDown)"
}
Stop and Rest Buttons
#IBAction func stopButton(sender: UIButton) {
if timerRunning == true{
timer.invalidate()
timerRunning = false
}
}
#IBAction func restartButton(sender: UIButton) {
timerCountUp = 0
timerUpLabel.text = "0"
}
For my start button I have the following
#IBAction func startButton(sender: UIButton) {
if timerRunning == false{
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("CountingUp"), userInfo: nil, repeats: true)
timerRunning = true
}
If my selector is either "CountingUp" or "CountingDown" I get one of them to work, so I did a bit of searching and came up with the following which does not work
#IBAction func startButton(sender: UIButton) {
if timerRunning == false{
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("timerStarted"), userInfo: nil, repeats: true)
timerRunning = true
}
func timerStarted() {
CountingUp()
CountingDown()
}
}
Would it be possible to show me where I have gone wrong please?