【问题标题】:iOS view similar to Chronometer on AndroidiOS 视图类似于 Android 上的 Chronometer
【发布时间】:2012-11-27 15:13:51
【问题描述】:

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

【问题讨论】:

    标签: android ios view chronometer


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

        猜你喜欢
        • 2012-06-08
        • 1970-01-01
        • 1970-01-01
        • 2014-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多