[英]iOS view similar to Chronometer on Android
无论如何我都没有找到要做这个,所以我在这里用 GPL 许可证创建了一个要点,如果有人兴奋地把它变成一个 lib :)
我会为此使用Timer
。
var timer = Timer()
timer = Timer(timeInterval: 0.01, target: self, selector: #selector(update), userInfo: nil, repeats: false)
@IBAction func start(sender: UIButton) {
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(ViewController.update(_:)), userInfo: nil, repeats: true)
RunLoop.current.add(timer, forMode: RunLoop.Mode.common)
startTime = Date() // new instance variable that you would need to add.
}
func update() {
let elapsedTime = Date().timeIntervalSince(startTime)
let currTime = totalTime - elapsedTime
//total time is an instance variable that is the total amount of time in seconds that you want
countDown.text = String(currTime)
if currTime < 0 {
timer.invalidate()
//do other stuff that you need to do when time runs out.
}
}
如果您想要更小或更大的时间段,只需更改Interval
参数即可。 此外,您仍然需要解决创建一些逻辑来分割分钟/秒等问题。 使用Date
方法并增加一些实例变量。
为什么不在计时器模式下使用UIDatePicker ?
UIDatePicker* datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 320, 206)];
datePicker.datePickerMode = UIDatePickerModeCountDownTimer;
datePicker.countDownDuration = 60*5;
[self.view addSubview:datePicker];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.