2015-08-29 73 views
0

無效後工作使用Timer.fire()不是斯威夫特

@IBAction func pauseButton(sender: AnyObject) { 
    if isPaused == false { 

     timer.invalidate() 
     isPaused = true 
     displayLabel.text = "\(count)" 
     println("App is paused equals \(isPaused)") 

    } else if isPaused == true { 
     var isPaused = false 
     timer.fire() 
     // timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true) 
    } 
} 

暫停應用後,我想打再次停頓,在這種情況下,定時器將繼續從那裏來算離開。

此外,當我按暫停/播放太多次時,定時器會發生多次,這會導致定時器每秒增加幾次。

請幫忙!

// 
// ViewController.swift 
// Navigation Bars 
// 
// Created by Alex Ngounou on 8/27/15. 
// Copyright (c) 2015 Alex Ngounou. All rights reserved. 
// 


import UIKit 

class ViewController: UIViewController { 

    var timer = NSTimer() 
    var count = 0 
    var isPaused = false 

    func updateTime() { 

     switch count { 
     case 0, 1: 
      count++ 
     println("\(count) second.") 
     displayLabel.text = "\(count)" 

     case 2: 
      count++ 
      println("\(count) seconds.") 
      displayLabel.text = "\(count)" 

     default: 
      count++ 
      println("\(count) seconds.") 
      displayLabel.text = "\(count)" 

     } 
    } 




    **strong text**@IBAction func playButton(sender: AnyObject) { 

     var isPaused = false 

     timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true) 
    } 


    @IBAction func stopButton(sender: AnyObject) { 

     timer.invalidate() 
     count = 0 
     displayLabel.text = "0" 
    } 


    // if it's currently paused, pressing on the pause button again should restart the counter from where it originally left off. 

    @IBAction func pauseButton(sender: AnyObject) { 

     if isPaused == false { 

     timer.invalidate() 
     isPaused = true 
     displayLabel.text = "\(count)" 
     println("App is paused equals \(isPaused)") 

     } else if isPaused == true { 

      var isPaused = false 

      timer.fire() 

      // timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true) 
     } 
    } 


    @IBAction func resetButton(sender: AnyObject) { 

     timer.invalidate() 
     count = 0 
     displayLabel.text = "" 
    } 


    @IBOutlet weak var displayLabel: UILabel! 



    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

    timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true) 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


} 
+0

我意識到這是一個兩個問題,我可以單獨張貼他們如果需要的話。請幫忙! –

+1

喲永遠不應該比較布爾類型爲true。這是多餘的。如果isPaused = False {..},則更改if!isPaused {..}。如果isPaused == true,則更改{..}以if isPaused {..}。您也可以切換isPaused值,從if語句中添加單行。 isPaused =!isPaused –

回答

3

來源:https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/

一旦失效,定時器對象不能被重用。

因此,基本上NSTimer一旦無效就什麼都不會做。在該點之後,必須將您的timer屬性分配給新建的NSTimer對象,以使其再次啓動。如果您的失效簿記準確無誤,則不存在多個定時器的「累積」問題。

雖然對於您的實際問題最簡單的方法可能是邏輯過濾。也就是說,無限期地保持NSTimer對象,並讓它不斷髮射。當存儲的屬性isPausedtrue時,您忽略定時器事件(通過立即從處理函數返回),否則您處理它們。

+0

哇!我真的明白你在說什麼,並且在自拍後拍拍自己,這讓我更加感動。我一定會嘗試這種方法! –

0

一個「懶惰」的做法可能是另有用途:

class ViewController: UIViewController { 

    var timer: Timer? 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     startTimer() 
    } 


    final func killTimer(){ 
     self.timer?.invalidate() 
     self.timer = nil 
    } 


    final private func startTimer() { 

     // make it re-entrant: 
     // if timer is running, kill it and start from scratch 
     self.killTimer() 
     let fire = Date().addingTimeInterval(1) 
     let deltaT : TimeInterval = 1.0 

     self.timer = Timer(fire: fire, interval: deltaT, repeats: true, block: { (t: Timer) in 

      print("hello") 

     }) 

    RunLoop.main.add(self.timer!, forMode: RunLoopMode.commonModes) 

    }