How to create custom class Loading view using Swift
我正在通过自定义类加载视图实现加载概念。我需要通过使用简单的
下面的代码是我在
中使用
1 2 3 4 5 6 7 8 9 10 11 12 13 | func loadinHubShow() { let alert = UIAlertController(title: nil, message:"Please wait...", preferredStyle: .alert) let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50)) loadingIndicator.hidesWhenStopped = true loadingIndicator.style = UIActivityIndicatorView.Style.gray loadingIndicator.startAnimating(); alert.view.addSubview(loadingIndicator) present(alert, animated: true, completion: nil) } func loadinHubDismiss() { dismiss(animated: false, completion: nil) } |
如何将其更改为自定义类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | import Foundation import UIKit public class Indicator { public static let sharedInstance = Indicator() var blurImg = UIImageView() var indicator = UIActivityIndicatorView() private init() { blurImg.frame = UIScreen.main.bounds blurImg.backgroundColor = UIColor.black blurImg.isUserInteractionEnabled = true blurImg.alpha = 0.5 indicator.style = .whiteLarge indicator.center = blurImg.center indicator.startAnimating() indicator.color = .red } func showIndicator(){ DispatchQueue.main.async( execute: { UIApplication.shared.keyWindow?.addSubview(self.blurImg) UIApplication.shared.keyWindow?.addSubview(self.indicator) }) } func hideIndicator(){ DispatchQueue.main.async( execute: { self.blurImg.removeFromSuperview() self.indicator.removeFromSuperview() }) } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | import UIKit class Loader: UIView { var view: UIView! @IBOutlet fileprivate weak var blurView: UIVisualEffectView! @IBOutlet fileprivate weak var loader: UIActivityIndicatorView! var targetView: UIView? required init(forView view: UIView) { super.init(frame: view.bounds) targetView = view self._setup() targetView?.addSubview(self) } override init(frame: CGRect) { super.init(frame: frame) _setup() } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) self._setup() } private func _setup() { view = _loadViewFromNib() view.frame = bounds view.autoresizingMask = [.flexibleWidth, .flexibleHeight] view.translatesAutoresizingMaskIntoConstraints = true addSubview(view) } private func _loadViewFromNib() -> UIView { let bundle = Bundle(for: type(of: self)) let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle) let nibView = nib.instantiate(withOwner: self, options: nil).first as! UIView return nibView } func showLoading(withCompletion completion: (() -> Swift.Void)? = nil) { UIView.animate(withDuration: 0.5, animations: { }) { _ in completion?() } } func hideLoading() { UIView.animate(withDuration: 0.5, animations: { }) { _ in self.removeFromSuperview() } } } |
您可以在ViewController中调用以下函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | func showLoading() { if self.view.subviews.map({ $0 is Loader }).contains(true) { return } let loader = Loader(forView: self.view) loader.showLoading() } func hideLoading() { for view in self.view.subviews { if let view = view as? Loader { view.hideLoading() break } } } |
您可以根据需要自定义以下代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | func showActivityIndicatory(uiView: UIView) { var container: UIView = UIView() container.frame = uiView.frame container.center = uiView.center container.backgroundColor = UIColorFromHex(0xffffff, alpha: 0.3) var loadingView: UIView = UIView() loadingView.frame = CGRectMake(0, 0, 80, 80) loadingView.center = uiView.center loadingView.backgroundColor = UIColorFromHex(0x444444, alpha: 0.7) loadingView.clipsToBounds = true loadingView.layer.cornerRadius = 10 var actInd: UIActivityIndicatorView = UIActivityIndicatorView() actInd.frame = CGRectMake(0.0, 0.0, 40.0, 40.0); actInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge actInd.center = CGPointMake(loadingView.frame.size.width / 2, loadingView.frame.size.height / 2); loadingView.addSubview(actInd) container.addSubview(loadingView) uiView.addSubview(container) actInd.startAnimating() } |