From 6596e6c971e46ff232c54daa84e5226c779a8665 Mon Sep 17 00:00:00 2001 From: xaoxuu Date: Thu, 8 Aug 2019 19:29:03 +0800 Subject: [PATCH] update --- ProHUD/Alert/AlertConfig.swift | 54 ++++++++++++++++++++---------- ProHUD/Alert/AlertController.swift | 17 +++++++++- ProHUD/HUDController.swift | 2 +- 3 files changed, 53 insertions(+), 20 deletions(-) diff --git a/ProHUD/Alert/AlertConfig.swift b/ProHUD/Alert/AlertConfig.swift index 4169329..c166c4d 100644 --- a/ProHUD/Alert/AlertConfig.swift +++ b/ProHUD/Alert/AlertConfig.swift @@ -90,6 +90,35 @@ internal extension ProHUD.Configuration.Alert { var loadForceQuitButton: (ProHUD.Alert) -> Void { return privLoadForceQuitButton } + + var setupDefaultDuration: (ProHUD.Alert) -> Void { + return { (vc) in + // 如果设置了超时时间,但是增加了按钮 + if let t = vc.model.duration, t > 0 { + if vc.buttonEvents.count > 0 { + vc.duration(nil) + } + } else if vc.model.duration == nil && vc.model.scene != .loading { + // 如果没有设置超时时间 + vc.duration(2) + } + } + } + + var reloadStack: (ProHUD.Alert) -> Void { + return { (vc) in + if vc.textStack.arrangedSubviews.count > 0 { + vc.contentStack.addArrangedSubview(vc.textStack) + } else { + vc.textStack.removeFromSuperview() + } + if vc.actionStack.arrangedSubviews.count > 0 { + vc.contentStack.addArrangedSubview(vc.actionStack) + } else { + vc.actionStack.removeFromSuperview() + } + } + } } fileprivate var privLoadSubviews: (ProHUD.Alert) -> Void = { @@ -167,15 +196,8 @@ fileprivate var privReloadData: (ProHUD.Alert) -> Void = { } vc.imageView = icon } - if vc.model.scene == .loading { - let ani = CABasicAnimation(keyPath: "transform.rotation.z") - ani.toValue = M_PI*2.0 - ani.duration = 2 - ani.repeatCount = 10000 - vc.imageView?.layer.add(ani, forKey: "rotationAnimation") - } else { - vc.imageView?.layer.removeAllAnimations() - } + vc.imageView?.layer.removeAllAnimations() + // text if vc.model.title?.count ?? 0 > 0 || vc.model.message?.count ?? 0 > 0 { vc.contentStack.addArrangedSubview(vc.textStack) @@ -278,15 +300,8 @@ fileprivate var privReloadData: (ProHUD.Alert) -> Void = { vc.view.layoutIfNeeded() } } - switch vc.model.scene { - case .loading: - vc.duration(nil) - default: - vc.duration(2) - } - if vc.actionStack.arrangedSubviews.count > 0 { - vc.duration(nil) - } + + } }() @@ -326,3 +341,6 @@ fileprivate var privLoadForceQuitButton: (ProHUD.Alert) -> Void = { } } }() + + + diff --git a/ProHUD/Alert/AlertController.swift b/ProHUD/Alert/AlertController.swift index 2c089b3..41031ad 100644 --- a/ProHUD/Alert/AlertController.swift +++ b/ProHUD/Alert/AlertController.swift @@ -189,7 +189,21 @@ public extension Alert { @discardableResult func update(icon: UIImage?) -> Alert { model.icon = icon cfg.alert.reloadData(self) - imageView?.layer.removeAllAnimations() + return self + } + + @discardableResult func animate(rotate: Bool) -> Alert { + if rotate { + DispatchQueue.main.async { + let ani = CABasicAnimation(keyPath: "transform.rotation.z") + ani.toValue = M_PI*2.0 + ani.duration = 2 + ani.repeatCount = 10000 + self.imageView?.layer.add(ani, forKey: "rotationAnimation") + } + } else { + imageView?.layer.removeAllAnimations() + } return self } @@ -297,6 +311,7 @@ fileprivate extension Alert { // 布局 cfg.alert.loadSubviews(a) cfg.alert.reloadData(a) + cfg.alert.setupDefaultDuration(a) // 强制退出按钮 a.model.setupForceQuit(duration: cfg.alert.forceQuitTimer) { [weak self] in if let aa = self, aa.actionStack.superview == nil { diff --git a/ProHUD/HUDController.swift b/ProHUD/HUDController.swift index 0c4205f..bf6e53f 100644 --- a/ProHUD/HUDController.swift +++ b/ProHUD/HUDController.swift @@ -17,7 +17,7 @@ public class HUDController: UIViewController { internal var disappearCallback: (() -> Void)? /// 按钮事件 - fileprivate var buttonEvents = [UIButton:() -> Void]() + internal var buttonEvents = [UIButton:() -> Void]() init() { super.init(nibName: nil, bundle: nil)