From 6eee00880bbeb8eacd5e83ea178bc69b5f76d17f Mon Sep 17 00:00:00 2001 From: xaoxuu Date: Thu, 17 Aug 2023 21:26:27 +0800 Subject: [PATCH] =?UTF-8?q?=E9=BB=98=E8=AE=A4=E5=8A=A8=E7=94=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PHDemo/PHDemo/CapsuleVC.swift | 63 +++------------ Sources/ProHUD/Capsule/Capsule.swift | 4 + Sources/ProHUD/Capsule/CapsuleManager.swift | 85 ++++++++++++++++----- 3 files changed, 82 insertions(+), 70 deletions(-) diff --git a/PHDemo/PHDemo/CapsuleVC.swift b/PHDemo/PHDemo/CapsuleVC.swift index d4f68d8..d534b06 100644 --- a/PHDemo/PHDemo/CapsuleVC.swift +++ b/PHDemo/PHDemo/CapsuleVC.swift @@ -58,43 +58,20 @@ class CapsuleVC: ListVC { } list.add(title: "不同位置、不同动画") { section in - section.add(title: "顶部,缩放") { + section.add(title: "顶部,默认滑入") { Capsule(.info("一条简短的消息")) { capsule in - capsule.config.animateBuildIn { window, completion in - let duration = 1.0 - let d0 = duration * 0.2 - let d1 = duration - window.transform = .init(scaleX: 0.001, y: 0.001) - window.alpha = 0 - UIView.animate(withDuration: d0, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0.5) { - window.transform = .init(scaleX: 0.01, y: 0.5) - } - UIView.animate(withDuration: d1, delay: d0 * 0.2, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5) { - window.transform = .identity - } completion: { done in - completion() - } - UIView.animate(withDuration: duration * 0.4, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1) { - window.alpha = 1 - } - } - capsule.config.animateBuildOut { window, completion in - let duration = 0.8 - UIView.animate(withDuration: duration, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1) { - window.transform = .init(scaleX: 0.0001, y: 0.5) - } completion: { done in - completion() - } - UIView.animate(withDuration: duration * 0.6, delay: duration * 0.4, usingSpringWithDamping: 1, initialSpringVelocity: 1) { - window.alpha = 0 - } - } + + } + } + section.add(title: "中间,默认缩放") { + Capsule(.middle.info("一条简短的消息")) { capsule in + } } section.add(title: "中间,黑底白字,透明渐变") { Capsule(.middle.info("一条简短的消息")) { capsule in capsule.config.tintColor = .white - capsule.config.cardCornerRadius = 4 + capsule.config.cardCornerRadius = 8 capsule.config.contentViewMask { mask in mask.layer.backgroundColor = UIColor.black.withAlphaComponent(0.75).cgColor } @@ -118,11 +95,13 @@ class CapsuleVC: ListVC { } } } - section.add(title: "底部,渐变背景,回弹滑入") { + section.add(title: "底部,渐变背景,默认回弹滑入") { Capsule(.bottom.enter("点击进入")) { capsule in capsule.config.tintColor = .white + capsule.config.cardEdgeInsets = .init(top: 16, left: 24, bottom: 16, right: 24) capsule.config.customTextLabel { label in label.textColor = .white + label.font = .boldSystemFont(ofSize: 16) } capsule.config.contentViewMask { mask in mask.effect = .none @@ -137,24 +116,6 @@ class CapsuleVC: ListVC { mask.layer.insertSublayer(gradientLayer, at: 0) } capsule.config.cardCornerRadius = .infinity - capsule.config.animateBuildIn { window, completion in - window.transform = .init(translationX: 0, y: 240) - window.alpha = 0 - UIView.animate(withDuration: 0.8, delay: 0, usingSpringWithDamping: 0.75, initialSpringVelocity: 0.5) { - window.transform = .identity - window.alpha = 1 - } completion: { done in - completion() - } - } - capsule.config.animateBuildOut { window, completion in - UIView.animate(withDuration: 0.8, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0.5) { - window.transform = .init(translationX: 0, y: 240) - window.alpha = 0 - } completion: { done in - completion() - } - } }.onTapped { capsule in Alert(.message("收到点击事件").duration(1)).push() capsule.pop() @@ -181,7 +142,7 @@ extension Capsule.ViewModel { func enter(_ text: String?) -> Self { self.message(text) .icon(.init(systemName: "arrow.right.circle.fill")) - .duration(.infinity) +// .duration(.infinity) } static var systemError: Self { diff --git a/Sources/ProHUD/Capsule/Capsule.swift b/Sources/ProHUD/Capsule/Capsule.swift index 41d250b..bb22727 100644 --- a/Sources/ProHUD/Capsule/Capsule.swift +++ b/Sources/ProHUD/Capsule/Capsule.swift @@ -54,6 +54,10 @@ open class Capsule: Controller { @objc public var position: Position = .top + public func position(position: Position) -> Self { + self.position = position + return self + } public static var top: Self { let obj = Self.init() obj.position = .top diff --git a/Sources/ProHUD/Capsule/CapsuleManager.swift b/Sources/ProHUD/Capsule/CapsuleManager.swift index 69bb04d..291d271 100644 --- a/Sources/ProHUD/Capsule/CapsuleManager.swift +++ b/Sources/ProHUD/Capsule/CapsuleManager.swift @@ -62,18 +62,45 @@ extension Capsule: HUD { navEvents[.onViewWillAppear]?(self) if isNew { window.isHidden = false + func completion() { + self.navEvents[.onViewDidAppear]?(self) + } if let animateBuildIn = config.animateBuildIn { - animateBuildIn(window) { - self.navEvents[.onViewDidAppear]?(self) - } + animateBuildIn(window, completion) } else { let duration = config.animateDurationForBuildInByDefault - window.transform = .init(translationX: 0, y: -window.frame.maxY - 20) - UIView.animateEaseOut(duration: duration) { - window.transform = .identity - } completion: { done in - self.navEvents[.onViewDidAppear]?(self) + switch position { + case .top: + window.transform = .init(translationX: 0, y: -window.frame.maxY - 20) + UIView.animateEaseOut(duration: duration) { + window.transform = .identity + } completion: { done in + completion() + } + case .middle: + let d0 = duration * 0.2 + let d1 = duration + window.transform = .init(scaleX: 0.001, y: 0.001) + window.alpha = 0 + UIView.animate(withDuration: duration, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5) { + window.transform = .identity + } completion: { done in + completion() + } + UIView.animate(withDuration: duration * 0.4, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1) { + window.alpha = 1 + } + case .bottom: + let offsetY = AppContext.appBounds.height - newFrame.maxY + 100 + window.transform = .init(translationX: 0, y: offsetY) + UIView.animate(withDuration: duration, delay: 0, usingSpringWithDamping: 0.75, initialSpringVelocity: 0.5) { + window.transform = .identity + } completion: { done in + completion() + } } + + } } else { view.layoutIfNeeded() @@ -91,21 +118,41 @@ extension Capsule: HUD { guard let window = attachedWindow, let windowScene = windowScene else { return } AppContext.capsuleWindows[windowScene]?[vm.position] = nil navEvents[.onViewWillDisappear]?(self) + func completion() { + window.isHidden = true + window.transform = .identity + self.navEvents[.onViewDidDisappear]?(self) + } if let animateBuildOut = config.animateBuildOut { - animateBuildOut(window) { - window.isHidden = true - window.transform = .identity - self.navEvents[.onViewDidAppear]?(self) - } + animateBuildOut(window, completion) } else { let duration = config.animateDurationForBuildOutByDefault - UIView.animateEaseOut(duration: duration) { - window.transform = .init(translationX: 0, y: -window.frame.maxY - 20) - } completion: { done in - window.isHidden = true - window.transform = .identity - self.navEvents[.onViewDidDisappear]?(self) + let oldFrame = window.frame + switch vm.position { + case .top: + UIView.animateEaseOut(duration: duration) { + window.transform = .init(translationX: 0, y: -oldFrame.maxY - 20) + } completion: { done in + completion() + } + case .middle: + UIView.animate(withDuration: duration * 0.6, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0.5) { + window.transform = .init(scaleX: 0.001, y: 0.001) + } completion: { done in + completion() + } + UIView.animate(withDuration: duration * 0.4, delay: duration * 0.2, usingSpringWithDamping: 1, initialSpringVelocity: 0.5) { + window.alpha = 0 + } + case .bottom: + let offsetY = AppContext.appBounds.height - oldFrame.maxY + 100 + UIView.animate(withDuration: duration, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0) { + window.transform = .init(translationX: 0, y: offsetY) + } completion: { done in + completion() + } } + } }