IOS从指定的数字开始计算分钟数

时间:2017-03-10 13:20:59

标签: swift3 xcode8

在我的足球秒表中,我试图让我的计数器从45:00开始,当第二个哈弗被点击时被触摸但无法弄清楚,我已经尝试将分钟设置为45:00,当我触摸开始再次计时,但计数器仍然从00:00开始。有人可以告诉我吗? 提前致谢

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

1 个答案:

答案 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 firstHalfTime = true