0

我下面的快速代码可能需要几秒钟,然后从放置在文本字段中的内容开始倒计时。我希望用户能够输入 5:30 并且倒计时 330 秒。现在为了让用户这样做,我必须写 330 秒而不是 5:30。

   import UIKit

   class ViewController: UIViewController {

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


}



@objc func startHit() {
    timer.invalidate()
    if let counterText = enterTime.text, let counterValue = Int(counterText) {
        counter = counterValue
        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()
    }
}

}

4

2 回答 2

1

你可以试试

let str = "5:30"
let arr = str.components(separatedBy:":")
let fr = Int(arr.first!)!
let la = Int(arr.last!)!
count = fr * 60 + la // 330

重新格式化

let lef = count / 60
let rig = count % 60
let res = "\(lef):\(rig)"
于 2019-12-22T23:16:29.323 回答
0

您可以在两个函数中提取该功能,例如

// Formatting from displayed string to seconds
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
    }
}

// Formatting from seconds to displayed string
func convertToTimestring(from seconds: Int) -> String {
    let formatter = DateComponentsFormatter()
    formatter.allowedUnits = [.hour, .minute, .second]
    formatter.unitsStyle = .positional
    let formattedString = formatter.string(from: TimeInterval(seconds))!
    return formattedString
}

然后像这样使用它

let timeSting = "03:30"
let seconds = convertToSeconds(from: timeSting)

或者还有几个小时

let timeSting = "01:03:30"
let seconds = convertToSeconds(from: timeSting)

所以你的hitStart功能可能看起来像这样

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

所以你的timerAction功能可能看起来像这样

@objc func timerAction() {
    counter -= 1
    lblTime.text = convertToTimestring(from: counter)
    if ( counter == 0 ) {
        timer.invalidate()
    }
}
于 2019-12-22T23:40:51.257 回答