Swift - 进度条UIProgressView的用法

1,创建进度条

1
2
3
4
var progressView= UIProgressView (progressViewStyle: UIProgressViewStyle . Default )
progressView.center= self .view.center
progressView.progress=0.5 //默认进度50%
.view.addSubview(progressView);

2,设置进度,同时有动画效果
1
progressView.setProgress(0.8,animated: true )
3,改变进度条颜色
2
progressView.progressTintColor= UIColor .greenColor() //已有进度颜色
progressView.trackTintColor= .blueColor() //剩余进度颜色(即进度槽颜色)


步骤如下:

一、在函数外部定义三个变量

var timer: NSTimer!

var remainTime = 0

var progress: UIProgressView!

overridefuncviewDidLoad() {

//这里放置步骤二的代码即可

}

二、在函数中创建进度条控件

progress = UIProgressView(frame: CGRect(x: (width-100)/2,y: height/2,width: 100,height: 1))

progress.progress = 0

progress.progressTintColor = UIColor.redColor()

progress.trackTintColor = UIColor.blackColor()

self.view.addSubview(progress)

timer = NSTimer.scheduledTimerWithTimeInterval(1,target: self,selector: "timerAction",userInfo: nil,repeats:true)

timer.fire()

三、创建事件响应的函数

func timerAction() {

if(remainTime >= 100){

timer.invalidate()

var homeView = UIStoryboard(name: "Main",bundle:nil).instantiateViewControllerWithIdentifier("HomeViewController") as! HomeViewController

self.presentViewController(homeView,animated: true,completion: nil)

} else {

remainTime = remainTime + 35

let progressValue = Float(remainTime)/100

progress.setProgress(progressValue,animated:true)

}

运行后,就可以看到进度条的加载效果了。

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...