From 8cd9061ba365a505bc763ebd43303608c691463d Mon Sep 17 00:00:00 2001 From: InKwon James Kim Date: Thu, 4 Jul 2019 17:16:40 +0900 Subject: [PATCH] add progressView --- .../Extension/UIViewControllerExtension.swift | 37 +++++++++++++++---- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/GMERemittance/Extension/UIViewControllerExtension.swift b/GMERemittance/Extension/UIViewControllerExtension.swift index 05462f97..c2f49994 100644 --- a/GMERemittance/Extension/UIViewControllerExtension.swift +++ b/GMERemittance/Extension/UIViewControllerExtension.swift @@ -276,32 +276,53 @@ extension UIViewController { } } +private var associatedObjectHandle: UInt8 = 0 extension UIViewController { - private static var progressView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 2)) + private var progressView: UIView? { + get { + guard let view = objc_getAssociatedObject(self, &associatedObjectHandle) as? UIView else { + return nil + } + + return view + } + set { + objc_setAssociatedObject(self, &associatedObjectHandle, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC) + } + } func setPercent(with percent: CGFloat) { - UIViewController.progressView.removeFromSuperview() - view.addSubview(UIViewController.progressView) + let progressSubView: UIView + + if let view = progressView { + progressSubView = view + } else { + progressSubView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 2)) + progressView = progressSubView + } + + progressSubView.removeFromSuperview() + view.addSubview(progressSubView) print("progress: \(percent)") - UIViewController.progressView.backgroundColor = AppConstants.themeRedColor + progressSubView.backgroundColor = AppConstants.themeRedColor let width = view.frame.width * percent UIView.animate( withDuration: 0.5, animations: { - UIViewController.progressView.frame = CGRect(x: 0, y: 0, width: width, height: 2) + progressSubView.frame = CGRect(x: 0, y: 0, width: width, height: 2) }, completion: { _ in if width == self.view.frame.width { UIView.animate( withDuration: 0.2, animations: { - UIViewController.progressView.alpha = 0.0 + progressSubView.alpha = 0.0 }, completion: { _ in - UIViewController.progressView.frame = CGRect(x: 0, y: 0, width: 0, height: 2) - UIViewController.progressView.alpha = 1.0 + progressSubView.frame = CGRect(x: 0, y: 0, width: 0, height: 2) + progressSubView.alpha = 1.0 }) } }