简体   繁体   English

Swift-创建进度条

[英]Swift - Creating a progress bar

Hi I am using this code to try and animate a progress bar based on time. 嗨,我正在使用此代码尝试根据时间对进度条进行动画处理。

  import UIKit

class LoadingScreen: UIViewController {

    @IBOutlet var progressView: UIProgressView!

    override func viewDidLoad() {
        super.viewDidLoad()



        var time = 0.0
        var timer: NSTimer

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

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

However I get an error which says: cannot sign a value of type double to a value of type float. 但是我收到一条错误消息:无法将double类型的值签名为float类型的值。

EDIT: 编辑:

The error is on this line: 错误在此行上:

progressView.progress = time / 3 

Unless told otherwise swift compiler assumes type inference of Double for 0.0 - declare as 除非另有说明,否则Swift编译器会假设Double的类型推断为0.0-声明为

var time : Float = 0.0

Reference - https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html - Swift always chooses Double (rather than Float) when inferring the type of floating-point numbers. 参考-https: //developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html- 推断浮点数类型时,Swift总是选择Double(而不是Float)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM