2

On Android there exists a standard view called Chronometer details here. Does iOS have a similar view or a library that does something similar?

Handcraftsman
  • 6,863
  • 2
  • 40
  • 33
ademar111190
  • 14,215
  • 14
  • 85
  • 114

3 Answers3

1

I not found anyway todo this, so i create a gist here with GPL license, if someone get excited to make it a lib :)

ademar111190
  • 14,215
  • 14
  • 85
  • 114
1

I would use an Timer for this.

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.
    }
}

If you want a smaller or larger pieces of time, just change it the Interval parameter. Also you still would have to address creating some logic to split minutes / seconds ect. using Date methods and incrementing some instance variable.

Daniel Lyon
  • 1,499
  • 10
  • 15
0

Why not use a UIDatePicker in timer mode?

UIDatePicker* datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 320, 206)];
datePicker.datePickerMode = UIDatePickerModeCountDownTimer;
datePicker.countDownDuration = 60*5;
[self.view addSubview:datePicker];
jjv360
  • 4,120
  • 3
  • 23
  • 37
  • UIDatePicker looks very different from what I need, if I can leave it with appearance similar this http://cdn5.staztic.com/cdn/screenshot/simplechronometer-2-0.jpg could be. – ademar111190 Nov 27 '12 at 17:29