Swift - 进度条(UIProgressView)的用法

前端之家收集整理的这篇文章主要介绍了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)

}

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

原文链接:https://www.f2er.com/swift/324899.html

猜你在找的Swift相关文章