Come posso creare un conto alla rovescia con uno NSTimer
utilizzando Swift?Come posso effettuare un conto alla rovescia con NSTimer?
risposta
Domanda 1:
@IBOutlet var countDownLabel: UILabel!
var count = 10
override func viewDidLoad() {
super.viewDidLoad()
var timer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: Selector("update"), userInfo: nil, repeats: true)
}
func update() {
if(count > 0) {
countDownLabel.text = String(count--)
}
}
Domanda 2:
È possibile fare entrambe le cose. SpriteKit è l'SDK che usi per scene, movimenti, ecc. Simple View Application è il modello di progetto. Non dovrebbero entrare in conflitto
variabile per il timer
var timer = 60
NSTimer con 1.0 come intervallo di
var clock = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "countdown", userInfo: nil, repeats: true)
Qui è possibile diminuire il timer
func countdown() {
timer--
}
Grazie troppo :))))) (Y) –
dovrebbe essere Selector ("conto alla rovescia") – Khuong
@ khuong291: Grazie, ho modificato la risposta. –
In Swift 3.0 questo funzionerà :
var counter = 30
override func viewDidLoad() {
super.viewDidLoad()
var _ = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateCounter), userInfo: nil, repeats: true)
}
func updateCounter() {
//you code, this is an example
if counter > 0 {
print("\(counter) seconds to the end of the world")
counter -= 1
}
fare il conto alla rovescia app Xcode 8.1, Swift 3
import UIKit
import Foundation
class ViewController: UIViewController, UITextFieldDelegate {
var timerCount = 0
var timerRunning = false
@IBOutlet weak var timerLabel: UILabel! //ADD Label
@IBOutlet weak var textField: UITextField! //Add TextField /Enter any number to Countdown
override func viewDidLoad() {
super.viewDidLoad()
//Reset
timerLabel.text = ""
if timerCount == 0 {
timerRunning = false
}
}
//Figure out Count method
func Counting() {
if timerCount > 0 {
timerLabel.text = "\(timerCount)"
timerCount -= 1
} else {
timerLabel.text = "GO!"
}
}
//ADD Action Button
@IBAction func startButton(sender: UIButton) {
//Figure out timer
if timerRunning == false {
_ = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(ViewController.Counting), userInfo: nil, repeats: true)
timerRunning = true
}
//unwrap textField and Display result
if let countebleNumber = Int(textField.text!) {
timerCount = countebleNumber
textField.text = "" //Clean Up TextField
} else {
timerCount = 3 //Defoult Number to Countdown if TextField is nil
textField.text = "" //Clean Up TextField
}
}
//Dismiss keyboard
func keyboardDismiss() {
textField.resignFirstResponder()
}
//ADD Gesture Recignizer to Dismiss keyboard then view tapped
@IBAction func viewTapped(_ sender: AnyObject) {
keyboardDismiss()
}
//Dismiss keyboard using Return Key (Done) Button
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
keyboardDismiss()
return true
}
}
Swift 3
private let NUMBER_COUNT_DOWN = 3
var countDownLabel = UILabel()
var countDown = NUMBER_COUNT_DOWN
var timer:Timer?
private func countDown(time: Double)
{
countDownLabel.frame = CGRect(x: 0, y: 0, width: 300, height: 300)
countDownLabel.font = UIFont.systemFont(ofSize: 300)
countDownLabel.textColor = .black
countDownLabel.center = CGPoint(x: self.view.frame.width/2, y: self.view.frame.height/2)
countDownLabel.textAlignment = .center
self.view.addSubview(countDownLabel)
view.bringSubview(toFront: countDownLabel)
timer = Timer.scheduledTimer(timeInterval: time, target: self, selector: #selector(updateCountDown), userInfo: nil, repeats: true)
}
func updateCountDown() {
if(countDown > 0) {
countDownLabel.text = String(countDown)
countDown = countDown - 1
} else {
removeCountDownLable()
}
}
private func removeCountDownLable() {
countDown = NUMBER_COUNT_DOWN
countDownLabel.text = ""
countDownLabel.removeFromSuperview()
timer?.invalidate()
timer = nil
}
- 1. (NSTimer) creazione di un conto alla rovescia del timer
- 2. C# conto alla rovescia
- 3. Conto alla rovescia per Android
- 4. Attività di Celeria con conto alla rovescia
- 5. PHP Conto alla rovescia fino alla data
- 6. Implementazione di un conto alla rovescia con Servizio in background
- 7. Timer conto alla rovescia in Pygame
- 8. Javascript refresh + conto alla rovescia testo
- 9. più timer conto alla rovescia all'interno di uitableview
- 10. Reindirizza 10 secondi Conto alla rovescia
- 11. tempo timer conto alla rovescia per android
- 12. Come fare un conto alla rovescia in Android?
- 13. Come eseguire il conto alla rovescia con gli osservabili RxJS?
- 14. conto alla rovescia multiplo su singola pagina
- 15. Come si implementa un conto alla rovescia a scorrimento orizzontale?
- 16. come fare un conto alla rovescia in java
- 17. Conto alla rovescia di ProgressBar Android
- 18. Come creare un timer conto alla rovescia wpf?
- 19. Come posso visualizzare un messaggio che esegue il conto alla rovescia ogni secondo con AngularJS?
- 20. Timeout cookie Javascript con timer conto alla rovescia
- 21. Timer conto alla rovescia per Android alla data
- 22. Conto alla rovescia per Natale con Joda-Time
- 23. ListView e voci con timer per il conto alla rovescia
- 24. Come eseguire il conto alla rovescia in ciclo for?
- 25. Come aggiungere tempo al timer per il conto alla rovescia?
- 26. creare un semplice conto alla rovescia di 10 secondi
- 27. come mostrare il conto alla rovescia su uilabel in iphone?
- 28. Conto alla rovescia Javascript utilizzando il fuso orario assoluto?
- 29. Cronometro che esegue il conto alla rovescia in C#
- 30. Javascript, conto alla rovescia e testo del display
Grazie molto :) –
Usa #selector (update) a Swift 2.2 –