How can I make a countdown with an NSTimer
using 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.
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!") }
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With