我下面的快速代码可能需要几秒钟的时间并从文本字段中倒数,我希望用户能够输入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()
}
}
}
答案 0 :(得分: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)"
答案 1 :(得分:0)
您可以通过两个函数来提取该功能,例如
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
defaultConfig {
applicationId "com.example.usub"
minSdkVersion 26
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.2.0-alpha01'
implementation 'com.firebase:geofire-android:2.1.2'
implementation 'com.github.kmenager:material-animated-switch:1.2.2'
implementation 'com.squareup.retrofit2:retrofit:2.7.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.7.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.firebase:firebase-database:19.2.0'
implementation 'com.google.firebase:firebase-auth:19.2.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'com.github.d-max:spots-dialog:0.7@aar'
implementation "androidx.cardview:cardview:1.0.0"
implementation 'uk.co.chrisjenx:calligraphy:2.3.0'
implementation 'com.rengwuxian.materialedittext:library:2.1.4'
implementation 'com.google.android.gms:play-services-maps:17.0.0'
//implementation 'com.google.android.gms:play-services:17.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.google.firebase:firebase-analytics:17.2.1'
implementation 'com.google.android.gms:play-services-location:17.0.0'
}
apply plugin: 'com.google.gms.google-services'
然后像使用它
// 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