2019-07-31 17:40:39 +08:00
|
|
|
|
//
|
2019-08-03 17:48:37 +08:00
|
|
|
|
// cfg.guard.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
|
|
|
|
|
|
|
|
|
|
public extension ProHUD.Configuration {
|
|
|
|
|
struct Guard {
|
2019-08-03 16:22:50 +08:00
|
|
|
|
// MARK: 卡片样式
|
2019-07-31 17:40:39 +08:00
|
|
|
|
/// 最大宽度(用于优化横屏或者iPad显示)
|
2019-08-03 16:22:50 +08:00
|
|
|
|
public var cardMaxWidth = CGFloat(460)
|
|
|
|
|
/// 圆角半径(手机竖屏时为0)
|
|
|
|
|
public var cardCornerRadius = CGFloat(16)
|
|
|
|
|
|
|
|
|
|
/// 余量:元素与元素之间的距离
|
|
|
|
|
public var margin = CGFloat(8)
|
|
|
|
|
/// 填充:元素内部控件距离元素边界的距离
|
|
|
|
|
public var padding = CGFloat(16)
|
|
|
|
|
|
|
|
|
|
/// 颜色
|
|
|
|
|
public var tintColor: UIColor?
|
|
|
|
|
|
|
|
|
|
// MARK: 文本样式
|
2019-07-31 17:40:39 +08:00
|
|
|
|
/// 标题字体
|
2019-08-03 16:22:50 +08:00
|
|
|
|
public var titleFont = UIFont.boldSystemFont(ofSize: 22)
|
2019-08-08 09:25:28 +08:00
|
|
|
|
/// 副标题字体
|
|
|
|
|
public var subTitleFont = UIFont.boldSystemFont(ofSize: 20)
|
2019-07-31 17:40:39 +08:00
|
|
|
|
/// 正文字体
|
2019-08-03 16:22:50 +08:00
|
|
|
|
public var bodyFont = UIFont.systemFont(ofSize: 18)
|
2019-07-31 17:40:39 +08:00
|
|
|
|
|
2019-08-03 16:22:50 +08:00
|
|
|
|
// MARK: 按钮样式
|
|
|
|
|
/// 按钮字体
|
|
|
|
|
public var buttonFont = UIFont.boldSystemFont(ofSize: 18)
|
2019-08-03 18:03:20 +08:00
|
|
|
|
|
2019-08-03 16:22:50 +08:00
|
|
|
|
/// 按钮圆角半径
|
|
|
|
|
public var buttonCornerRadius = CGFloat(12)
|
2019-07-31 17:40:39 +08:00
|
|
|
|
|
|
|
|
|
/// 更新视图
|
|
|
|
|
/// - Parameter callback: 回调代码
|
2019-08-03 17:48:37 +08:00
|
|
|
|
public mutating func reloadData(_ callback: @escaping (ProHUD.Guard) -> Void) {
|
|
|
|
|
privReloadData = callback
|
2019-07-31 17:40:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-03 16:22:50 +08:00
|
|
|
|
|
|
|
|
|
|
2019-08-12 15:02:36 +08:00
|
|
|
|
// MARK: - 内部调用
|
2019-08-03 17:48:37 +08:00
|
|
|
|
internal extension ProHUD.Configuration.Guard {
|
2019-08-12 15:02:36 +08:00
|
|
|
|
|
2019-08-03 17:48:37 +08:00
|
|
|
|
var reloadData: (ProHUD.Guard) -> Void {
|
|
|
|
|
return privReloadData
|
2019-08-03 16:22:50 +08:00
|
|
|
|
}
|
2019-08-12 15:02:36 +08:00
|
|
|
|
|
2019-08-08 09:25:28 +08:00
|
|
|
|
var reloadStack: (ProHUD.Guard) -> Void {
|
|
|
|
|
return { (vc) in
|
|
|
|
|
if vc.textStack.arrangedSubviews.count > 0 {
|
|
|
|
|
vc.contentStack.addArrangedSubview(vc.textStack)
|
|
|
|
|
} else {
|
|
|
|
|
vc.textStack.removeFromSuperview()
|
|
|
|
|
}
|
|
|
|
|
if vc.actionStack.arrangedSubviews.count > 0 {
|
|
|
|
|
vc.contentStack.addArrangedSubview(vc.actionStack)
|
|
|
|
|
} else {
|
|
|
|
|
vc.actionStack.removeFromSuperview()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-03 16:22:50 +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.Guard) -> Void = {
|
|
|
|
|
return { (vc) in
|
|
|
|
|
debug(vc, "reloadData")
|
|
|
|
|
let config = cfg.guard
|
2019-08-09 18:02:41 +08:00
|
|
|
|
// background
|
|
|
|
|
vc.view.tintColor = config.tintColor
|
|
|
|
|
vc.view.backgroundColor = UIColor(white: 0, alpha: 0)
|
|
|
|
|
vc.view.addSubview(vc.contentView)
|
|
|
|
|
vc.contentView.contentView.addSubview(vc.contentStack)
|
2019-08-03 17:48:37 +08:00
|
|
|
|
// 更新布局
|
|
|
|
|
var width = UIScreen.main.bounds.width
|
|
|
|
|
if width > config.cardMaxWidth {
|
|
|
|
|
// 横屏或者iPad
|
|
|
|
|
width = config.cardMaxWidth
|
|
|
|
|
vc.contentView.layer.masksToBounds = true
|
|
|
|
|
vc.contentView.layer.cornerRadius = config.cardCornerRadius
|
|
|
|
|
} else {
|
|
|
|
|
vc.contentView.layer.shadowRadius = 4
|
|
|
|
|
vc.contentView.layer.shadowOffset = .init(width: 0, height: 2)
|
|
|
|
|
vc.contentView.layer.shadowOpacity = 0.12
|
|
|
|
|
}
|
|
|
|
|
vc.contentView.snp.makeConstraints { (mk) in
|
2019-08-12 17:59:40 +08:00
|
|
|
|
if isPortrait && vc.isFullScreen {
|
|
|
|
|
mk.top.equalToSuperview()
|
|
|
|
|
}
|
2019-08-03 17:48:37 +08:00
|
|
|
|
mk.centerX.equalToSuperview()
|
|
|
|
|
if UIDevice.current.userInterfaceIdiom == .phone {
|
2019-08-12 15:02:36 +08:00
|
|
|
|
if width < config.cardMaxWidth {
|
2019-08-03 17:48:37 +08:00
|
|
|
|
mk.bottom.equalToSuperview()
|
2019-08-12 15:02:36 +08:00
|
|
|
|
} else {
|
2020-06-22 10:14:47 +08:00
|
|
|
|
mk.bottom.equalToSuperview().offset(-ProHUD.safeAreaInsets.bottom)
|
2019-08-03 17:48:37 +08:00
|
|
|
|
}
|
|
|
|
|
} else if UIDevice.current.userInterfaceIdiom == .pad {
|
|
|
|
|
mk.centerY.equalToSuperview()
|
|
|
|
|
}
|
|
|
|
|
mk.width.equalTo(width)
|
|
|
|
|
}
|
|
|
|
|
// stack
|
|
|
|
|
vc.contentStack.snp.makeConstraints { (mk) in
|
2019-08-12 17:59:40 +08:00
|
|
|
|
if isPortrait && vc.isFullScreen {
|
2020-06-22 10:14:47 +08:00
|
|
|
|
mk.top.equalToSuperview().offset(ProHUD.safeAreaInsets.top)
|
2019-08-12 17:59:40 +08:00
|
|
|
|
} else {
|
|
|
|
|
mk.top.equalToSuperview().offset(config.padding)
|
|
|
|
|
}
|
2019-08-03 17:48:37 +08:00
|
|
|
|
mk.centerX.equalToSuperview()
|
2019-08-12 15:02:36 +08:00
|
|
|
|
if width < config.cardMaxWidth {
|
2020-06-22 10:14:47 +08:00
|
|
|
|
let bottom = ProHUD.safeAreaInsets.bottom
|
2019-08-12 15:02:36 +08:00
|
|
|
|
if bottom == 0 {
|
|
|
|
|
mk.bottom.equalToSuperview().offset(-config.padding)
|
|
|
|
|
} else {
|
|
|
|
|
mk.bottom.equalToSuperview().offset(-config.padding/2 - bottom)
|
|
|
|
|
}
|
2019-08-03 17:48:37 +08:00
|
|
|
|
} else {
|
2019-08-12 15:02:36 +08:00
|
|
|
|
mk.bottom.equalToSuperview().offset(-config.padding)
|
2019-08-03 17:48:37 +08:00
|
|
|
|
}
|
|
|
|
|
if isPortrait {
|
|
|
|
|
mk.width.equalToSuperview().offset(-config.padding * 2)
|
|
|
|
|
} else {
|
|
|
|
|
mk.width.equalToSuperview().offset(-config.padding * 4)
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-12 15:02:36 +08:00
|
|
|
|
config.reloadStack(vc)
|
2019-08-03 17:48:37 +08:00
|
|
|
|
}
|
|
|
|
|
}()
|