burada şeylerin sayacı bildirirken, bir .0 veya ekleyerek Çeşidi
var counter = 0
Bir çift olarak ilan edebilir Int
olduğu sonucuna oluyor
Birincisi, bir çift türünü belirtme türü:
var counter: NSTimeInternval = 0.0
Ardından, sayı değişkenini görüntülemek için dize birlikte çalışabilirliğini kullanabilirsiniz. dize, böyle: Burada
gameTimerLabel.text = "\(counter)"
bir sayaç olarak bir NSTimer kullanarak bir örnek görünüm denetleyicisi, öyle saniyede sayar: zamanlayıcılar kullanarak her zaman olmayabilir olduğunda
class ViewController: UIViewController {
// Setup a timer and a counter for use
var timer = NSTimer()
var counter : NSTimeInterval = 0
@IBOutlet weak var label: UILabel!
// Invalidest the timer, resets the counter and udpates the label
@IBAction func resetTimer(sender: AnyObject) {
// Invalidate the timer, reset the label.
self.timer.invalidate()
self.label.text = ""
self.counter = 0
}
// A button that when pressed starts and stops the timer
// There's no pause/resume, so it's invalidated & created again
// But the counter value reamins the same
@IBAction func timerBttnTouched(sender: AnyObject) {
if self.timer.valid {
self.timer.invalidate()
} else {
self.setupTimer()
}
}
// Does the actual counting everytime the timer calls this method
func timerFired() {
self.counter += 1
self.label.text = "\(self.counter)"
}
// Setups a timer, adds it to the run loop and specifies what method should fire when the timer fires
func setupTimer() {
// Setupt the timer, this will call the timerFired method every second
self.timer = NSTimer(
timeInterval: 1,
target: self,
selector: #selector(self.timerFired),
userInfo: nil,
repeats: true)
// Add the timer to the run loop
NSRunLoop.currentRunLoop().addTimer(
self.timer,
forMode: NSDefaultRunLoopMode)
}
}
önemli şey nota İhtiyaç duyduğunuzda, tam olarak olarak adlandırılır, bu bir zamanlayıcı kullanıldığında istenen hassasiyete göre dikkate alınmalıdır.
Yorumlarda ele alındığı gibi, iki tarihi karşılaştıran bir yöntemi tetiklemek için bir zamanlayıcı kullanan ve ekran için bir dize oluşturmak üzere bir NSDateComponentsFormatter kullanan çözüm. İlk tarih viewDidLoad oluşturulur ancak herhangi bir yerde oluşturulabilir:
class ViewController: UIViewController {
// Setup a timer and a counter for use
var timer = NSTimer()
var counter : NSTimeInterval = 0
var startDate: NSDate?
override func viewDidLoad() {
// Set the initial date
self.startDate = NSDate()
}
@IBOutlet weak var label: UILabel!
// Invalidest the timer, resets the counter and udpates the label
@IBAction func resetTimer(sender: AnyObject) {
// Invalidate the timer, reset the label.
self.timer.invalidate()
self.label.text = ""
self.counter = 0
}
// A button that when pressed starts and stops the timer
// There's no pause/resume, so it's invalidated & created again
// But the counter value reamins the same
@IBAction func timerBttnTouched(sender: AnyObject) {
if self.timer.valid {
self.timer.invalidate()
} else {
self.setupTimer()
}
}
// Does the actual counting everytime the timer calls this method
func timerFired() {
let now = NSDate()
let difference = now.timeIntervalSinceDate(self.startDate!)
// Format the difference for display
// For example, minutes & seconds
let dateComponentsFormatter = NSDateComponentsFormatter()
dateComponentsFormatter.stringFromTimeInterval(difference)
self.label.text = dateComponentsFormatter.stringFromTimeInterval(difference)
}
// Setups a timer, adds it to the run loop and specifies what method should fire when the timer fires
func setupTimer() {
// Setupt the timer, this will call the timerFired method every second
self.timer = NSTimer(
timeInterval: 1,
target: self,
selector: #selector(self.timerFired),
userInfo: nil,
repeats: true)
// Add the timer to the run loop
NSRunLoop.currentRunLoop().addTimer(
self.timer,
forMode: NSDefaultRunLoopMode)
}
}
Ben bu değişiklikleri eklemek ettik Çift –
sayaç beyan edilmelidir dizeye zaman aralığını dönüştürmek gerekir. Şimdi görüntüleniyor. Bunu nasıl sayabilirim? veya doğru sayılacak zamanı güncellemek mi istiyorsunuz? –