如何使用Swift與NSTimer
進行倒計時?如何使用NSTimer進行倒計時?
回答
問題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--)
}
}
問題2:
你都可以做。 SpriteKit是您用於場景,動畫等的SDK。簡單視圖應用程序是項目模板。他們不應該發生衝突
變量爲您定時器
var timer = 60
的NSTimer以1.0爲間隔
var clock = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "countdown", userInfo: nil, repeats: true)
在這裏,您可以減少定時器
func countdown() {
timer--
}
太謝謝:)))))(Y) – 2015-03-31 17:47:36
應該是選擇器(「倒計時」) – Khuong 2016-03-12 03:22:21
@ khuong291:謝謝,我編輯了答案。 – 2016-03-13 21:16:55
在雨燕3.0,這將工作:
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
}
讓倒計時程序的Xcode 8.1,斯威夫特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
}
}
斯威夫特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. 使用UIProgressView和NSTimer執行倒計時
- 2. Objective-C:用NSTimer倒計時
- 3. 如何進行倒計時?
- 4. 使用NSTimer創建倒計時
- 5. NSTimer毫秒倒計時
- 6. NSTimer倒計時標籤
- 7. 如何進行倒計時Swift
- 8. 如何進行全球倒計時?
- 9. 如何使用NSDate運行倒計時?
- 10. 1-2秒延遲NStimer倒計時
- 11. 如何使用秒和毫秒進行倒計時
- 12. 使用UIDatePicker在iOS中進行計時器倒計時
- 13. NSTimer倒計時跳過上一秒(不使用最後毫秒)
- 14. 用毫秒創建倒數計時器(NSTimer),但它不能正確倒計數
- 15. 倒計時,進度
- 16. 進入倒計時
- 17. 如何在C#中用Console.Clear()和多線程進行倒計時
- 18. Android如何使倒計時像倒計時鐘?
- 19. 如何使用倒計時jquery插件
- 20. 使用datatable在web服務中使用datatable進行倒計時的獨特倒數計時器
- 21. 如何使倒計時運行在後臺使用eclipse
- 22. jQuery倒計時進度條
- 23. 如何使用Lua中的固定分鐘秒數進行倒計時?
- 24. 如何在每月的每個星期日進行倒計時?
- 25. 如何在手錶併發症中進行倒計時?
- 26. 如何在本月的每個星期五進行倒計時?
- 27. 如何在對話框中進行倒計時,並在對話框再次打開時恢復倒計時
- 28. 如何在java(秒錶Java)中進行倒計時CountDown計時器
- 29. 使用javascript的倒計時
- 30. 使用秒錶倒計時
非常感謝:) – 2015-03-31 17:47:22
使用#selector斯威夫特(更新)2.2 – 2016-05-10 12:42:31