【问题标题】:Create a UIProgressView to go from 0.0 (empty) to 1.0 (full) in 3 seconds创建一个 UIProgressView 在 3 秒内从 0.0(空)变为 1.0(满)
【发布时间】:2015-07-15 12:48:27
【问题描述】:

基本上我想创建一个 UIProgressView 在 3 秒内从 0.0(空)变为 1.0(满)。谁能指出我在 UIProgressView 中快速使用 NSTimer 的正确方向?

【问题讨论】:

    标签: ios swift uiprogressview uiprogressbar


    【解决方案1】:

    如果有人对这里感兴趣的是 Swift 5 工作版本:

    extension UIProgressView {
        @available(iOS 10.0, *)
        func setAnimatedProgress(progress: Float = 1, duration: Float = 1, completion: (() -> ())? = nil) {
            Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { (timer) in
                DispatchQueue.main.async {
                    let current = self.progress
                    self.setProgress(current+(1/duration), animated: true)
                }
                if self.progress >= progress {
                    timer.invalidate()
                    if completion != nil {
                        completion!()
                    }
                }
            }
        }
    }
    

    用法:

    // Will fill the progress bar in 70 seconds
    self.progressBar.setAnimatedProgress(duration: 70) {
        print("Done!")
    }
    

    【讨论】:

      【解决方案2】:

      在搜索了一个并遇到了这个问题后,我创建了自己的解决方案。

      extension UIProgressView {
      
          func setAnimatedProgress(progress: Float,
              duration: NSTimeInterval = 1,
              delay: NSTimeInterval = 0,
              completion: () -> ()
          ) {
              dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) {
                  sleep(UInt32(delay))
                  dispatch_async(dispatch_get_main_queue()) {
                      self.layer.speed = Float(pow(duration, -1))
                      self.setProgress(progress, animated: true)
                  }
                  sleep(UInt32(duration))
                  dispatch_async(dispatch_get_main_queue()) {
                      self.layer.speed = 1
                      completion()
                  }
              }
          }
      }
      

      【讨论】:

        【解决方案3】:

        声明属性:

        var time = 0.0
        var timer: NSTimer
        

        初始化定时器:

        timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector:Selector("setProgress"), userInfo: nil, repeats: true)
        

        实现setProgress函数:

        func setProgress() {
            time += 0.1
            dispatch_async(dispatch_get_main_queue(), {
                progressView.progress = time / 3 
            })
            if time >= 3 {
                timer.invalidate()
            }
        }
        

        (我不能 100% 确定是否需要调度块,以确保 UI 在主线程中更新。如果没有必要,请随意删除它。)

        【讨论】:

        • 我只是试了一下,遇到了各种各样的问题:P 我为 UIProgressView 类型的进度条快速设置了一个新的可可触摸文件。我被告知该类没有初始化程序。
        • 我假设已经定义了进度视图(在故事板或代码中)。你知道怎么做,不是吗?
        • 定义是什么意思。我是编码新手,可以构建应用程序,但还不知道正确的术语:P 会有一个出口来定义它吗?
        • 是的,我就是这样做的:在情节提要中添加进度视图,这样您就可以看到它出现在 i.r.t. 的位置。其他元素,然后为它创建一个出口。
        • 我试过了,但它不会让我...我正在使用拆分视图在另一侧显示我的代码,但它不会让我选择将其作为出口。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-08-04
        • 2019-09-09
        • 2021-05-31
        • 1970-01-01
        • 1970-01-01
        • 2011-05-25
        • 2013-10-31
        相关资源
        最近更新 更多