diff --git a/Example/Example/ViewController.swift b/Example/Example/ViewController.swift index ea9a03b..e81bcd5 100644 --- a/Example/Example/ViewController.swift +++ b/Example/Example/ViewController.swift @@ -27,6 +27,28 @@ class ViewController: UIViewController { } } + + + @IBAction func test(_ sender: UIButton) { + + +// testUpdateAction() + testGuard() + + } + + func testDelete() { + let a = ProHUD.push(alert: .delete, title: "确认删除", message: "此操作不可撤销") + a.add(action: .destructive, title: "确认", action: { [weak a] in + a?.remove(action: 0, 1) + a?.update(scene: .loading, title: "正在删除", message: "请稍后片刻") + DispatchQueue.main.asyncAfter(deadline: .now()+1) { + a?.update(scene: .success, title: "删除成功", message: "啊哈哈哈哈").duration(2) + ProHUD.push(toast: .success, title: "删除成功", message: "aaa") + } + }).add(action: .cancel, title: "取消", action: nil) + + } func testToast() { let t = ProHUD.Toast(scene: .loading, title: "正在加载", message: "请稍候片刻") @@ -39,60 +61,42 @@ class ViewController: UIViewController { let a2 = ProHUD.push(alert: .loading, title: "正在加载", message: "马上就要成功了") DispatchQueue.main.asyncAfter(deadline: .now()+1) { let a3 = ProHUD.push(alert: .error, title: "加载失败", message: "点击充实") - a3.addAction(style: .default, title: "重新加载") { [weak a3] in - a3?.updateContent(scene: .success, title: "加载成功", message: "马上就要成功了") - a3?.updateAction(index: 0, style: .default, title: "OK", action: { [weak a2, a3] in - a2?.pop() + a3.add(action: .default, title: "重新加载") { [weak a3] in + a3?.update(scene: .success, title: "加载成功", message: "马上就要成功了") + a3?.update(action: 0, style: .default, title: "OK", action: { [weak a3] in a3?.pop() - }).removeAction(index: 1).removeAction(index: 1) - }.addAction(style: .destructive, title: "终止", action: nil).addAction(style: .cancel, title: "取消", action: nil) + }).remove(action: 1, 2) + }.add(action: .destructive, title: "终止", action: nil).add(action: .cancel, title: "取消", action: nil) } } - } - - func testDelete() { - let a = ProHUD.push(alert: .delete, title: "确认删除", message: "此操作不可撤销") - a.addAction(style: .destructive, title: "确认", action: { [weak a] in - a?.removeAction(index: 0).removeAction(index: 0) - a?.updateContent(scene: .loading, title: "正在删除", message: "请稍后片刻") - DispatchQueue.main.asyncAfter(deadline: .now()+1) { - a?.updateContent(scene: .success, title: "删除成功", message: "啊哈哈哈哈").duration(2) - ProHUD.push(toast: .success, title: "删除成功", message: "aaa") - } - }).addAction(style: .cancel, title: "取消", action: nil) + + } - - @IBAction func test(_ sender: UIButton) { - - - textUpdateAction() - - } - func testGuard() { let g = ProHUD.Guard(title: "请求权限", message: "请打开相机权限开关,否则无法进行测量。") - g.loadTitle("呵呵") - g.loadBody("请打开相机权限开关,否则无法进行测量。请打开相机权限开关,否则无法进行测量。") - g.loadButton(style: .default, title: "测试弹窗", action: { [weak self] in + g.add(title: "呵呵") + g.add(message: "请打开相机权限开关,否则无法进行测量。请打开相机权限开关,否则无法进行测量。") + g.add(action: .default, title: "测试弹窗", action: { [weak self] in self?.testToast() }) - g.loadButton(style: .destructive, title: "测试删除弹窗", action: { [weak self] in + g.add(action: .destructive, title: "测试删除弹窗", action: { [weak self] in self?.testDelete() }) - g.loadButton(style: .cancel, title: "我知道了") + g.add(action: .cancel, title: "我知道了") + g.push(to: self) debugPrint("test: ", g) } - func textUpdateAction() { + func testUpdateAction() { let a = ProHUD.push(alert: .confirm, title: "确认删除", message: "此操作无法撤销") - a.addAction(style: .destructive, title: "删除") { - a.removeAction(index: 0, 1).updateContent(scene: .loading, title: "正在删除", message: "请稍后片刻") - }.addAction(style: .cancel, title: "取消", action: nil) + a.add(action: .destructive, title: "删除") { + a.remove(action: 0, 1).update(scene: .loading, title: "正在删除", message: "请稍后片刻") + }.add(action: .cancel, title: "取消", action: nil) diff --git a/ProHUD/Alert/AlertController.swift b/ProHUD/Alert/AlertController.swift index 7993b96..13aa7c6 100644 --- a/ProHUD/Alert/AlertController.swift +++ b/ProHUD/Alert/AlertController.swift @@ -75,11 +75,6 @@ public extension ProHUD { } - public override func viewDidDisappear(_ animated: Bool) { - super.viewDidDisappear(animated) - disappearCallback?() - } - } @@ -121,19 +116,11 @@ public extension ProHUD.Alert { // MARK: 设置函数 - /// 设置超时时间 - /// - Parameter duration: 超时时间 - @discardableResult func duration(_ duration: TimeInterval?) -> ProHUD.Alert { - model.duration = duration - willLayoutSubviews() - return self - } - /// 添加按钮 /// - Parameter style: 样式 /// - Parameter text: 标题 /// - Parameter action: 事件 - @discardableResult func addAction(style: UIAlertAction.Style, title: String?, action: (() -> Void)?) -> ProHUD.Alert { + @discardableResult func add(action style: UIAlertAction.Style, title: String?, action: (() -> Void)?) -> ProHUD.Alert { if let btn = privAddButton(custom: Button.actionButton(title: title), action: action) as? Button { btn.update(style: style) } @@ -143,7 +130,7 @@ public extension ProHUD.Alert { /// 添加按钮 /// - Parameter button: 按钮 /// - Parameter action: 事件 - @discardableResult func addAction(custom button: UIButton, action: (() -> Void)?) -> ProHUD.Alert { + @discardableResult func add(button: UIButton, action: (() -> Void)?) -> ProHUD.Alert { privAddButton(custom: button, action: action) return self } @@ -162,12 +149,29 @@ public extension ProHUD.Alert { return self } - /// 更新标题 + /// 设置持续时间 + /// - Parameter duration: 持续时间 + @discardableResult func duration(_ duration: TimeInterval?) -> ProHUD.Alert { + model.duration = duration + willLayoutSubviews() + return self + } + + /// 更新 + /// - Parameter scene: 场景 /// - Parameter title: 标题 - @discardableResult func updateContent(scene: Scene, title: String? = nil, message: String? = nil, icon: UIImage? = nil) -> ProHUD.Alert { + /// - Parameter message: 正文 + @discardableResult func update(scene: Scene, title: String?, message: String?) -> ProHUD.Alert { + model.scene = scene model.title = title model.message = message - model.scene = scene + cfg.alert.reloadData(self) + return self + } + + /// 更新图标 + /// - Parameter icon: 图标 + @discardableResult func update(icon: UIImage?) -> ProHUD.Alert { model.icon = icon cfg.alert.reloadData(self) return self @@ -178,8 +182,8 @@ public extension ProHUD.Alert { /// - Parameter style: 样式 /// - Parameter title: 标题 /// - Parameter action: 事件 - @discardableResult func updateAction(index: Int, style: UIAlertAction.Style, title: String?, action: (() -> Void)?) -> ProHUD.Alert { - return updateAction(index: index, button: { (btn) in + @discardableResult func update(action index: Int, style: UIAlertAction.Style, title: String?, action: (() -> Void)?) -> ProHUD.Alert { + return update(action: index, button: { (btn) in btn.setTitle(title, for: .normal) if let b = btn as? Button { b.update(style: style) @@ -192,7 +196,7 @@ public extension ProHUD.Alert { /// - Parameter index: 索引 /// - Parameter button: 按钮 /// - Parameter action: 事件 - @discardableResult func updateAction(index: Int, button: (UIButton) -> Void, action: (() -> Void)? = nil) -> ProHUD.Alert { + @discardableResult func update(action index: Int, button: (UIButton) -> Void, action: (() -> Void)? = nil) -> ProHUD.Alert { if index < self.actionStack.arrangedSubviews.count, let btn = self.actionStack.arrangedSubviews[index] as? UIButton { button(btn) if let ac = action { @@ -207,7 +211,7 @@ public extension ProHUD.Alert { /// 移除按钮 /// - Parameter index: 索引 - @discardableResult func removeAction(index: Int...) -> ProHUD.Alert { + @discardableResult func remove(action index: Int...) -> ProHUD.Alert { for (i, idx) in index.enumerated() { privRemoveAction(index: idx-i) } @@ -250,8 +254,8 @@ public extension ProHUD { /// - Parameter title: 标题 /// - Parameter message: 正文 /// - Parameter icon: 图标 - @discardableResult func push(alert scene: Alert.Scene, title: String? = nil, message: String? = nil, icon: UIImage? = nil) -> Alert { - return push(Alert(scene: scene, title: title, message: message, icon: icon)) + @discardableResult func push(alert scene: Alert.Scene, title: String? = nil, message: String? = nil) -> Alert { + return push(Alert(scene: scene, title: title, message: message)) } /// 获取指定的实例 @@ -285,6 +289,7 @@ public extension ProHUD { } } } + } @@ -303,8 +308,8 @@ public extension ProHUD { /// - Parameter title: 标题 /// - Parameter message: 正文 /// - Parameter icon: 图标 - @discardableResult class func push(alert: Alert.Scene, title: String? = nil, message: String? = nil, icon: UIImage? = nil) -> Alert { - return shared.push(alert: alert, title: title, message: message, icon: icon) + @discardableResult class func push(alert: Alert.Scene, title: String? = nil, message: String? = nil) -> Alert { + return shared.push(alert: alert, title: title, message: message) } /// 获取指定的实例 @@ -354,35 +359,23 @@ fileprivate extension ProHUD.Alert { } func willLayoutSubviews() { - willLayout?.cancel() - model.durationBlock?.cancel() - model.forceQuitTimerBlock?.cancel() - willLayout = DispatchWorkItem(block: { [weak self] in + model.setupWillLayout(duration: 0.001) { [weak self] in if let a = self { // 布局 cfg.alert.loadSubviews(a) cfg.alert.reloadData(a) - // 超时 - if let t = a.model.duration, t > 0 { - a.model.durationBlock = DispatchWorkItem(block: { [weak self] in - self?.pop() - }) - DispatchQueue.main.asyncAfter(deadline: .now()+t, execute: a.model.durationBlock!) - } else { - a.model.durationBlock = nil + // 持续时间 + a.model.setupDuration(duration: a.model.duration) { [weak self] in + self?.pop() } // 强制退出按钮 - if cfg.alert.forceQuitTimer > 0 && self?.actionStack.superview == nil { - a.model.forceQuitTimerBlock = DispatchWorkItem(block: { [weak self] in - if let s = self { - cfg.alert.loadForceQuitButton(s) - } - }) - DispatchQueue.main.asyncAfter(deadline: .now()+cfg.alert.forceQuitTimer, execute: a.model.forceQuitTimerBlock!) + a.model.setupForceQuit(duration: cfg.alert.forceQuitTimer) { [weak self] in + if let aa = self, aa.actionStack.superview == nil { + cfg.alert.loadForceQuitButton(aa) + } } } - }) - DispatchQueue.main.asyncAfter(deadline: .now()+0.001, execute: willLayout!) + } } @discardableResult func privAddButton(custom button: UIButton, action: (() -> Void)?) -> UIButton { diff --git a/ProHUD/Alert/AlertModel.swift b/ProHUD/Alert/AlertModel.swift index dc321f2..f1dfc79 100644 --- a/ProHUD/Alert/AlertModel.swift +++ b/ProHUD/Alert/AlertModel.swift @@ -78,12 +78,38 @@ public extension ProHUD.Alert { /// 强制退出代码 internal var forceQuitCallback: (() -> Void)? - public init(title: String? = nil, message: String? = nil, icon: UIImage? = nil) { - self.title = title - self.message = message - self.icon = icon + internal var willLayoutBlock: DispatchWorkItem? + + internal mutating func setupDuration(duration: TimeInterval?, callback: @escaping () -> Void) { + self.duration = duration + durationBlock?.cancel() + if let t = duration, t > 0 { + durationBlock = DispatchWorkItem(block: callback) + DispatchQueue.main.asyncAfter(deadline: .now()+t, execute: durationBlock!) + } else { + durationBlock = nil + } } + internal mutating func setupForceQuit(duration: TimeInterval?, callback: @escaping () -> Void) { + forceQuitTimerBlock?.cancel() + if let t = duration, t > 0 { + forceQuitTimerBlock = DispatchWorkItem(block: callback) + DispatchQueue.main.asyncAfter(deadline: .now()+t, execute: forceQuitTimerBlock!) + } else { + forceQuitTimerBlock = nil + } + } + + internal mutating func setupWillLayout(duration: TimeInterval?, callback: @escaping () -> Void) { + willLayoutBlock?.cancel() + if let t = duration, t > 0 { + willLayoutBlock = DispatchWorkItem(block: callback) + DispatchQueue.main.asyncAfter(deadline: .now()+t, execute: willLayoutBlock!) + } else { + willLayoutBlock = nil + } + } } diff --git a/ProHUD/Guard/GuardController.swift b/ProHUD/Guard/GuardController.swift index 3b91a3c..1d1b40a 100644 --- a/ProHUD/Guard/GuardController.swift +++ b/ProHUD/Guard/GuardController.swift @@ -58,10 +58,10 @@ public extension ProHUD { view.tintColor = cfg.guard.tintColor if let _ = title { - loadTitle(title) + add(title: title) } if let _ = message { - loadBody(message) + add(message: message) } cfg.guard.loadSubviews(self) cfg.guard.reloadData(self) @@ -74,16 +74,6 @@ public extension ProHUD { } - public override func viewDidDisappear(_ animated: Bool) { - super.viewDidDisappear(animated) - - } - - deinit { - debug(self, "deinit") - } - - } } @@ -134,13 +124,13 @@ public extension ProHUD.Guard { /// 加载一个标题 /// - Parameter text: 文本 - @discardableResult func loadTitle(_ text: String?) -> UILabel { + @discardableResult func add(title: String?) -> UILabel { let lb = UILabel() lb.font = cfg.guard.titleFont lb.textColor = cfg.primaryLabelColor lb.numberOfLines = 0 lb.textAlignment = .justified - lb.text = text + lb.text = title textStack.addArrangedSubview(lb) if #available(iOS 11.0, *) { let count = textStack.arrangedSubviews.count @@ -155,13 +145,13 @@ public extension ProHUD.Guard { /// 加载一段正文 /// - Parameter text: 文本 - @discardableResult func loadBody(_ text: String?) -> UILabel { + @discardableResult func add(message: String?) -> UILabel { let lb = UILabel() lb.font = cfg.guard.bodyFont lb.textColor = cfg.secondaryLabelColor lb.numberOfLines = 0 lb.textAlignment = .justified - lb.text = text + lb.text = message textStack.addArrangedSubview(lb) return lb } @@ -170,7 +160,7 @@ public extension ProHUD.Guard { /// - Parameter style: 样式 /// - Parameter title: 标题 /// - Parameter action: 事件 - @discardableResult func loadButton(style: UIAlertAction.Style, title: String?, action: (() -> Void)? = nil) -> UIButton { + @discardableResult func add(action style: UIAlertAction.Style, title: String?, action: (() -> Void)? = nil) -> UIButton { let btn = Button.actionButton(title: title) btn.titleLabel?.font = cfg.guard.buttonFont if actionStack.superview == nil { @@ -188,6 +178,13 @@ public extension ProHUD.Guard { } + /// 消失事件 + /// - Parameter callback: 事件回调 + @discardableResult func didDisappear(_ callback: (() -> Void)?) -> ProHUD.Guard { + disappearCallback = callback + return self + } + } fileprivate extension ProHUD.Guard { diff --git a/ProHUD/HUDController.swift b/ProHUD/HUDController.swift index a3c9650..cb858b1 100644 --- a/ProHUD/HUDController.swift +++ b/ProHUD/HUDController.swift @@ -9,13 +9,14 @@ import UIKit public class HUDController: UIViewController { - - public var identifier = String(Date().timeIntervalSince1970) - internal var willLayout: DispatchWorkItem? + /// ID标识 + public var identifier = String(Date().timeIntervalSince1970) /// 消失回调 internal var disappearCallback: (() -> Void)? + + /// 按钮事件 internal var buttonEvents = [UIButton:() -> Void]() init() { @@ -38,6 +39,10 @@ public class HUDController: UIViewController { // Do any additional setup after loading the view. } + public override func viewDidDisappear(_ animated: Bool) { + super.viewDidDisappear(animated) + disappearCallback?() + } } diff --git a/ProHUD/Toast/ToastController.swift b/ProHUD/Toast/ToastController.swift index b23e0a4..3e506ad 100644 --- a/ProHUD/Toast/ToastController.swift +++ b/ProHUD/Toast/ToastController.swift @@ -91,10 +91,6 @@ public extension ProHUD { } - public override func viewDidDisappear(_ animated: Bool) { - super.viewDidDisappear(animated) - disappearCallback?() - } } @@ -129,16 +125,8 @@ public extension ProHUD.Toast { /// 设置持续时间 /// - Parameter duration: 持续时间 @discardableResult func duration(_ duration: TimeInterval?) -> ProHUD.Toast { - model.duration = duration - // 持续时间 - model.durationBlock?.cancel() - if let t = duration, t > 0 { - model.durationBlock = DispatchWorkItem(block: { [weak self] in - self?.pop() - }) - DispatchQueue.main.asyncAfter(deadline: .now()+t, execute: model.durationBlock!) - } else { - model.durationBlock = nil + model.setupDuration(duration: duration) { [weak self] in + self?.pop() } return self } @@ -161,12 +149,10 @@ public extension ProHUD.Toast { /// - Parameter scene: 场景 /// - Parameter title: 标题 /// - Parameter message: 内容 - /// - Parameter icon: 图标 - @discardableResult func update(scene: Scene, title: String? = nil, message: String? = nil, icon: UIImage? = nil) -> ProHUD.Toast { + @discardableResult func update(scene: Scene, title: String?, message: String?) -> ProHUD.Toast { model.scene = scene model.title = title model.message = message - model.icon = icon cfg.toast.reloadData(self) return self } @@ -175,7 +161,7 @@ public extension ProHUD.Toast { /// - Parameter title: 标题 @discardableResult func update(title: String?) -> ProHUD.Toast { model.title = title - titleLabel.text = title + cfg.toast.reloadData(self) return self } @@ -183,7 +169,7 @@ public extension ProHUD.Toast { /// - Parameter message: 消息 @discardableResult func update(message: String?) -> ProHUD.Toast { model.message = message - bodyLabel.text = message + cfg.toast.reloadData(self) return self } @@ -191,7 +177,7 @@ public extension ProHUD.Toast { /// - Parameter icon: 图标 @discardableResult func update(icon: UIImage?) -> ProHUD.Toast { model.icon = icon - imageView.image = icon + cfg.toast.reloadData(self) return self } @@ -292,8 +278,8 @@ public extension ProHUD { /// - Parameter title: 标题 /// - Parameter message: 内容 /// - Parameter icon: 图标 - @discardableResult func push(toast scene: Toast.Scene, title: String? = nil, message: String? = nil, icon: UIImage? = nil) -> Toast { - return push(Toast(scene: scene, title: title, message: message, icon: icon)) + @discardableResult func push(toast scene: Toast.Scene, title: String? = nil, message: String? = nil) -> Toast { + return push(Toast(scene: scene, title: title, message: message)) } /// 获取指定的toast @@ -346,8 +332,8 @@ public extension ProHUD { /// - Parameter title: 标题 /// - Parameter message: 内容 /// - Parameter icon: 图标 - @discardableResult class func push(toast: Toast.Scene, title: String? = nil, message: String? = nil, icon: UIImage? = nil) -> Toast { - return shared.push(toast: toast, title: title, message: message, icon: icon) + @discardableResult class func push(toast: Toast.Scene, title: String? = nil, message: String? = nil) -> Toast { + return shared.push(toast: toast, title: title, message: message) } /// 获取指定的toast diff --git a/ProHUD/Toast/ToastModel.swift b/ProHUD/Toast/ToastModel.swift index 62498f1..b9d1353 100644 --- a/ProHUD/Toast/ToastModel.swift +++ b/ProHUD/Toast/ToastModel.swift @@ -57,10 +57,15 @@ public extension ProHUD.Toast { /// 点击事件回调 internal var tapCallback: (() -> Void)? - public init(title: String? = nil, message: String? = nil, icon: UIImage? = nil) { - self.title = title - self.message = message - self.icon = icon + internal mutating func setupDuration(duration: TimeInterval?, callback: @escaping () -> Void) { + self.duration = duration + durationBlock?.cancel() + if let t = duration, t > 0 { + durationBlock = DispatchWorkItem(block: callback) + DispatchQueue.main.asyncAfter(deadline: .now()+t, execute: durationBlock!) + } else { + durationBlock = nil + } }