我以编程方式创建了UIProgressView
和UILabel
。我希望我的进度条每秒更新一次,并在屏幕上显示进度,并更新标签值。所有这些都应该在viewDidLoad()
的召唤下完成。
取一个类型 Timer?
的变量,然后只需从 viewDidLoad
方法启动计时器,该方法将每隔一秒接收一次调用。然后只需更新该方法中的progressBar.progress
值和label.text
值,如下所述:
-
取一个变量
var timer:Timer?
-
在您的
viewDidLoad
使用timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector:#selector(self.updateProgressAndLabelValue), userInfo: nil, repeats: true)
做 一个函数,做需要的事情
func updateProgressAndLabelValue(){ //You will get call every second here //You can update the progressBar and Label here only //For example: yourProgressViewOutlet.progress = updatedValue //And lblYOuLabel.text = "(updatedValue)" }