2019-07-31 17:40:39 +08:00
|
|
|
|
//
|
2019-08-03 17:48:37 +08:00
|
|
|
|
// cfg.toast.swift
|
2019-07-31 17:40:39 +08:00
|
|
|
|
// ProHUD
|
|
|
|
|
//
|
|
|
|
|
// Created by xaoxuu on 2019/7/31.
|
|
|
|
|
// Copyright © 2019 Titan Studio. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
2020-06-10 13:08:34 +08:00
|
|
|
|
import UIKit
|
2019-07-31 17:40:39 +08:00
|
|
|
|
import SnapKit
|
|
|
|
|
|
2020-06-19 19:13:33 +08:00
|
|
|
|
public extension UIWindow.Level {
|
|
|
|
|
static let proToast = UIWindow.Level.alert + 1000
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-31 17:40:39 +08:00
|
|
|
|
public extension ProHUD.Configuration {
|
|
|
|
|
struct Toast {
|
2019-08-03 17:48:37 +08:00
|
|
|
|
// MARK: 卡片样式
|
2019-07-31 17:40:39 +08:00
|
|
|
|
/// 最大宽度(用于优化横屏或者iPad显示)
|
2019-08-02 13:55:23 +08:00
|
|
|
|
public var maxWidth = CGFloat(556)
|
2019-07-31 17:40:39 +08:00
|
|
|
|
/// 圆角半径
|
2019-08-01 17:02:17 +08:00
|
|
|
|
public var cornerRadius = CGFloat(12)
|
2019-08-03 17:48:37 +08:00
|
|
|
|
/// 余量:元素与元素之间的距离
|
2019-07-31 17:40:39 +08:00
|
|
|
|
public var margin = CGFloat(8)
|
2019-08-03 17:48:37 +08:00
|
|
|
|
/// 填充:元素内部控件距离元素边界的距离
|
2019-07-31 17:40:39 +08:00
|
|
|
|
public var padding = CGFloat(16)
|
2020-06-19 13:50:17 +08:00
|
|
|
|
/// 行间距
|
|
|
|
|
public var lineSpace = CGFloat(4)
|
2019-08-03 17:48:37 +08:00
|
|
|
|
// MARK: 图标样式
|
|
|
|
|
/// 图标尺寸
|
2020-06-19 19:13:33 +08:00
|
|
|
|
public var iconSize = CGSize(width: 44, height: 44)
|
2019-07-31 17:40:39 +08:00
|
|
|
|
|
2019-08-03 17:48:37 +08:00
|
|
|
|
// MARK: 文本样式
|
|
|
|
|
/// 标题字体
|
|
|
|
|
public var titleFont = UIFont.boldSystemFont(ofSize: 18)
|
|
|
|
|
/// 标题最多行数
|
2020-06-19 09:49:24 +08:00
|
|
|
|
public var titleMaxLines = Int(5)
|
2019-07-31 17:40:39 +08:00
|
|
|
|
|
2019-08-03 17:48:37 +08:00
|
|
|
|
/// 正文字体
|
|
|
|
|
public var bodyFont = UIFont.systemFont(ofSize: 16)
|
|
|
|
|
/// 正文最多行数
|
2020-06-19 09:49:24 +08:00
|
|
|
|
public var bodyMaxLines = Int(20)
|
2019-08-01 17:02:17 +08:00
|
|
|
|
|
2019-07-31 17:40:39 +08:00
|
|
|
|
/// 更新视图
|
|
|
|
|
/// - Parameter callback: 回调代码
|
2019-08-01 17:02:17 +08:00
|
|
|
|
public mutating func reloadData(_ callback: @escaping (ProHUD.Toast) -> Void) {
|
2019-08-03 17:48:37 +08:00
|
|
|
|
privReloadData = callback
|
2019-07-31 17:40:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-12 15:02:36 +08:00
|
|
|
|
|
2019-07-31 17:40:39 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-31 21:08:25 +08:00
|
|
|
|
|
2019-08-12 15:02:36 +08:00
|
|
|
|
// MARK: - 内部调用
|
2021-07-20 16:04:39 +08:00
|
|
|
|
extension ProHUD.Configuration.Toast {
|
2019-08-12 15:02:36 +08:00
|
|
|
|
|
2019-08-03 17:48:37 +08:00
|
|
|
|
var reloadData: (ProHUD.Toast) -> Void {
|
|
|
|
|
return privReloadData
|
2019-07-31 21:08:25 +08:00
|
|
|
|
}
|
2019-08-12 15:02:36 +08:00
|
|
|
|
|
2019-08-12 17:59:40 +08:00
|
|
|
|
|
2019-07-31 21:08:25 +08:00
|
|
|
|
}
|
2019-08-03 17:48:37 +08:00
|
|
|
|
|
2019-08-12 15:02:36 +08:00
|
|
|
|
// MARK: - 默认实现
|
2019-08-03 17:48:37 +08:00
|
|
|
|
fileprivate var privReloadData: (ProHUD.Toast) -> Void = {
|
|
|
|
|
return { (vc) in
|
|
|
|
|
debug(vc, "reloadData")
|
|
|
|
|
let config = cfg.toast
|
2019-08-12 15:02:36 +08:00
|
|
|
|
let scene = vc.vm.scene
|
|
|
|
|
if vc.imageView.superview == nil {
|
|
|
|
|
vc.view.addSubview(vc.imageView)
|
|
|
|
|
}
|
2020-06-19 13:50:17 +08:00
|
|
|
|
if vc.textStack.superview == nil {
|
|
|
|
|
vc.view.addSubview(vc.textStack)
|
|
|
|
|
vc.textStack.addArrangedSubview(vc.titleLabel)
|
|
|
|
|
vc.textStack.addArrangedSubview(vc.bodyLabel)
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-12 15:02:36 +08:00
|
|
|
|
// 设置数据
|
2019-08-13 11:32:27 +08:00
|
|
|
|
vc.imageView.image = vc.vm.icon ?? vc.vm.scene.image
|
2019-08-12 17:59:40 +08:00
|
|
|
|
vc.imageView.layer.removeAllAnimations()
|
2019-08-03 17:48:37 +08:00
|
|
|
|
vc.titleLabel.textColor = cfg.primaryLabelColor
|
2019-08-13 11:32:27 +08:00
|
|
|
|
vc.titleLabel.text = vc.vm.title ?? vc.vm.scene.title
|
2019-08-03 17:48:37 +08:00
|
|
|
|
vc.bodyLabel.textColor = cfg.secondaryLabelColor
|
2019-08-13 11:32:27 +08:00
|
|
|
|
vc.bodyLabel.text = vc.vm.message ?? vc.vm.scene.message
|
2019-08-03 17:48:37 +08:00
|
|
|
|
|
2020-06-19 13:50:17 +08:00
|
|
|
|
if let count = vc.titleLabel.text?.count, count > 0 {
|
|
|
|
|
vc.textStack.insertArrangedSubview(vc.titleLabel, at: 0)
|
|
|
|
|
} else {
|
|
|
|
|
vc.titleLabel.removeFromSuperview()
|
|
|
|
|
}
|
|
|
|
|
if let count = vc.bodyLabel.text?.count, count > 0 {
|
|
|
|
|
vc.textStack.addArrangedSubview(vc.bodyLabel)
|
|
|
|
|
} else {
|
|
|
|
|
vc.bodyLabel.removeFromSuperview()
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-03 17:48:37 +08:00
|
|
|
|
// 更新布局
|
|
|
|
|
vc.imageView.snp.makeConstraints { (mk) in
|
|
|
|
|
mk.top.equalToSuperview().offset(config.padding)
|
|
|
|
|
mk.leading.equalToSuperview().offset(config.padding)
|
|
|
|
|
mk.bottom.lessThanOrEqualToSuperview().offset(-config.padding)
|
|
|
|
|
mk.width.height.equalTo(config.iconSize)
|
|
|
|
|
}
|
2020-06-19 13:50:17 +08:00
|
|
|
|
vc.textStack.snp.makeConstraints { (mk) in
|
2019-08-03 17:48:37 +08:00
|
|
|
|
mk.top.equalToSuperview().offset(config.padding)
|
2020-06-19 13:50:17 +08:00
|
|
|
|
mk.leading.equalTo(vc.imageView.snp.trailing).offset(config.padding - 4)
|
2019-08-03 17:48:37 +08:00
|
|
|
|
mk.leading.greaterThanOrEqualToSuperview().offset(config.padding)
|
|
|
|
|
mk.trailing.equalToSuperview().offset(-config.padding)
|
|
|
|
|
mk.bottom.lessThanOrEqualToSuperview().offset(-config.padding)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vc.view.layoutIfNeeded()
|
2019-08-12 17:59:40 +08:00
|
|
|
|
|
|
|
|
|
// 设置持续时间
|
|
|
|
|
vc.vm.updateDuration()
|
|
|
|
|
|
2020-06-24 11:28:54 +08:00
|
|
|
|
// 移除动画
|
|
|
|
|
vc.stopRotate(vc.animateLayer)
|
|
|
|
|
vc.animateLayer = nil
|
|
|
|
|
vc.animation = nil
|
|
|
|
|
|
|
|
|
|
// 移除进度
|
|
|
|
|
vc.progressView?.removeFromSuperview()
|
|
|
|
|
|
2020-06-24 11:12:42 +08:00
|
|
|
|
// id 包含 .rotate 的会自动旋转
|
|
|
|
|
if vc.vm.scene.identifier.contains(".rotate") {
|
|
|
|
|
vc.startRotate()
|
|
|
|
|
}
|
2020-06-22 16:34:21 +08:00
|
|
|
|
|
2019-08-12 15:02:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}()
|