From 1da52428443539fe9b2444989c9fde29965ae115 Mon Sep 17 00:00:00 2001 From: xaoxuu Date: Fri, 2 Aug 2019 14:50:42 +0800 Subject: [PATCH] update --- Example/Example/ViewController.swift | 22 +++++++-- ProHUD/Alert/AlertConfig.swift | 70 +++++++++++++++++----------- ProHUD/Alert/AlertController.swift | 12 ++++- ProHUD/Toast/ToastController.swift | 2 +- 4 files changed, 72 insertions(+), 34 deletions(-) diff --git a/Example/Example/ViewController.swift b/Example/Example/ViewController.swift index cf9ce62..2e4aa27 100644 --- a/Example/Example/ViewController.swift +++ b/Example/Example/ViewController.swift @@ -17,7 +17,7 @@ class ViewController: UIViewController { ProHUD.configAlert { (alert) in - alert.minimizeTimeout = 1 + alert.minimizeTimeout = 5 } @@ -26,11 +26,23 @@ class ViewController: UIViewController { override func touchesBegan(_ touches: Set, with event: UIEvent?) { - ProHUD.show(alert: .delete, title: "确认删除", message: "此操作不可撤销").timeout(nil) + let a = ProHUD.show(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: "啊哈哈哈哈").timeout(2) + a?.addAction(style: .default, title: "我知道了", action: { +// a?.remove() + a?.removeAction(index: 0) + }) + } + }).addAction(style: .cancel, title: "取消", action: nil) - ProHUD.show(alert: .loading, title: "确认删除", message: "此操作不可撤销").timeout(nil) - - ProHUD.show(alert: .confirm, title: "确认删除", message: "此操作不可撤销").timeout(3) +// +// ProHUD.show(alert: .loading, title: "确认删除", message: "此操作不可撤销").timeout(nil) +// +// ProHUD.show(alert: .confirm, title: "确认删除", message: "此操作不可撤销").timeout(3) // // a.addAction(style: .destructive, title: "删除") { [weak a] in diff --git a/ProHUD/Alert/AlertConfig.swift b/ProHUD/Alert/AlertConfig.swift index 6fea3d5..05fb8e5 100644 --- a/ProHUD/Alert/AlertConfig.swift +++ b/ProHUD/Alert/AlertConfig.swift @@ -60,24 +60,26 @@ public extension ProHUD.Configuration { lazy var loadSubviews: (ProHUD.Alert, Alert) -> Void = { return { (vc, config) in debugPrint(vc, "loadSubviews") - vc.view.addSubview(vc.contentView) - vc.contentView.contentView.addSubview(vc.contentStack) - - vc.contentStack.spacing = alertConfig.margin + alertConfig.padding - - vc.contentView.layer.masksToBounds = true - vc.contentView.layer.cornerRadius = alertConfig.cornerRadius - - vc.contentView.snp.makeConstraints { (mk) in - mk.center.equalToSuperview() - mk.width.lessThanOrEqualTo(CGFloat.minimum(UIScreen.main.bounds.width * 0.68, alertConfig.maxWidth)) - } - vc.contentStack.snp.makeConstraints { (mk) in - mk.centerX.equalToSuperview() - mk.top.equalToSuperview().offset(alertConfig.padding) - mk.bottom.equalToSuperview().offset(-alertConfig.padding) - mk.leading.equalToSuperview().offset(alertConfig.padding) - mk.trailing.equalToSuperview().offset(-alertConfig.padding) + if vc.contentView.superview == nil { + vc.view.addSubview(vc.contentView) + vc.contentView.contentView.addSubview(vc.contentStack) + + vc.contentStack.spacing = alertConfig.margin + alertConfig.padding + + vc.contentView.layer.masksToBounds = true + vc.contentView.layer.cornerRadius = alertConfig.cornerRadius + + vc.contentView.snp.makeConstraints { (mk) in + mk.center.equalToSuperview() + mk.width.lessThanOrEqualTo(CGFloat.minimum(UIScreen.main.bounds.width * 0.68, alertConfig.maxWidth)) + } + vc.contentStack.snp.makeConstraints { (mk) in + mk.centerX.equalToSuperview() + mk.top.equalToSuperview().offset(alertConfig.padding) + mk.bottom.equalToSuperview().offset(-alertConfig.padding) + mk.leading.equalToSuperview().offset(alertConfig.padding) + mk.trailing.equalToSuperview().offset(-alertConfig.padding) + } } } @@ -184,7 +186,15 @@ public extension ProHUD.Configuration { vc.textStack.removeFromSuperview() } if vc.actionStack.superview != nil { - vc.contentStack.addArrangedSubview(vc.actionStack) + if isFirstLayout { + vc.contentStack.addArrangedSubview(vc.actionStack) + } else { + vc.actionStack.transform = .init(scaleX: 1, y: 0.001) + UIView.animateForAlert { + vc.contentStack.addArrangedSubview(vc.actionStack) + vc.view.layoutIfNeeded() + } + } // 适配横竖屏和iPad if isPortrait == false { vc.actionStack.axis = .horizontal @@ -195,17 +205,25 @@ public extension ProHUD.Configuration { mk.width.greaterThanOrEqualTo(200) mk.leading.trailing.equalToSuperview() } - } - if isFirstLayout { - vc.view.layoutIfNeeded() - vc.imageView?.transform = .init(scaleX: 0.75, y: 0.75) - } else { + if isFirstLayout == false { + UIView.animateForAlert { + vc.actionStack.transform = .identity + } + } } - UIView.animateForAlert { - vc.imageView?.transform = .identity + if isFirstLayout { vc.view.layoutIfNeeded() + vc.imageView?.transform = .init(scaleX: 0.75, y: 0.75) + UIView.animateForAlert { + vc.imageView?.transform = .identity + vc.view.layoutIfNeeded() + } + } else { + UIView.animateForAlert { + vc.view.layoutIfNeeded() + } } } }() diff --git a/ProHUD/Alert/AlertController.swift b/ProHUD/Alert/AlertController.swift index f427cf6..a1b9171 100644 --- a/ProHUD/Alert/AlertController.swift +++ b/ProHUD/Alert/AlertController.swift @@ -127,7 +127,13 @@ public extension ProHUD { if actionStack.superview == nil { contentStack.addArrangedSubview(actionStack) } + self.view.layoutIfNeeded() + button.transform = .init(scaleX: 1, y: 0.001) actionStack.addArrangedSubview(button) + UIView.animateForAlert { + button.transform = .identity + self.view.layoutIfNeeded() + } addTouchUpAction(for: button) { [weak self] in action?() if button.tag == UIAlertAction.Style.cancel.rawValue { @@ -201,14 +207,16 @@ public extension ProHUD { } else if index < self.actionStack.arrangedSubviews.count, let btn = self.actionStack.arrangedSubviews[index] as? UIButton { btn.removeFromSuperview() } + if self.actionStack.arrangedSubviews.count == 0 { + self.actionStack.removeFromSuperview() + } + willLayoutSubviews() UIView.animateForAlert { self.view.layoutIfNeeded() } return self } - - } } diff --git a/ProHUD/Toast/ToastController.swift b/ProHUD/Toast/ToastController.swift index 9f9c61c..c5f76bc 100644 --- a/ProHUD/Toast/ToastController.swift +++ b/ProHUD/Toast/ToastController.swift @@ -365,7 +365,7 @@ public extension ProHUD { return shared.show(toast: toast, title: title, message: message, icon: icon) } - class func alert(identifier: String?) -> [Toast] { + class func toast(identifier: String?) -> [Toast] { return shared.toasts(identifier: identifier) }