swift 4.2实现活动指示器和进度条
代码:
import UIKit
class ViewController: UIViewController {
var activityIndicatorView: UIActivityIndicatorView!
var progressView: UIProgressView!
var timer: Timer!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = UIColor.black
let screen = UIScreen.main.bounds
//1. 获得指示器
self.activityIndicatorView = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.whiteLarge)
var frame = self.activityIndicatorView.frame
frame.origin = CGPoint(x: (screen.size.width - frame.size.width)/2, y: 84)
//重新设置控件位置
self.activityIndicatorView.frame = frame
//false:当控件处于非活动状态时,控件不会隐藏
self.activityIndicatorView.hidesWhenStopped = false
self.view.addSubview(self.activityIndicatorView)
//2.Upload按钮
let buttonUpload = UIButton(type: UIButton.ButtonType.system)
buttonUpload.setTitle("Upload", for: UIControl.State())
let buttonUploadWidth: CGFloat = 50
let buttonUploadHeight: CGFloat = 30
let buttonUploadTopView: CGFloat = 190
buttonUpload.frame = CGRect(x: (screen.size.width - buttonUploadWidth)/2, y: buttonUploadTopView, width: buttonUploadWidth, height: buttonUploadHeight)
buttonUpload.addTarget(self,action: #selector(startToMove(sender:)),for: .touchUpInside)
self.view.addSubview(buttonUpload)
//3.进度条
let progressViewWidth:CGFloat = 200
let progressViewHeight:CGFloat = 2
let progressViewTopView: CGFloat = 283
self.progressView = UIProgressView(frame: CGRect(x: CGFloat(screen.size.width - progressViewWidth)/2, y: progressViewTopView, width: progressViewWidth, height: progressViewHeight))
self.view.addSubview(self.progressView)
//4.Download按钮
let buttonDownload = UIButton(type: UIButton.ButtonType.system)
buttonDownload.setTitle("Download", for: UIControl.State())
let buttonDownloadWidth: CGFloat = 69
let buttonDownloadHeight: CGFloat = 30
let buttonDownloadTopView: CGFloat = 384
buttonDownload.frame = CGRect(x: (screen.size.width - buttonDownloadWidth)/2, y: buttonDownloadTopView, width: buttonDownloadWidth, height: buttonDownloadHeight)
buttonDownload.addTarget(self,action: #selector(downloadProgress(_ :)),for: .touchUpInside)
self.view.addSubview(buttonDownload)
}
@objc func startToMove(sender: AnyObject) {
if(self.activityIndicatorView.isAnimating){
self.activityIndicatorView.stopAnimating()
} else {
self.activityIndicatorView.startAnimating()
}
}
@objc func downloadProgress(_ sender: AnyObject){
self.timer = Timer.scheduledTimer(timeInterval: 1.0 , target: self, selector: #selector(download), userInfo: nil, repeats: true)
}
@objc func download(){
self.progressView.progress = self.progressView.progress + 0.1
if (self.progressView.progress == 1.0){
self.timer.invalidate()
let alertcontroller: UIAlertController = UIAlertController(title: "download completed!", message: "", preferredStyle: UIAlertController.Style.alert)
let okAction = UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil)
alertcontroller.addAction(okAction)
//显示
self.present(alertcontroller,animated: true, completion: nil)
}
}
}
image.png