iOS视图类似于Android上的Chronometer

时间:2012-11-27 15:13:51

标签: android ios view chronometer

在Android上,存在一个名为Chronometer details here的标准视图。 iOS是否有类似的视图或类似的库?

3 个答案:

答案 0 :(得分:1)

我还是没有找到这个,所以我创建了一个带有GPL许可证的要点here,如果有人兴奋地把它变成了lib:)

答案 1 :(得分: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 方法并增加一些实例变量。

答案 2 :(得分:0)

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

UIDatePicker* datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 320, 206)];
datePicker.datePickerMode = UIDatePickerModeCountDownTimer;
datePicker.countDownDuration = 60*5;
[self.view addSubview:datePicker];