Jak mogę wykonać odliczanie przy pomocy NSTimer
używając Swift?Jak mogę odliczać z NSTimer?
Odpowiedz
Pytanie 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--)
}
}
Pytanie 2:
można zrobić jedno i drugie. SpriteKit to SDK, którego używasz do tworzenia scen, ruchu itp. Prosta aplikacja View to szablon projektu. Nie powinny one kolidować
zmiennej do zaprogramowania
var timer = 60
NSTimer z 1,0 w przedziale
var clock = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "countdown", userInfo: nil, repeats: true)
Tutaj można zmniejszyć licznik
func countdown() {
timer--
}
Dzięki za #selector :))))) (Y) –
powinien być selektorem ("odliczanie") – Khuong
@ khuong291: Dzięki, zredagowałem odpowiedź. –
w Swift 3.0 to będzie działać :
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
}
Bądź Countdown aplikacji 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. Jak powstrzymać/unieważnić NStimer
- 2. Jak zatrzymać NSTimer
- 3. NSTimer - jak opóźnić Swift
- 4. Jak zatrzymać NSTimer?
- 5. Swift, jak korzystać z tła NSTimer?
- 6. NSTimer z anonimową funkcją/blokiem?
- 7. NSTimer z aplikacją paska menu
- 8. Jak zatrzymać zdarzenie NStimer?
- 9. Dokładność NSTimer
- 10. Jak zresetować NSTimer? kod swift
- 11. IOS: zatrzymać NSTimer
- 12. Jak uruchomić NSTimer od razu?
- 13. podczas pętli, wiecznie uciekać lub odliczać
- 14. Wywołanie metody NSTimer
- 15. NSTimer nie uruchamia wybieraka
- 16. NSTimer nie strzela
- 17. Swift NSTimer userinfo
- 18. Jak wiadomo, kiedy do unieważnienia obiekt `NSTimer`
- 19. NSTimer z wieloma przedziałami czasowymi w sekwencji
- 20. NSTimer i NSRunLoop
- 21. Jak "sprawdzić poprawność" NSTimer po unieważnieniu go?
- 22. Co możemy użyć zamiast nstimer?
- 23. NSTimer zarządzanie pamięcią
- 24. NSTimer nie zatrzymuje się
- 25. NSTimer z blokiem - czy robię to dobrze?
- 26. jak zatrzymać i wznowić NSTimer w iphone
- 27. jak rozstać NSTimer w celu-c
- 28. NSTimer userInfo. Jak obiekt przechodzi do selektora?
- 29. Przekazywanie danych przez NSTimer UserInfo
- 30. NSTimer wyłącza dealloc w UIView
Dziękuję bardzo :) –
Zastosowanie (update) w Swift 2.2 –