I have a working countdown which you can increase by pushing the add button and thats what the time counts down from (so basically a user can set the countdown)
I would like to display the starting time as 00:00 as it does in my label.
When I click button to increase the countdown it just begins at 1 obviously because at the moment its just an Int
So i had a thought of create a dictionary something like this
var timeDictionary : [Double : Double] = [00 : 00]
I just want to increase to 00:01, 2, 3 when the + button is pressed and start from 00:00. Can anyone help with this if possible please?
this is my full code for the countdown
var timeDictionary : [Double : Double] = [00 : 00]
var timer = NSTimer()
var countdown = 0
func runTimer() {
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self,
selector:Selector("updateTimer"), userInfo: nil, repeats: true)
}
func updateTimer() {
if countdown > 0 {
countdown--
TimerLabel.text = String(countdown)
} else {
countdown = 0
TimerLabel.text = String(countdown)
}
}
#IBOutlet weak var TimerLabel: UILabel!
#IBAction func IncreaseCountdown(sender: AnyObject) {
countdown++
TimerLabel.text = String(countdown)
}
#IBAction func StartCountdown(sender: AnyObject) {
runTimer()
}
#IBAction func StopCountdown(sender: AnyObject) {
timer.invalidate()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
The simplest approach is just to format the counter as you display it -
#IBOutlet weak var timerLabel: UILabel!
var timer = NSTimer()
var countdown = 0
func runTimer() {
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector:Selector("updateTimer"), userInfo: nil, repeats: true)
}
func updateTimer() {
if --countdown < 1 {
timer.invalidate()
countdown=0;
}
self.updateTimerLabel();
}
#IBAction func IncreaseCountdown(sender: AnyObject) {
countdown++
self.updateTimerLabel()
}
#IBAction func StartCountdown(sender: AnyObject) {
runTimer()
}
#IBAction func StopCountdown(sender: AnyObject) {
timer.invalidate()
}
func updateTimerLabel() {
TimerLabel.text =NSString(format:"%02d:%02d",seconds/60,seconds%60)
}
}
Note that I also changed TimerLabel to timerLabel as the convention is that variables should start with a lowercase letter
Related
i have got a problem with my Counter application. When starting the counter everything works fine, but the label is showing the initial value 0.0 plus in addition the new time += 0.2. See the result at the foto below. Does anyone have a solution? Thank you!
import UIKit
class CounterViewController: UIViewController {
var time = 0.0
var timer = Timer()
#IBOutlet weak var timerLabel:UILabel!
#IBAction func startCounter(_ sender:UIButton){
timer = Timer.scheduledTimer(timeInterval: 0.2, target: self, selector:#selector(CounterViewController.startAction), userInfo:nil, repeats: true)
}
#IBAction func pauseCounter(_ sender:UIButton){
timer.invalidate()
}
#IBAction func stopCounter(_ sender:UIButton){
timer.invalidate()
time = 0.0
timerLabel.text = "\(time)"
}
#objc func startAction(){
time += 0.2
timerLabel.text = "\(time)"
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
see label twice
Try this in timer declaration
var timer:Timer?
and set this in viewDidLoad
timerLabel.text = "0.0"
Also check your xib or storyboard If added same label twice
I have a label. And I have 3 strings. I need to display text of 3 strings in the same label with delay of 10 seconds over a infinite loop. How can i solve this with simple animations in swift 3?
That's my solution. Just connect a UILabel to the IBOutlet
class ViewController: UIViewController {
#IBOutlet weak var textLabel: UILabel!
let messages = ["PROFESSIONAL AND BEST LEARNING CENTER","LEARNING TECHNOLOGY AND DESIGN IN A SMART WAY","EXPLORE YOUR SKILLS"]
let delayTime = 10.0
var counter = 0
override func viewDidLoad() {
super.viewDidLoad()
let timer = Timer.scheduledTimer(timeInterval: delayTime, target: self, selector: #selector(changeDisplayedText), userInfo: nil, repeats: true)
timer.fire()
}
func changeDisplayedText() {
textLabel.text = messages[counter % messages.count]
counter += 1
}
}
This will work for your, connect your outlet properly and declare those string in an array and load it with timer change.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var label: UILabel!
var array = ["Aaaaaaaaaaa", "Bbbbbbbbbbb", "Ccccccccccc"]
var scrollIndex = 0
override func viewDidLoad() {
super.viewDidLoad()
let timer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(self.myDelayedFunction), userInfo: nil, repeats: true)
timer.fire()
}
func myDelayedFunction()-> Void {
let count = self.array.count
if scrollIndex == count {
scrollIndex = 0
}
if scrollIndex < count {
if count > 1{
self.label.text = array[scrollIndex]
self.scrollIndex += 1
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Start of app (Main storyboard but it's fine in simulator to.)
After clicking play button in mutates into something that isn't a 1,2,3....
Here is the code in the viewcontroller and it's not much. Pretty simple stopwatch app.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var timeLabel: UILabel!
var counter = 0
var clock : Timer = Timer()
override func viewDidLoad() {
timeLabel.text = String(counter)
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func playButton(_ sender: Any) {
clock = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
}
func updateTimer(){
timeLabel.text = String(describing: counter += 1)
}
#IBAction func pauseButton(_ sender: Any) {
clock.invalidate()
}
#IBAction func resetButton(_ sender: Any) {
clock.invalidate()
counter = 0
timeLabel.text = String(counter)
}
}
I've tried setting constraints but that just shoots out "ambiguous" warnings all over the place. Any suggestions? Ideas?
The problem is at your updateTimer method. You need to move your variable increment to the line above and then create a string representation from the variable:
func updateTimer() {
counter += 1
timeLabel.text = String(counter)
}
This question already has an answer here:
Swift Version 2 Bool [closed]
(1 answer)
Closed 7 years ago.
I'm having some issues running an App using Swift v2. Stopping the tmrRun is one issue. I know how to [tmrRun invalidate] in Objective C but not in Swift v2. Any help would be appreciated.
class ViewController: UIViewController {
#IBOutlet var imvWolf: UIImageView!
#IBOutlet var btnGo: UIButton!
#IBOutlet var btnStop: UIButton!
#IBOutlet var sliSpeed: UISlider!
var pic = 0
var tmrRun: NSTimer?
#IBAction func startRunnng(sender: UIButton)
{
tmrRun = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: "update", userInfo: nil, repeats: true)
btnGo.userInteractionEnabled = false
btnStop.userInteractionEnabled = true
sliSpeed.userInteractionEnabled = false
}
#IBAction func stopRunnng(sender: UIButton)
{
[tmrRun invalidate]
btnGo.userInteractionEnabled = true
btnStop.userInteractionEnabled = false
sliSpeed.userInteractionEnabled = true
}
func takeABound() -> ()
{
pic += 1;
if (pic == 8){
pic = 0;
}
}
override func viewDidLoad() {
super.viewDidLoad()
pic = 0;
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
To stop the timer is definitely timername.invalidate(), but remember you have to also stop the animation:
timername.invalidate()
isAnimating = false
If doing this you still have issues I think the problem it's where you're typing it.
Try it using this code:
#IBAction func playButton(sender: AnyObject) {
moveWolf()
}
#IBAction func pauseButton(sender: AnyObject) {
timername.invalidate()
isAnimating = false
}
func moveWolf(){
//Here instead of 0.1 you set the slider value
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("doAnimation"), userInfo: nil, repeats: true)
isAnimating = true
}
func doAnimation(){
if counter == 5 {
counter = 1
}
else{
counter++
}
imatge.image = UIImage(named: "picture\(counter).png")
}
Hope that helps you! Good luck!
How do i stop my timer from running? Not like a pause, but a stop.
import UIKit
class LastManStandingViewController: UIViewController {
#IBOutlet weak var timeLabel: UILabel!
#IBOutlet weak var timeTextbox: UITextField!
#IBOutlet weak var startButton: UIButton!
#IBOutlet weak var stopButton: UIButton!
var myCounter = 0
var myTimer : NSTimer = NSTimer()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
timeLabel.text = String(myCounter)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func startTimer(){
myTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTimer"), userInfo: nil, repeats: true)
println("func startTimer")
}
func stopTimer(){
myTimer.invalidate()
myCounter = 0
timeLabel.text = String(myCounter)
println("func stopTimer")
}
func updateTimer(){
timeLabel.text = String(myCounter++)
println("func updateTimer")
}
#IBAction func startButton(sender: AnyObject) {
startTimer()
}
#IBAction func stopButton(sender: AnyObject) {
stopTimer()
}
}
I can start the timer, but when i press the Stop button, it reset itself, and starts counting again. It doesn't stop.
Made it work. Something was buggy with my project! Fixed it by removing the button and re-adding them. Looks like i had a duplicate or something.
You don't have to use Selector:
#IBAction func startButton(sender: AnyObject) {
myTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "updateTimer:", userInfo: nil, repeats: true)
}
Also, the timer passes itself to the selected method, so you can invalidate it inside the method if you need:
func updateTimer(timer: NSTimer) {
timeLabel.text = String(Counter++)
timer.invalidate()
}
Or if the timer is an instance variable:
myTimer.invalidate()
myTimer = nil
It's a good thing to nil the instance variable timer after having invalidated it, it avoids further confusion if you need to create another timer with the same variable. Also, method names and variables should begin with a lowercase letter.
Screenshot to show the timer invalidated and set to nil.
Update for Swift 2.2+
See https://stackoverflow.com/a/36160191/2227743 for the new #selector syntax replacing Selector().
you can use this when some condition is met and you want to stop timer:
Timer.invalidate()
Here is simple example :
func UpdateTimer(){
timeLabel.text = String(Counter++)
if timeLabel.text == String("5") {
Timer.invalidate()
}
}
this will stop timer.
You can modify it as per your need.
As pre Swift 2.2
let printTimer = NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: #selector(printDescription), userInfo: nil, repeats: true)
func printDescription() {
print("Print timer will print every after 2.0 seconds")
}
Print timer will print every after 2.0 seconds