【问题标题】:How do I set seconds to 00 when increasing or decreasing my countdown timer?增加或减少倒数计时器时,如何将秒数设置为 00?
【发布时间】:2017-09-10 03:58:01
【问题描述】:

我使用 NSTimer 构建倒数计时器,我使用两个按钮来增加和减少时间。我希望能够在增加或减少时间时将倒数计时器秒数设置为 00。例如,假设计时器在 10:42,我按下减少按钮,一旦它应该转到 10:00 而不是 9:42。我怎么能做到这一点?这是我目前用来增加和减少时间的代码:

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)

}

【问题讨论】:

    标签: swift nstimer


    【解决方案1】:

    要将值增加到下一整分钟,您可以执行以下操作:

    • 除以 60 将秒转换为分钟,然后加一。
    • 四舍五入到下一个整数以获得完整的分钟, 然后再次乘以 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
    

    【讨论】:

    • 当我按下减少按钮时,它会下降 1 秒,当我按下增加按钮时,它会上升整整 60 秒,而秒数不会设置为 00。
    • 顺便说一句,我的 timeCount 变量设置为 1800 秒。
    • @coding22 我的解决方案假定 timeCount 是一个 Int。
    • 哦,我的错,我应该把它放在操作中。如果将其设置为 1800 等特定值,您知道该怎么做吗?
    • 感谢您的帮助,但我遇到了问题。当我按下增加或减少按钮时,它不会增加或减少时间。我更新了操作中的代码。你能检查我是否正确调用它?谢谢!
    【解决方案2】:

    试试这个:

    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)
    }
    

    【讨论】:

    • 当我使用您的代码增加时间时,它会增加 30 秒,并且不会将秒数设置为 00。因此,当它在 30:28 时,它会变为 30:58。当我减少时间时,它减少了 31 秒。
    猜你喜欢
    • 2015-08-24
    • 2023-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-02
    相关资源
    最近更新 更多