Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a countdown with NSTimer?

How can I make a countdown with an NSTimer using Swift?

like image 451
Giovanie Rodz Avatar asked Mar 31 '15 17:03

Giovanie Rodz


People also ask

How do you create a 60s countdown timer that prints out the seconds remaining every second in Swift?

Underneath the timerLabel outlet create the following variables: var seconds = 60 //This variable will hold a starting value of seconds. It could be any amount above 0. var timer = Timer() var isTimerRunning = false //This will be used to make sure only one timer is created at a time.

How do I start a countdown timer in Swift?

Creating a non-repeating timerlet timer1 = Timer. scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(fireTimer), userInfo: nil, repeats: false) let timer2 = Timer. scheduledTimer(withTimeInterval: 1.0, repeats: false) { timer in print("Timer fired!") }


1 Answers

Question 1:

@IBOutlet var countDownLabel: UILabel!  var count = 10  override func viewDidLoad() {     super.viewDidLoad()      var timer = Timer.scheduledTimer(timeInterval: 0.4, target: self, selector: #selector(UIMenuController.update), userInfo: nil, repeats: true) }  func update() {     if(count > 0) {         countDownLabel.text = String(count--)     } } 

Question 2:

You can do both. SpriteKit is the SDK you use for scene, motion, etc. Simple View Application is the project template. They should not conflict

like image 62
Bigman Avatar answered Oct 25 '22 17:10

Bigman