2015-08-29 39 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) 

    }