今回の成果物
こちらに置いてあります
negibouze/SimpleLoadingSample
1. LoadingView.xibを作成する
1. 背景を設定する
alphaで透過させるとsubviewも一緒に透過されてしまうので、opacityで透過させる。
2. Activity Indicatorを配置する
2. LoadingView.swiftを作成する
LoadingView
class LoadingView: UIView {
@IBOutlet var contentView: UIView!
@IBOutlet weak var indicator: UIActivityIndicatorView!
3. .xibと.swiftを紐づける
1. File's OwnerにLoadingView.swiftを設定する
2. 各要素を紐づける
4. LoadingViewを実装する
LoadingView(抜粋)
class LoadingView: UIView {
private static let sharedInstance = LoadingView()
private var isShow = false
private override init(frame: CGRect) {
super.init(frame: (UIApplication.sharedApplication().delegate?.window??.bounds)!)
self.setup()
}
class func show(duration: Double = 0.5) {
if sharedInstance.isShow {
return
}
guard let win = UIApplication.sharedApplication().delegate?.window, let window = win else {
return
}
sharedInstance.isShow = true
sharedInstance.indicator.startAnimating()
window.addSubview(sharedInstance)
UIView.animateWithDuration(duration,
animations: {
sharedInstance.alpha = 1.0
},
completion: nil
)
}
class func hide(duration: Double = 0.5) {
if !sharedInstance.isShow {
return
}
UIView.animateWithDuration(duration,
animations: {
sharedInstance.alpha = 0
},
completion: { b in
sharedInstance.removeFromSuperview()
sharedInstance.indicator.stopAnimating()
sharedInstance.isShow = false
}
)
}