본문 바로가기

iOS

[swift3] Create Simple Progress Dialog View (ProgressBar, ProgressIndicator, LoadingProgress)

- declare


import UIKit
class ProgressDialogView: UIView {
let activityIndictor: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.white)
init() {
super.init(frame: CGRect(x: 0,
y: 0,
width: 0,
height: 0))
self.setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setup()
}
func setup() {
self.addSubview(activityIndictor)
activityIndictor.startAnimating()
}
override func didMoveToSuperview() {
super.didMoveToSuperview()
if let superview = self.superview {
let width = superview.frame.size.width
let height = superview.frame.size.height
self.frame = CGRect(x: 0,
y: 0,
width: width,
height: height)
self.backgroundColor = UIColor.init(white: 0.0, alpha: 0.5)
let activityIndicatorSize: CGFloat = 40
activityIndictor.frame = CGRect(x: width / 2 - activityIndicatorSize / 2,
y: height / 2 - activityIndicatorSize / 2,
width: activityIndicatorSize,
height: activityIndicatorSize)
layer.masksToBounds = true
}
}
func show() {
self.isHidden = false
}
func hide() {
self.isHidden = true
}
}


- usage


import UIKit
class UsageUIViewController: UIViewController {
let progressDialog:ProgressDialogView = ProgressDialogView()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(progressDialog)
}
override func viewWillAppear(_ animated: Bool) {
showProgressDialog()
}
override func viewWillDisappear(_ animated: Bool) {
hideProgressDialog()
}
func showProgressDialog() {
self.progressDialog.show()
}
func hideProgressDialog() {
self.progressDialog.hide()
}
}


- preview