Swift - USTrogress的UIProgressView不顺畅

dwi*_*own 8 uiprogressview ios swift

所以我使用NSTimer让用户知道应用程序正在运行.进度条设置为持续3秒,但在运行时,它以"滴答"运动显示,并且它不应该像应该的那样平滑.无论如何我可以让它更顺利 - 我确信我的计算错误......

如果有人可以看看那将是伟大的.这是代码:

import UIKit

class LoadingScreen: UIViewController {


    var time : Float = 0.0
    var timer: NSTimer?

    @IBOutlet weak var progressView: UIProgressView!


    override func viewDidLoad() {
        super.viewDidLoad()

// Do stuff

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

}//close viewDidLoad

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

}
Run Code Online (Sandbox Code Playgroud)

mbo*_*one 20

编辑:一个简单的3秒UIView动画(推荐)

如果您的栏只是平滑移动以指示活动,可以考虑使用UIActivityIndicatorView或自定义UIView动画:

override func viewDidAppear(animated: Bool)
{
    super.viewDidAppear(animated)

    UIView.animateWithDuration(3, animations: { () -> Void in
        self.progressView.setProgress(1.0, animated: true)
    })
}
Run Code Online (Sandbox Code Playgroud)

确保您的progressView进度设置为零开始.这将导致进度的平滑3秒动画.

简单的动画进度(工作但仍然跳跃一点)

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIProgressView_Class/#//apple_ref/occ/instm/UIProgressView/setProgress:animated :

func setProgress() {
    time += 0.1
    progressView.setProgress(time / 3, animated: true)
    if time >= 3 {
        timer!.invalidate()
    }
}
Run Code Online (Sandbox Code Playgroud)

间隔较小的选项.(不建议)

将计时器设置为较小的间隔:

timer = NSTimer.scheduledTimerWithTimeInterval(0.001, target: self, selector:Selector("setProgress"), userInfo: nil, repeats: true)
Run Code Online (Sandbox Code Playgroud)

然后更新你的功能

func setProgress() {
    time += 0.001
    progressView.setProgress(time / 3, animated: true)
    if time >= 3 {
        timer!.invalidate()
    }
}
Run Code Online (Sandbox Code Playgroud)