2

在 Android 上,这里有一个称为 Chronometer details的标准视图。iOS 是否有类似的视图或执行类似操作的库?

4

3 回答 3

1

无论如何我都没有找到这样做,所以我在这里使用 GPL 许可证创建一个要点,如果有人兴奋地把它变成一个 lib :)

于 2012-11-27T18:15:33.587 回答
1

我会为此使用一个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方法并增加一些实例变量。

于 2021-04-01T14:33:20.853 回答
0

为什么不在定时器模式下使用UIDatePicker ?

UIDatePicker* datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 320, 206)];
datePicker.datePickerMode = UIDatePickerModeCountDownTimer;
datePicker.countDownDuration = 60*5;
[self.view addSubview:datePicker];
于 2012-11-27T15:27:27.247 回答