2017-02-21 94 views
0

我有一个秒表,我需要禁用停止按钮,直到经过的时间或更长时间,我有它的工作,但如果我要点击停止按钮,在经过的时间发生),然后点击它时间已经过去,它不起作用。如果我启动定时器,并等待或让它通过,如果>10秒它工作正常启用/禁用Xcode 8中的按钮

@IBAction func firstHalfClicked(_ sender: UIButton) { 

     if startStopWatch == true { 
      timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(StopWatchVC.updateStopwatch), userInfo: nil, repeats: true) 
      startStopWatch = false 
      self.firstHalf.alpha = 0.2 
      self.secondHalf.alpha = 0.2 
      self.endFirstHalf.alpha = 0.2 
      self.endSecondHalf.alpha = 0.2 
      seconds = 0 
      minutes = 0 
      tempTimelineLbl.text = "Kick Off here at " 
     } 
    } 

    @IBAction func endFirstHalfClicked(_ sender: UIButton) { 

     if seconds < 10 { 
      endFirstHalf.isEnabled = false 
     } else { 
      if startStopWatch == false && seconds > 10 { 
       endFirstHalf.isEnabled = true 
       timer.invalidate() 
       startStopWatch = true 
       seconds = 0 
       minutes = 45 
       stopwatchLabel.text = "45:00" 
       tempTimelineLbl.text = "Half Time" 
       self.secondHalf.alpha = 1 
       self.firstHalf.alpha = 0.2 
       self.endFirstHalf.alpha = 0.2 

      } 
     } 
    } 

回答

0

当前正在决定是否该按钮应该启用或不后用户点击它。因此,当您在10秒钟之前点击停止按钮时,该按钮被禁用,这意味着无法再次点击按钮,因为没有任何东西可以告诉按钮再次启用。

您的firstHalfClicked方法应禁用停止按钮。你的updateStopwatch方法应该启用按钮一次10秒(或任何你想要的时间间隔)通过。 endFirstHalfClicked应该只包含else区块中的代码减去启用endFirstHalf的代码行。

+0

完美感 – Chet