2017-03-10 11 views
0

私のフットボールのストップウォッチでは、secondHalfClickedがタッチされたときに45:00からカウンタを取得しようとしていますが、分数を45に設定しようとしました:00をタッチしてもう一度タイマーを開始しますが、カウンターはまだ00:00から開始します。誰でも私にアドバイスできますか?事前に おかげでIOS指定された番号から分をカウントアップ

@IBAction func firstHalfClicked(_ sender: UIButton) { 

    if startStopWatch == true { 
     timerStart() 
     startStopWatch = false 
     seconds = 0 
     minutes = 0 

    } 
} 

@IBAction func endFirstHalfClicked(_ sender: UIButton) { 

    if startStopWatch == false { 
     timer.invalidate() 
     startStopWatch = true 
     seconds = 0 
     minutes = 45 
     stopwatchLabel.text = "45:00" 
    } 
} 

@IBAction func secondHalfClicked(_ sender: UIButton) { 

    if startStopWatch == true { 
     timerStart() 
     seconds = 0 
     minutes = 45 
     startStopWatch = false 
     tempTimelineLbl.text = "2nd Half Underway" 
    } 
} 

    @IBAction func endSecondHalfClicked(_ sender: UIButton) { 

     if startStopWatch == false { 
      timer.invalidate() 
      startStopWatch = true 
      seconds = 0 
      minutes = 0 
      stopwatchLabel.text = "90:00" 
      tempTimelineLbl.text = "Full Time" 
     } 
    } 

func timerStart() { 

    timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(StopWatchVC.updateStopwatch), userInfo: nil, repeats: true) 
} 

func updateStopwatch() { 

    let stopWatchString = stopwatch.elapsedTimeSinceStart() 
    stopwatchLabel.text = stopWatchString 
} 


class Stopwatch { 

    var startTime:Date? 

    func startTimer() { 
     startTime = Date(); 
    } 

    func elapsedTimeSinceStart() -> String { 
     var elapsed = 0.0; 
     if let elapsedTime = startTime { 
      elapsed = elapsedTime.timeIntervalSinceNow 
     } 
     elapsed = -elapsed 
     let minutes = Int(floor((elapsed/60))); 
     let seconds = Int(floor((elapsed.truncatingRemainder(dividingBy: 60)))); 
     let timeString = String(format: "%02d:%02d", minutes, seconds) 
     return timeString 
    } 
} 
+0

「分」を45に設定していますが、どこで使用するのかわかりません。 –

+0

私はそれを使用していると思っていました。私はこれが私が苦労している部分だと思う。 – Chet

+0

"それを使う"とは、それを数えたり表示したりするためにどこを使うのかという意味だった。 –

答えて

0

はそれをこのよう

私のクラス

func elapsedTimeSinceStart() -> String { 
     var elapsed = 0.0; 
     if let elapsedTime = startTime { 
      if firstHalfTime { 
       elapsed = elapsedTime.timeIntervalSinceNow 
      } else { 
       elapsed = elapsedTime.timeIntervalSinceNow - 45*60 
      } 
     } 
     elapsed = -elapsed 
     let minutes = Int(floor((elapsed/60))); 
     let seconds = Int(floor((elapsed.truncatingRemainder(dividingBy: 60)))); 
//  print(elapsed) 
     let timeString = String(format: "%02d:%02d", minutes, seconds) 
//  print(timeString) 
     return timeString 

その後、私のIBoutlets

@IBAction func firstHalfClicked(_ sender: UIButton) { 

     if startStopWatch == true { 
      timerStart() 
      firstHalfTime = true 

@IBAction func secondHalfClicked(_ sender: UIButton) { 

     if startStopWatch == true { 
      timerStart() 
      firstHalfTime = false 

が作成されたグローバルVAR

をソートするマネージド
関連する問題