From 4b2d9dac3ced72fcd4c9aee97c3c151cfd84e64d Mon Sep 17 00:00:00 2001 From: xaoxuu Date: Sat, 10 Sep 2022 13:52:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PHDemo/PHDemo/AlertVC.swift | 10 +-- PHDemo/PHDemo/SheetVC.swift | 21 +++--- PHDemo/PHDemo/ToastVC.swift | 65 ++++++++++++++++++- Sources/ProHUD/Alert/AlertManager.swift | 23 +++---- Sources/ProHUD/Core/Utils/ConsolePrint.swift | 2 +- Sources/ProHUD/Sheet/Sheet.swift | 23 ------- Sources/ProHUD/Sheet/SheetManager.swift | 30 +++++++++ .../ProHUD/Toast/ToastConvenienceLayout.swift | 6 +- Sources/ProHUD/Toast/ToastManager.swift | 23 +++---- 9 files changed, 130 insertions(+), 73 deletions(-) diff --git a/PHDemo/PHDemo/AlertVC.swift b/PHDemo/PHDemo/AlertVC.swift index cf3c3b2..36efc2f 100644 --- a/PHDemo/PHDemo/AlertVC.swift +++ b/PHDemo/PHDemo/AlertVC.swift @@ -108,8 +108,8 @@ class AlertVC: ListVC { } } } - list.add(title: "控件动态管理") { section in - section.add(title: "按钮增删") { + list.add(title: "控件管理") { section in + section.add(title: "按钮增删改查") { Alert(.note) { alert in alert.vm.message = "可以动态增加、删除按钮" alert.add(action: "在底部增加按钮", style: .filled(color: .systemGreen)) { alert in @@ -172,7 +172,7 @@ class AlertVC: ListVC { } } } - list.add(title: "多实例管理") { section in + list.add(title: "实例管理") { section in section.add(title: "多层级弹窗") { func f(i: Int) { Alert { alert in @@ -186,7 +186,7 @@ class AlertVC: ListVC { } f(i: 1) } - section.add(title: "弹出loading,如果有就不重复弹") { + section.add(title: "弹出loading,如果已经存在就更新") { func f(i: Int) { Alert.lazyPush(identifier: "haha") { alert in if i < 2 { @@ -209,7 +209,7 @@ class AlertVC: ListVC { } } list.add(title: "放在特定页面") { [weak self] section in - section.add(title: "放在特定页面") { + section.add(title: "放在一个VC中") { let vc = UIViewController() vc.title = "页面" vc.view.backgroundColor = .systemYellow diff --git a/PHDemo/PHDemo/SheetVC.swift b/PHDemo/PHDemo/SheetVC.swift index 66a6b20..bc22e08 100644 --- a/PHDemo/PHDemo/SheetVC.swift +++ b/PHDemo/PHDemo/SheetVC.swift @@ -63,18 +63,10 @@ class SheetVC: ListVC { } } - section.add(title: "全屏") { - Sheet { sheet in - sheet.config.isFullScreen = true - sheet.add(title: "ProHUD") - sheet.add(action: "OK") - } - } - } list.add(title: "事件管理") { section in - section.add(title: "拦截返回事件") { + section.add(title: "拦截点击背景事件") { Sheet { sheet in sheet.add(title: "ProHUD") sheet.add(message: "点击背景将不会dismiss,必须在下方做出选择才能关掉") @@ -84,7 +76,7 @@ class SheetVC: ListVC { } onTappedBackground: { sheet in print("点击了背景") Toast.lazyPush(identifier: "alert") { toast in - toast.vm = .note + toast.vm = .error toast.vm.title = "点击了背景" toast.vm.message = "点击背景将不会dismiss,必须在下方做出选择才能关掉" toast.vm.duration = 2 @@ -206,6 +198,15 @@ class SheetVC: ListVC { } } } + + section.add(title: "全屏Sheet") { + Sheet { sheet in + sheet.config.isFullScreen = true + sheet.add(title: "ProHUD") + sheet.add(action: "OK") + } + } + } } diff --git a/PHDemo/PHDemo/ToastVC.swift b/PHDemo/PHDemo/ToastVC.swift index d3dcffc..56c3822 100644 --- a/PHDemo/PHDemo/ToastVC.swift +++ b/PHDemo/PHDemo/ToastVC.swift @@ -39,7 +39,7 @@ class ToastVC: ListVC { toast.update(progress: 1) } DispatchQueue.main.asyncAfter(deadline: .now() + 2.5) { - Toast.update(identifier: "loading") { toast in + Toast.find(identifier: "loading") { toast in toast.vm = .success(2).title("加载成功").message("这条通知2s后消失") } } @@ -106,13 +106,35 @@ class ToastVC: ListVC { } } + list.add(title: "实例管理") { section in + var i = 0 + section.add(title: "多实例共存") { + Toast(.loading.title("多实例共存").message("直接创建的实例,以平铺方式排列").duration(2)).push() + } + section.add(title: "不存在就创建,存在就更新") { + i += 1 + Toast.lazyPush(identifier: "loading") { toast in + toast.vm = .loading.title("正在加载\(i)").message("这条消息不会重复显示多条") + } + } + section.add(title: "如果存在就更新,如果不存在就忽略") { + i += 1 + Toast.find(identifier: "loading") { toast in + toast.vm = .success.title("加载完成\(i)").message("这条消息不会重复显示多条").duration(2) + } + } + section.add(title: "移除指定实例") { + Toast.find(identifier: "loading")?.pop() + } + } + list.add(title: "自定义视图") { section in section.add(title: "图片") { Toast { toast in toast.config.cardMaxHeight = 200 // 自定义视图时需手动指定高度 let imgv = UIImageView(image: UIImage(named: "landscape")) imgv.contentMode = .scaleAspectFill - toast.add(customView: imgv).snp.makeConstraints { make in + toast.add(subview: imgv).snp.makeConstraints { make in make.edges.equalToSuperview() } toast.onTapped { toast in @@ -121,6 +143,43 @@ class ToastVC: ListVC { } } + section.add(title: "圆角半径") { + func foo() { + Toast { toast in + toast.vm = .title("圆角半径").message("可以在共享配置中设置,则全局生效") + toast.add(action: "8", style: .gray) { toast in + Toast.Configuration.shared { config in + config.cardCornerRadius = 8 + } + toast.pop() + foo() + } + toast.add(action: "16", style: .gray) { toast in + Toast.Configuration.shared { config in + config.cardCornerRadius = 16 + } + toast.pop() + foo() + } + toast.add(action: "24", style: .gray) { toast in + Toast.Configuration.shared { config in + config.cardCornerRadius = 24 + } + toast.pop() + foo() + } + toast.add(action: "32", style: .gray) { toast in + Toast.Configuration.shared { config in + config.cardCornerRadius = 32 + } + toast.pop() + foo() + } + } + } + foo() + } + section.add(title: "其它控件") { Toast { toast in toast.config.cardMaxHeight = 200 // 自定义视图时需手动指定高度 @@ -158,7 +217,7 @@ class ToastVC: ListVC { make.height.equalTo(40) } - toast.add(customView: stack).snp.makeConstraints { make in + toast.add(subview: stack).snp.makeConstraints { make in make.edges.equalToSuperview().inset(8) } toast.onTapped { toast in diff --git a/Sources/ProHUD/Alert/AlertManager.swift b/Sources/ProHUD/Alert/AlertManager.swift index 14c74ca..1526a0e 100644 --- a/Sources/ProHUD/Alert/AlertManager.swift +++ b/Sources/ProHUD/Alert/AlertManager.swift @@ -85,29 +85,24 @@ extension Alert { /// 更新HUD实例 /// - Parameter callback: 实例更新代码 - public func update(callback: @escaping (_ alert: Alert) -> Void) { - callback(self) - } - - /// 更新HUD实例 - /// - Parameters: - /// - identifier: 唯一标识符 - /// - callback: 实例更新代码 - public static func update(identifier: String, callback: @escaping (_ alert: Alert) -> Void) { - guard let vc = AlertWindow.alerts.last(where: { $0.identifier == identifier }) else { - return + func update(handler: @escaping (_ alert: Alert) -> Void) { + handler(self) + reloadData() + UIView.animateEaseOut(duration: config.animateDurationForReloadByDefault) { + self.view.layoutIfNeeded() } - callback(vc) - vc.reloadData() } /// 查找HUD实例 /// - Parameter identifier: 唯一标识符 /// - Returns: HUD实例 - public static func find(identifier: String) -> Alert? { + public static func find(identifier: String, update handler: ((_ alert: Alert) -> Void)? = nil) -> Alert? { guard let vc = AlertWindow.alerts.last(where: { $0.identifier == identifier }) else { return nil } + if let handler = handler { + vc.update(handler: handler) + } return vc } diff --git a/Sources/ProHUD/Core/Utils/ConsolePrint.swift b/Sources/ProHUD/Core/Utils/ConsolePrint.swift index 089b29f..768b085 100644 --- a/Sources/ProHUD/Core/Utils/ConsolePrint.swift +++ b/Sources/ProHUD/Core/Utils/ConsolePrint.swift @@ -10,6 +10,6 @@ import Foundation func consolePrint(_ items: Any...) { #if DEBUG guard Configuration.enablePrint else { return } - print(">> ProHUD: ", items) + print(">> ProHUD:", items) #endif } diff --git a/Sources/ProHUD/Sheet/Sheet.swift b/Sources/ProHUD/Sheet/Sheet.swift index bfe02d0..f89c882 100644 --- a/Sources/ProHUD/Sheet/Sheet.swift +++ b/Sources/ProHUD/Sheet/Sheet.swift @@ -73,29 +73,6 @@ public class Sheet: Controller { } extension Sheet { - func translateIn(completion: (() -> Void)?) { - UIView.animateEaseOut(duration: config.animateDurationForBuildInByDefault) { - self._translateIn() - } completion: { done in - completion?() - } - } - func translateOut(completion: (() -> Void)?) { - UIView.animateEaseOut(duration: config.animateDurationForBuildOutByDefault) { - self._translateOut() - } completion: { done in - completion?() - } - } - - func _translateIn() { - backgroundView.alpha = 1 - contentView.transform = .identity - } - func _translateOut() { - backgroundView.alpha = 0 - contentView.transform = .init(translationX: 0, y: view.frame.size.height - contentView.frame.minY + config.margin) - } @objc func _onTappedBackground(_ sender: UITapGestureRecognizer) { if let act = onTappedBackground { act(self) diff --git a/Sources/ProHUD/Sheet/SheetManager.swift b/Sources/ProHUD/Sheet/SheetManager.swift index b2f761b..26b635e 100644 --- a/Sources/ProHUD/Sheet/SheetManager.swift +++ b/Sources/ProHUD/Sheet/SheetManager.swift @@ -18,3 +18,33 @@ extension Sheet: HUD { } } + +extension Sheet { + + func translateIn(completion: (() -> Void)?) { + UIView.animateEaseOut(duration: config.animateDurationForBuildInByDefault) { + self._translateIn() + } completion: { done in + completion?() + } + } + + func translateOut(completion: (() -> Void)?) { + UIView.animateEaseOut(duration: config.animateDurationForBuildOutByDefault) { + self._translateOut() + } completion: { done in + completion?() + } + } + + func _translateIn() { + backgroundView.alpha = 1 + contentView.transform = .identity + } + + func _translateOut() { + backgroundView.alpha = 0 + contentView.transform = .init(translationX: 0, y: view.frame.size.height - contentView.frame.minY + config.margin) + } + +} diff --git a/Sources/ProHUD/Toast/ToastConvenienceLayout.swift b/Sources/ProHUD/Toast/ToastConvenienceLayout.swift index 534bc23..5960195 100644 --- a/Sources/ProHUD/Toast/ToastConvenienceLayout.swift +++ b/Sources/ProHUD/Toast/ToastConvenienceLayout.swift @@ -9,12 +9,12 @@ import UIKit public extension Toast { - @discardableResult func add(customView: UIView) -> UIView { + @discardableResult func add(subview: UIView) -> UIView { if contentStack.superview != nil { contentStack.removeFromSuperview() } - contentView.addSubview(customView) - return customView + contentView.addSubview(subview) + return subview } @discardableResult func add(action: Action) -> Button { diff --git a/Sources/ProHUD/Toast/ToastManager.swift b/Sources/ProHUD/Toast/ToastManager.swift index a223de2..4c788b3 100644 --- a/Sources/ProHUD/Toast/ToastManager.swift +++ b/Sources/ProHUD/Toast/ToastManager.swift @@ -40,29 +40,24 @@ extension Toast { /// 更新HUD实例 /// - Parameter callback: 实例更新代码 - public func update(callback: @escaping (_ toast: Toast) -> Void) { - callback(self) - } - - /// 更新HUD实例 - /// - Parameters: - /// - identifier: 唯一标识符 - /// - callback: 实例更新代码 - public static func update(identifier: String, callback: @escaping (_ toast: Toast) -> Void) { - guard let vc = ToastWindow.windows.last(where: { $0.toast.identifier == identifier })?.toast else { - return + func update(handler: @escaping (_ toast: Toast) -> Void) { + handler(self) + reloadData() + UIView.animateEaseOut(duration: config.animateDurationForReloadByDefault) { + self.view.layoutIfNeeded() } - callback(vc) - vc.reloadData() } /// 查找HUD实例 /// - Parameter identifier: 唯一标识符 /// - Returns: HUD实例 - public static func find(identifier: String) -> Toast? { + @discardableResult public static func find(identifier: String, update handler: ((_ toast: Toast) -> Void)? = nil) -> Toast? { guard let vc = ToastWindow.windows.last(where: { $0.toast.identifier == identifier })?.toast else { return nil } + if let handler = handler { + vc.update(handler: handler) + } return vc }