2017-09-10 9 views
0

私はNSTimerを使ってカウントダウンタイマーを作成し、2つのボタンを使って時間を増減します。時間を増減するときに、カウントダウンタイマー秒を00に設定したいと思っています。たとえば、タイマーが10時42分で、9時42分ではなく、10時になると減少ボタンを押します。どのように私はそれを行うことができるでしょうか?ここでImは時間を増減することが現在使用してコードは次のとおりです。カウントダウンタイマーを増減するにはどのように秒を00に設定しますか?

var timeCount:TimeInterval = 1800 //seconds 



func startTimer() { 
    focusTimer.position = CGPoint(x: self.size.width/2, y: self.size.height/1.5) 
    focusTimer.fontName = "Helvetica" 
    focusTimer.text = "30 00" 
    focusTimer.fontColor = UIColor.white 
    focusTimer.fontSize = 50 
    focusTimer.zPosition = 79 
    focusTimer.name = "focustimer" 
    addChild(focusTimer) 
} 

func timeStringForScore3(_ time:TimeInterval) -> String { 
    let minutes = Int(time)/60 % 60 
    let seconds = Int(time) % 60 
    return String(format:"%02i %02i", minutes, seconds) 
} 

func updateStopWatch3() { 
    self.timeCount -= 1 
    focusTimer.text = timeStringForScore3(timeCount) 
} 



@discardableResult 
func nextMinute(after seconds: TimeInterval) -> TimeInterval { 
    return (seconds/60 + 1).rounded(.down) * 60 
} 

@discardableResult 
func previousMinute(before seconds: TimeInterval) -> TimeInterval { 
    return (seconds/60 - 1).rounded(.up) * 60 
} 



override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 


for touch in touches { 



if node.name == "increase" { 
nextMinute(after: timeCount) 
focusTimer.text = timeStringForScore3(timeCount) 
} 


if node.name == "decrease" { 
previousMinute(before: timeCount) 
focusTimer.text = timeStringForScore3(timeCount) 

} 

答えて

1

を:

  • 秒で分単位に変換して60分で除算し、1を加算します。
  • フル分を得るには次の整数に切り捨て、 を入力し、60をもう一度掛けて値を秒に変換します。

応じて作品を下げる:

func nextMinute(after seconds: TimeInterval) -> TimeInterval { 
    return (seconds/60 + 1).rounded(.down) * 60 
} 

func previousMinute(before seconds: TimeInterval) -> TimeInterval { 
    return (seconds/60 - 1).rounded(.up) * 60 
} 

テストケース:

nextMinute(after: 1800.0) // 1860 
nextMinute(after: 1800.1) // 1860 
nextMinute(after: 1799.9) // 1800 
nextMinute(after: 0.0) // 60 
nextMinute(after: -15.0) // 0 
nextMinute(after: -60.0) // 0 
nextMinute(after: -61.0) // -60 

previousMinute(before: 1800.0) // 1740 
previousMinute(before: 1800.1) // 1800 
previousMinute(before: 1799.9) // 1740 
previousMinute(before: 0.0) // -60 
previousMinute(before: -15.0) // -60 
previousMinute(before: -60.0) // -120 
previousMinute(before: -61.0) // -120 
+0

私が減少ボタンを押すと1秒ずつ下がり、増加ボタンを押すと完全に60秒になり、秒は00に設定されません。 – coding22

+0

私のtimeCount変数は1800秒に設定されています。 – coding22

+0

@ coding22私の解は、timeCountがIntであると仮定しています。 –

1

このお試しください:次のように進むことができ、次のフル分に値を増やすには

if node.name == "increase" { 

     timeCount = timeCount + (60 - (timeCount % 60)) 
     focusTimer.text = timeStringForScore3(timeCount) 
    } 


if node.name == "decrease" { 
     timeCount = timeCount - (timeCount % 60) 
     focusTimer.text = timeStringForScore3(timeCount) 
} 
+0

私はあなたのコードを使用して時間を増やした場合、それは30秒で、それを増加し、00秒に設定されていません。だから30時28分に30時58分に行く。そして私が時間を短縮すると、それは31秒だけ減少します。 – coding22

関連する問題