从总秒数到分钟和秒的格式倒数

问题描述 投票:0回答:1

下面的我的快速代码允许用户输入分钟数,然后在输入分钟数时将其倒计时。但是,如果用户输入12:00,它将从720开始递减计数。我希望它对其进行格式化以使其像12:00、11:59等那样递减计数。 Func timerAction是所有这些发生的地方。

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {
let MAX_LENGTH_PHONENUMBER = 2
let ACCEPTABLE_NUMBERS     = "0123456789"
var enterTime = UITextField()
var lblTime = UILabel()
var startBTN = UIButton()
var timer = Timer()
var counter = 0

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    [enterTime,lblTime,startBTN].forEach{
        $0.backgroundColor = .systemRed
        view.addSubview($0)
        $0.translatesAutoresizingMaskIntoConstraints = false
    }

    enterTime.frame = CGRect(x: view.center.x-115, y: view.center.y-200, width: 60, height: 50)
    lblTime.frame = CGRect(x: view.center.x-115, y: view.center.y, width: 60, height: 50)
    startBTN.frame = CGRect(x: view.center.x-115, y: view.center.y+200, width: 60, height: 50)

    startBTN.addTarget(self, action: #selector(startHit), for: .touchDown)

    enterTime.placeholder = String("MM:SS")

    enterTime.delegate = self
    enterTime.addTarget(self, action: #selector(self.textFieldDidChange), for: UIControl.Event.editingChanged)
}



@objc func startHit() {
    timer.invalidate()
    counter = convertToSeconds(from: enterTime.text!)
    lblTime.text = String(counter)
    timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
}

@objc func timerAction() {
    counter -= 1
    lblTime.text = String(counter)
    if ( counter == 0 ) {
        timer.invalidate()
    }
}

func convertToSeconds(from timeString: String) -> Int {
    let components = timeString.components(separatedBy: ":")
    if components.count == 2 {
        let minutes = Int(components[0]) ?? 0
        let seconds = Int(components[1]) ?? 0
        return (minutes * 60) + seconds
    } else if components.count == 3 {
        let hours = Int(components[0]) ?? 0
        let minutes = Int(components[1]) ?? 0
        let seconds = Int(components[2]) ?? 0
        return (hours * 60 * 60) + (minutes * 60) + seconds
    } else {
        return 0
    }
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    let newLength: Int = textField.text!.count + string.count - range.length
    let numberOnly = NSCharacterSet.init(charactersIn: ACCEPTABLE_NUMBERS).inverted
    let strValid = string.rangeOfCharacter(from: numberOnly) == nil
    return (strValid && (newLength <= MAX_LENGTH_PHONENUMBER))
}



@objc func textFieldDidChange(_ textField: UITextField) {
    if  textField.text!.count == 2  {
        textField.text = textField.text! + ":00"
    }
}
}
swift int countdowntimer uitextfielddelegate
1个回答
0
投票

现在,您只是在这两个位置将表示秒数的整数直接转换为字符串:

@objc func startHit() {
    timer.invalidate()
    counter = convertToSeconds(from: enterTime.text!)
    lblTime.text = String(counter) <--------
    timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
}

@objc func timerAction() {
    counter -= 1
    lblTime.text = String(counter) <--------
    if ( counter == 0 ) {
        timer.invalidate()
    }
}

您需要更改这两个位置以使用更复杂的格式化逻辑,以将类似720转换为12:00

@objc func startHit() {
    timer.invalidate()
    counter = convertToSeconds(from: enterTime.text!)
    lblTime.text = formatSeconds(counter)
    timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
}

@objc func timerAction() {
    counter -= 1
    lblTime.text = formatSeconds(counter)
    if ( counter == 0 ) {
        timer.invalidate()
    }
}

这是formatSeconds的实现,它使用DateComponentsFormatter。这是一个格式化程序,非常适合格式化这样的时间。

func formatSeconds(_ totalSeconds: Int) -> String {
    let formatter = DateComponentsFormatter()
    formatter.unitsStyle = .positional
    formatter.allowedUnits = [.minute, .second]
    formatter.zeroFormattingBehavior = .pad
    return formatter.string(from: TimeInterval(totalSeconds))!
}
© www.soinside.com 2019 - 2024. All rights reserved.