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