2016-04-01 20 views
0

Belirli bir tarih ve saatte bir zamanlayıcı başlatmak istiyorum, o zaman oyunun geri kalanında bu başlangıç ​​saatini oyun zamanlayıcı olarak kullanın. "TimeIntervalSinceDate" kullanımı bana saniye verir ama sonra gameTimerLabel'da görüntülenecek saniye olmaya çalışmayacaktır. Buna yanlış şekilde gelebilirim. Herhangi bir tavsiye edilir.Game Timer NSTimer ile NSDate kullanıyor? swift

override func viewWillAppear(animated: Bool) { 
    let dateFormatter = NSDateFormatter() 


    dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss zzz" 
    let dateAsString1 = "Fri, 1 April 2016 11:30:00 MST" 
    let date1 = dateFormatter.dateFromString(dateAsString1)! 
    var currentTime = NSDate() 
    var counter = 0 
    gameTimerLabel.text = String(counter) 
    gameTimerLabel.text = counter //<- Error:Cannot assign value to type 'Int' to type 'String?' 
    counter = date1.timeIntervalSinceDate(currentTime) //<- Error:Cannot assign value of type 'NSTimeInterval' (aka 'Double') to type 'Int' 

} 
+0

Ben bu değişiklikleri eklemek ettik Çift –

+0

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? –

cevap

0

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) 
} 
} 
+0

olarak –

+0

Bunun için bir NSTimer kullanmalısınız, size birkaç dakika içinde cevapta bir alternatif vereyim. –

+0

@ Ring-Jarvi Cevabımı zamanlayıcı kullanarak alternatif bir yaklaşımla güncelledim. –