mirror of https://github.com/xaoxuu/ProHUD
代码优化
This commit is contained in:
parent
398a9f8472
commit
a41d03ca00
|
@ -71,14 +71,17 @@ class AlertVC: ListVC {
|
||||||
list.add(title: "文字 + 按钮") { section in
|
list.add(title: "文字 + 按钮") { section in
|
||||||
section.add(title: "只有一小段文字 + 无背景色按钮") {
|
section.add(title: "只有一小段文字 + 无背景色按钮") {
|
||||||
Alert { alert in
|
Alert { alert in
|
||||||
|
alert.config.cardMinWidth = 270
|
||||||
|
alert.config.cardEdgeInsets = .init(top: 0, left: 0, bottom: 16, right: 0)
|
||||||
|
alert.config.textEdgeInsets = .init(top: 30, left: 32, bottom: 30, right: 32)
|
||||||
alert.config.cardCornerRadius = 12
|
alert.config.cardCornerRadius = 12
|
||||||
alert.vm.title = "你正在使用移动网络观看"
|
|
||||||
alert.config.customTextLabel { label in
|
alert.config.customTextLabel { label in
|
||||||
label.font = .systemFont(ofSize: 15)
|
label.font = .systemFont(ofSize: 15)
|
||||||
}
|
}
|
||||||
alert.config.customButton { button in
|
alert.config.customButton { button in
|
||||||
button.titleLabel?.font = .systemFont(ofSize: 15)
|
button.titleLabel?.font = .systemFont(ofSize: 15)
|
||||||
}
|
}
|
||||||
|
alert.vm.title = "你正在使用移动网络观看"
|
||||||
} .onViewDidLoad { vc in
|
} .onViewDidLoad { vc in
|
||||||
guard let alert = vc as? Alert else {
|
guard let alert = vc as? Alert else {
|
||||||
return
|
return
|
||||||
|
@ -97,14 +100,17 @@ class AlertVC: ListVC {
|
||||||
}
|
}
|
||||||
section.add(title: "只有一段文字 + 无背景色按钮") {
|
section.add(title: "只有一段文字 + 无背景色按钮") {
|
||||||
Alert { alert in
|
Alert { alert in
|
||||||
|
alert.config.cardMinWidth = 270
|
||||||
|
alert.config.cardEdgeInsets = .init(top: 0, left: 0, bottom: 16, right: 0)
|
||||||
|
alert.config.textEdgeInsets = .init(top: 30, left: 32, bottom: 30, right: 32)
|
||||||
alert.config.cardCornerRadius = 12
|
alert.config.cardCornerRadius = 12
|
||||||
alert.vm.title = "为了维护社区氛围,上麦用户需进行主播认证"
|
alert.config.customTextLabel { label in
|
||||||
alert.config.customBodyLabel { label in
|
|
||||||
label.font = .systemFont(ofSize: 15)
|
label.font = .systemFont(ofSize: 15)
|
||||||
}
|
}
|
||||||
alert.config.customButton { button in
|
alert.config.customButton { button in
|
||||||
button.titleLabel?.font = .systemFont(ofSize: 15)
|
button.titleLabel?.font = .systemFont(ofSize: 15)
|
||||||
}
|
}
|
||||||
|
alert.vm.message = "为了维护社区氛围,上麦用户需进行主播认证"
|
||||||
} .onViewDidLoad { vc in
|
} .onViewDidLoad { vc in
|
||||||
guard let alert = vc as? Alert else {
|
guard let alert = vc as? Alert else {
|
||||||
return
|
return
|
||||||
|
@ -315,7 +321,7 @@ class AlertVC: ListVC {
|
||||||
slider.maximumValue = 100
|
slider.maximumValue = 100
|
||||||
slider.value = 50
|
slider.value = 50
|
||||||
alert.add(action: slider)
|
alert.add(action: slider)
|
||||||
alert.add(actionSpacing: 124)
|
alert.add(actionSpacing: 24)
|
||||||
alert.add(action: "取消", style: .gray)
|
alert.add(action: "取消", style: .gray)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -81,7 +81,6 @@ class SheetVC: ListVC {
|
||||||
toast.vm.message = "点击背景将不会dismiss,必须在下方做出选择才能关掉"
|
toast.vm.message = "点击背景将不会dismiss,必须在下方做出选择才能关掉"
|
||||||
toast.vm.duration = 2
|
toast.vm.duration = 2
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -112,13 +111,13 @@ class SheetVC: ListVC {
|
||||||
let s2 = UISlider()
|
let s2 = UISlider()
|
||||||
s2.minimumValue = 0
|
s2.minimumValue = 0
|
||||||
s2.maximumValue = 40
|
s2.maximumValue = 40
|
||||||
s2.value = Float(sheet.config.edgeInset)
|
s2.value = Float(sheet.config.screenEdgeInset)
|
||||||
sheet.add(subview: s2).snp.makeConstraints { make in
|
sheet.add(subview: s2).snp.makeConstraints { make in
|
||||||
make.height.equalTo(50)
|
make.height.equalTo(50)
|
||||||
}
|
}
|
||||||
if #available(iOS 14.0, *) {
|
if #available(iOS 14.0, *) {
|
||||||
s2.addAction(.init(handler: { [unowned s2] act in
|
s2.addAction(.init(handler: { [unowned s2] act in
|
||||||
sheet.config.edgeInset = CGFloat(s2.value)
|
sheet.config.screenEdgeInset = CGFloat(s2.value)
|
||||||
sheet.reloadData()
|
sheet.reloadData()
|
||||||
}), for: .valueChanged)
|
}), for: .valueChanged)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -155,7 +155,9 @@ class ToastVC: ListVC {
|
||||||
list.add(title: "实例管理") { section in
|
list.add(title: "实例管理") { section in
|
||||||
var i = 0
|
var i = 0
|
||||||
section.add(title: "多实例共存") {
|
section.add(title: "多实例共存") {
|
||||||
Toast(.loading.title("多实例共存").message("直接创建的实例,以平铺方式排列").duration(2)).push()
|
Toast(.loading.title("多实例共存").message("直接创建的实例,以平铺方式排列").duration(2)) { toast in
|
||||||
|
toast.config.cardEdgeInsets = .init(top: 32, left: 20, bottom: 32, right: 20)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
section.add(title: "不存在就创建,存在就更新") {
|
section.add(title: "不存在就创建,存在就更新") {
|
||||||
i += 1
|
i += 1
|
||||||
|
@ -244,12 +246,12 @@ class ToastVC: ListVC {
|
||||||
lb.textAlignment = .center
|
lb.textAlignment = .center
|
||||||
stack.addArrangedSubview(lb)
|
stack.addArrangedSubview(lb)
|
||||||
|
|
||||||
let btn1 = ProHUD.Button(config: toast.config, action: .init(style: .gray, title: "取消"))
|
let btn1 = ProHUD.ToastButton(config: toast.config, action: .init(style: .gray, title: "取消"))
|
||||||
btn1.onTouchUpInside { action in
|
btn1.onTouchUpInside { action in
|
||||||
print("点击了取消")
|
print("点击了取消")
|
||||||
testAlert()
|
testAlert()
|
||||||
}
|
}
|
||||||
let btn2 = ProHUD.Button(config: toast.config, action: .init(style: .tinted, title: "确定"))
|
let btn2 = ProHUD.ToastButton(config: toast.config, action: .init(style: .tinted, title: "确定"))
|
||||||
btn2.onTouchUp { action in
|
btn2.onTouchUp { action in
|
||||||
print("点击了确定")
|
print("点击了确定")
|
||||||
testAlert()
|
testAlert()
|
||||||
|
|
|
@ -20,16 +20,18 @@ public class Alert: ProHUD.Controller {
|
||||||
/// 内容容器(包括icon、textStack、actionStack)
|
/// 内容容器(包括icon、textStack、actionStack)
|
||||||
public lazy var contentStack: StackView = {
|
public lazy var contentStack: StackView = {
|
||||||
let stack = StackView(axis: .vertical)
|
let stack = StackView(axis: .vertical)
|
||||||
stack.spacing = config.margin
|
stack.spacing = 24
|
||||||
stack.alignment = .center
|
stack.alignment = .center
|
||||||
|
config.customContentStack?(stack)
|
||||||
return stack
|
return stack
|
||||||
}()
|
}()
|
||||||
|
|
||||||
/// 文本区容器
|
/// 文本区容器
|
||||||
public lazy var textStack: StackView = {
|
public lazy var textStack: StackView = {
|
||||||
let stack = StackView(axis: .vertical)
|
let stack = StackView(axis: .vertical)
|
||||||
stack.spacing = config.margin
|
stack.spacing = 8
|
||||||
stack.alignment = .center
|
stack.alignment = .center
|
||||||
|
config.customTextStack?(stack)
|
||||||
return stack
|
return stack
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
@ -66,7 +68,8 @@ public class Alert: ProHUD.Controller {
|
||||||
let stack = StackView()
|
let stack = StackView()
|
||||||
stack.alignment = .fill
|
stack.alignment = .fill
|
||||||
stack.distribution = .fillEqually
|
stack.distribution = .fillEqually
|
||||||
stack.spacing = config.margin
|
stack.spacing = 8
|
||||||
|
config.customActionStack?(stack)
|
||||||
return stack
|
return stack
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@ public class AlertButton: Button {
|
||||||
|
|
||||||
public override func update(config: ProHUD.Configuration, action: Action) {
|
public override func update(config: ProHUD.Configuration, action: Action) {
|
||||||
titleLabel?.font = .boldSystemFont(ofSize: 15)
|
titleLabel?.font = .boldSystemFont(ofSize: 15)
|
||||||
|
layer.cornerRadiusWithContinuous = 8
|
||||||
super.update(config: config, action: action)
|
super.update(config: config, action: action)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,7 @@ public extension Alert {
|
||||||
|
|
||||||
class Configuration: ProHUD.Configuration {
|
class Configuration: ProHUD.Configuration {
|
||||||
|
|
||||||
|
|
||||||
/// 指定排列方向(默认两个按钮水平排列,超过时垂直排列)
|
/// 指定排列方向(默认两个按钮水平排列,超过时垂直排列)
|
||||||
public var actionAxis: NSLayoutConstraint.Axis?
|
public var actionAxis: NSLayoutConstraint.Axis?
|
||||||
|
|
||||||
|
|
|
@ -84,17 +84,19 @@ extension Alert: DefaultLayout {
|
||||||
contentView.snp.remakeConstraints { make in
|
contentView.snp.remakeConstraints { make in
|
||||||
make.center.equalToSuperview()
|
make.center.equalToSuperview()
|
||||||
if customView == nil {
|
if customView == nil {
|
||||||
make.width.greaterThanOrEqualTo(config.cardMinWidth).priority(.low)
|
make.width.greaterThanOrEqualTo(config.cardMinWidth)
|
||||||
make.width.lessThanOrEqualTo(config.cardMaxWidthByDefault)
|
make.width.lessThanOrEqualTo(config.cardMaxWidthByDefault)
|
||||||
make.height.greaterThanOrEqualTo(config.cardMinHeight).priority(.low)
|
make.height.greaterThanOrEqualTo(config.cardMinHeight)
|
||||||
make.height.lessThanOrEqualTo(config.cardMaxHeightByDefault)
|
make.height.lessThanOrEqualTo(config.cardMaxHeightByDefault)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if contentStack.superview == nil {
|
if contentStack.superview == nil {
|
||||||
contentView.addSubview(contentStack)
|
contentView.addSubview(contentStack)
|
||||||
contentStack.spacing = config.margin + config.padding
|
|
||||||
contentStack.snp.remakeConstraints { make in
|
contentStack.snp.remakeConstraints { make in
|
||||||
make.edges.equalTo(contentView).inset(config.padding)
|
make.top.equalToSuperview().inset(config.cardEdgeInsets.top)
|
||||||
|
make.left.equalToSuperview().inset(config.cardEdgeInsets.left)
|
||||||
|
make.bottom.equalToSuperview().inset(config.cardEdgeInsets.bottom)
|
||||||
|
make.right.equalToSuperview().inset(config.cardEdgeInsets.right)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// card background
|
// card background
|
||||||
|
@ -144,8 +146,8 @@ extension Alert {
|
||||||
if imageView.superview == nil {
|
if imageView.superview == nil {
|
||||||
contentStack.insertArrangedSubview(imageView, at: 0)
|
contentStack.insertArrangedSubview(imageView, at: 0)
|
||||||
imageView.snp.remakeConstraints { (mk) in
|
imageView.snp.remakeConstraints { (mk) in
|
||||||
mk.top.left.greaterThanOrEqualTo(contentView).inset(config.padding*2.25)
|
mk.top.left.greaterThanOrEqualTo(contentView).inset(config.cardEdgeInsets.top * 2)
|
||||||
mk.right.bottom.lessThanOrEqualTo(contentView).inset(config.padding*2.25)
|
mk.right.bottom.lessThanOrEqualTo(contentView).inset(config.cardEdgeInsets.right * 2)
|
||||||
mk.width.equalTo(config.iconSize.width)
|
mk.width.equalTo(config.iconSize.width)
|
||||||
mk.height.equalTo(config.iconSize.height)
|
mk.height.equalTo(config.iconSize.height)
|
||||||
}
|
}
|
||||||
|
@ -172,15 +174,10 @@ extension Alert {
|
||||||
contentStack.insertArrangedSubview(textStack, at: 0)
|
contentStack.insertArrangedSubview(textStack, at: 0)
|
||||||
}
|
}
|
||||||
textStack.snp.remakeConstraints { (mk) in
|
textStack.snp.remakeConstraints { (mk) in
|
||||||
mk.top.greaterThanOrEqualTo(contentView).inset(config.padding*1.875)
|
mk.left.greaterThanOrEqualToSuperview().inset(config.textEdgeInsets.left)
|
||||||
mk.bottom.lessThanOrEqualTo(contentView).inset(config.padding*1.875)
|
mk.right.lessThanOrEqualToSuperview().inset(config.textEdgeInsets.right)
|
||||||
if UIScreen.main.bounds.width < 414 {
|
mk.top.greaterThanOrEqualTo(contentView).inset(config.cardEdgeInsets.top + config.textEdgeInsets.top)
|
||||||
mk.left.greaterThanOrEqualTo(contentView).inset(config.padding*2)
|
mk.bottom.lessThanOrEqualTo(contentView).inset(config.cardEdgeInsets.bottom + config.textEdgeInsets.bottom)
|
||||||
mk.right.lessThanOrEqualTo(contentView).inset(config.padding*2)
|
|
||||||
} else {
|
|
||||||
mk.left.greaterThanOrEqualTo(contentView).inset(config.padding*3)
|
|
||||||
mk.right.lessThanOrEqualTo(contentView).inset(config.padding*3)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if titleCount > 0 {
|
if titleCount > 0 {
|
||||||
|
|
|
@ -73,7 +73,7 @@ public extension Alert {
|
||||||
static func lazyPush(identifier: String? = nil, file: String = #file, line: Int = #line, handler: @escaping (_ alert: Alert) -> Void, onExists: ((_ alert: Alert) -> Void)? = nil) {
|
static func lazyPush(identifier: String? = nil, file: String = #file, line: Int = #line, handler: @escaping (_ alert: Alert) -> Void, onExists: ((_ alert: Alert) -> Void)? = nil) {
|
||||||
let id = identifier ?? (file + "#\(line)")
|
let id = identifier ?? (file + "#\(line)")
|
||||||
if let vc = AlertWindow.alerts.last(where: { $0.identifier == id }) {
|
if let vc = AlertWindow.alerts.last(where: { $0.identifier == id }) {
|
||||||
onExists?(vc)
|
vc.update(handler: onExists ?? handler)
|
||||||
} else {
|
} else {
|
||||||
Alert { alert in
|
Alert { alert in
|
||||||
alert.identifier = id
|
alert.identifier = id
|
||||||
|
|
|
@ -90,18 +90,40 @@ public class Configuration: NSObject {
|
||||||
/// 最小高度
|
/// 最小高度
|
||||||
public var cardMinHeight = CGFloat(32)
|
public var cardMinHeight = CGFloat(32)
|
||||||
|
|
||||||
|
/// 卡片内边距
|
||||||
|
public var cardEdgeInsets: UIEdgeInsets = {
|
||||||
|
.init(top: 16, left: 16, bottom: 16, right: 16)
|
||||||
|
}()
|
||||||
|
|
||||||
|
/// 文字区域内边距
|
||||||
|
public var textEdgeInsets: UIEdgeInsets = {
|
||||||
|
.init(top: 16, left: 16, bottom: 16, right: 16)
|
||||||
|
}()
|
||||||
|
|
||||||
/// 卡片圆角
|
/// 卡片圆角
|
||||||
public var cardCornerRadius: CGFloat?
|
public var cardCornerRadius: CGFloat?
|
||||||
var cardCornerRadiusByDefault: CGFloat { cardCornerRadius ?? 16 }
|
var cardCornerRadiusByDefault: CGFloat { cardCornerRadius ?? 16 }
|
||||||
|
|
||||||
/// 余量:元素与元素之间的距离
|
|
||||||
public var margin = CGFloat(8)
|
|
||||||
/// 填充:元素内部控件距离元素边界的距离
|
/// 填充:元素内部控件距离元素边界的距离
|
||||||
public var padding = CGFloat(16)
|
public var padding = CGFloat(16)
|
||||||
|
|
||||||
/// 颜色
|
/// 颜色
|
||||||
public var tintColor: UIColor?
|
public var tintColor: UIColor?
|
||||||
|
|
||||||
|
|
||||||
|
var customContentStack: ((_ stack: StackView) -> Void)?
|
||||||
|
public func customContentStack(handler: @escaping (_ stack: StackView) -> Void) {
|
||||||
|
customContentStack = handler
|
||||||
|
}
|
||||||
|
var customTextStack: ((_ stack: StackView) -> Void)?
|
||||||
|
public func customTextStack(handler: @escaping (_ stack: StackView) -> Void) {
|
||||||
|
customTextStack = handler
|
||||||
|
}
|
||||||
|
var customActionStack: ((_ stack: StackView) -> Void)?
|
||||||
|
public func customActionStack(handler: @escaping (_ stack: StackView) -> Void) {
|
||||||
|
customActionStack = handler
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: 图标样式
|
// MARK: 图标样式
|
||||||
/// 图标尺寸
|
/// 图标尺寸
|
||||||
public var iconSize = CGSize(width: 44, height: 44)
|
public var iconSize = CGSize(width: 44, height: 44)
|
||||||
|
@ -144,10 +166,6 @@ public class Configuration: NSObject {
|
||||||
customButton = handler
|
customButton = handler
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 按钮圆角
|
|
||||||
public var buttonCornerRadius: CGFloat?
|
|
||||||
var buttonCornerRadiusByDefault: CGFloat { buttonCornerRadius ?? 8 }
|
|
||||||
|
|
||||||
// MARK: 动画
|
// MARK: 动画
|
||||||
|
|
||||||
public var animateDurationForBuildIn: TimeInterval?
|
public var animateDurationForBuildIn: TimeInterval?
|
||||||
|
|
|
@ -31,13 +31,13 @@ open class Button: UIButton {
|
||||||
|
|
||||||
public convenience init(config: ProHUD.Configuration, action: Action) {
|
public convenience init(config: ProHUD.Configuration, action: Action) {
|
||||||
self.init(frame: .zero)
|
self.init(frame: .zero)
|
||||||
|
layer.cornerRadiusWithContinuous = 8
|
||||||
self.update(config: config, action: action)
|
self.update(config: config, action: action)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 更新按钮
|
/// 更新按钮
|
||||||
/// - Parameter style: 样式
|
/// - Parameter style: 样式
|
||||||
open func update(config: ProHUD.Configuration, action: Action) {
|
open func update(config: ProHUD.Configuration, action: Action) {
|
||||||
layer.cornerRadiusWithContinuous = config.buttonCornerRadiusByDefault
|
|
||||||
self.action = action
|
self.action = action
|
||||||
setTitle(action.title, for: .normal)
|
setTitle(action.title, for: .normal)
|
||||||
switch action.style {
|
switch action.style {
|
||||||
|
|
|
@ -27,8 +27,9 @@ public class Sheet: Controller {
|
||||||
/// 内容容器(包括icon、textStack、actionStack)
|
/// 内容容器(包括icon、textStack、actionStack)
|
||||||
public lazy var contentStack: StackView = {
|
public lazy var contentStack: StackView = {
|
||||||
let stack = StackView()
|
let stack = StackView()
|
||||||
stack.spacing = config.margin
|
stack.spacing = 8
|
||||||
stack.alignment = .fill
|
stack.alignment = .fill
|
||||||
|
config.customContentStack?(stack)
|
||||||
return stack
|
return stack
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@ public class SheetButton: Button {
|
||||||
|
|
||||||
public override func update(config: ProHUD.Configuration, action: Action) {
|
public override func update(config: ProHUD.Configuration, action: Action) {
|
||||||
titleLabel?.font = .boldSystemFont(ofSize: 18)
|
titleLabel?.font = .boldSystemFont(ofSize: 18)
|
||||||
|
layer.cornerRadiusWithContinuous = 12
|
||||||
super.update(config: config, action: action)
|
super.update(config: config, action: action)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ public extension Sheet {
|
||||||
class Configuration: ProHUD.Configuration {
|
class Configuration: ProHUD.Configuration {
|
||||||
|
|
||||||
/// 卡片距离屏幕的间距
|
/// 卡片距离屏幕的间距
|
||||||
public var edgeInset: CGFloat = 4
|
public var screenEdgeInset: CGFloat = 4
|
||||||
|
|
||||||
/// 是否是全屏的页面
|
/// 是否是全屏的页面
|
||||||
public var isFullScreen = false
|
public var isFullScreen = false
|
||||||
|
@ -45,8 +45,6 @@ public extension Sheet {
|
||||||
|
|
||||||
override var cardMaxHeightByDefault: CGFloat { cardMaxHeight ?? (UIScreen.main.bounds.height - 50) }
|
override var cardMaxHeightByDefault: CGFloat { cardMaxHeight ?? (UIScreen.main.bounds.height - 50) }
|
||||||
|
|
||||||
override var buttonCornerRadiusByDefault: CGFloat { buttonCornerRadius ?? 12 }
|
|
||||||
|
|
||||||
override var animateDurationForBuildInByDefault: CGFloat {
|
override var animateDurationForBuildInByDefault: CGFloat {
|
||||||
animateDurationForBuildIn ?? 0.5
|
animateDurationForBuildIn ?? 0.5
|
||||||
}
|
}
|
||||||
|
|
|
@ -153,10 +153,10 @@ public extension Sheet {
|
||||||
if #available(iOS 11.0, *) {
|
if #available(iOS 11.0, *) {
|
||||||
let count = contentStack.arrangedSubviews.count
|
let count = contentStack.arrangedSubviews.count
|
||||||
if count > 0 {
|
if count > 0 {
|
||||||
contentStack.setCustomSpacing(config.margin * 1.5, after: contentStack.arrangedSubviews[count-1])
|
contentStack.setCustomSpacing(12, after: contentStack.arrangedSubviews[count-1])
|
||||||
}
|
}
|
||||||
if count > 1 {
|
if count > 1 {
|
||||||
contentStack.setCustomSpacing(config.margin * 2, after: contentStack.arrangedSubviews[count-2])
|
contentStack.setCustomSpacing(16, after: contentStack.arrangedSubviews[count-2])
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Fallback on earlier versions
|
// Fallback on earlier versions
|
||||||
|
|
|
@ -44,7 +44,7 @@ extension Sheet: DefaultLayout {
|
||||||
loadContentMaskViewIfNeeded()
|
loadContentMaskViewIfNeeded()
|
||||||
// layout
|
// layout
|
||||||
let maxWidth = config.cardMaxWidthByDefault
|
let maxWidth = config.cardMaxWidthByDefault
|
||||||
var width = UIScreen.main.bounds.width - config.edgeInset * 2
|
var width = UIScreen.main.bounds.width - config.screenEdgeInset * 2
|
||||||
if width > maxWidth {
|
if width > maxWidth {
|
||||||
// landscape iPhone or iPad
|
// landscape iPhone or iPad
|
||||||
width = maxWidth
|
width = maxWidth
|
||||||
|
@ -56,7 +56,7 @@ extension Sheet: DefaultLayout {
|
||||||
make.centerX.equalToSuperview()
|
make.centerX.equalToSuperview()
|
||||||
if UIDevice.current.userInterfaceIdiom == .phone {
|
if UIDevice.current.userInterfaceIdiom == .phone {
|
||||||
if width < maxWidth {
|
if width < maxWidth {
|
||||||
make.bottom.equalToSuperview().inset(config.edgeInset)
|
make.bottom.equalToSuperview().inset(config.screenEdgeInset)
|
||||||
} else {
|
} else {
|
||||||
make.bottom.equalToSuperview().inset(screenSafeAreaInsets.bottom)
|
make.bottom.equalToSuperview().inset(screenSafeAreaInsets.bottom)
|
||||||
}
|
}
|
||||||
|
@ -78,16 +78,20 @@ extension Sheet: DefaultLayout {
|
||||||
if contentStack.superview == nil {
|
if contentStack.superview == nil {
|
||||||
contentView.addSubview(contentStack)
|
contentView.addSubview(contentStack)
|
||||||
contentStack.snp.remakeConstraints { make in
|
contentStack.snp.remakeConstraints { make in
|
||||||
|
let safeArea: UIEdgeInsets
|
||||||
if config.isFullScreen {
|
if config.isFullScreen {
|
||||||
make.top.equalToSuperview().offset(screenSafeAreaInsets.top)
|
safeArea = screenSafeAreaInsets
|
||||||
} else {
|
} else {
|
||||||
make.top.equalToSuperview().offset(config.padding * 2)
|
safeArea = .zero
|
||||||
}
|
}
|
||||||
make.bottom.equalToSuperview().inset(config.padding * 2)
|
make.top.equalToSuperview().offset(safeArea.top + config.cardEdgeInsets.top)
|
||||||
|
make.bottom.equalToSuperview().inset(safeArea.bottom + config.cardEdgeInsets.bottom)
|
||||||
if isPortrait {
|
if isPortrait {
|
||||||
make.left.right.equalToSuperview().inset(config.padding)
|
make.left.equalToSuperview().inset(safeArea.left + config.cardEdgeInsets.left)
|
||||||
|
make.right.equalToSuperview().inset(safeArea.right + config.cardEdgeInsets.right)
|
||||||
} else {
|
} else {
|
||||||
make.left.right.equalToSuperview().inset(config.padding * 2)
|
make.left.equalToSuperview().inset(safeArea.left + config.cardEdgeInsets.left * 2)
|
||||||
|
make.right.equalToSuperview().inset(safeArea.right + config.cardEdgeInsets.right * 2)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,7 @@ public extension Sheet {
|
||||||
static func lazyPush(identifier: String? = nil, file: String = #file, line: Int = #line, handler: @escaping (_ sheet: Sheet) -> Void, onExists: ((_ sheet: Sheet) -> Void)? = nil) {
|
static func lazyPush(identifier: String? = nil, file: String = #file, line: Int = #line, handler: @escaping (_ sheet: Sheet) -> Void, onExists: ((_ sheet: Sheet) -> Void)? = nil) {
|
||||||
let id = identifier ?? (file + "#\(line)")
|
let id = identifier ?? (file + "#\(line)")
|
||||||
if let vc = find(identifier: id).last {
|
if let vc = find(identifier: id).last {
|
||||||
onExists?(vc)
|
vc.update(handler: onExists ?? handler)
|
||||||
} else {
|
} else {
|
||||||
Sheet { sheet in
|
Sheet { sheet in
|
||||||
sheet.identifier = id
|
sheet.identifier = id
|
||||||
|
@ -86,7 +86,7 @@ extension Sheet {
|
||||||
|
|
||||||
func _translateOut() {
|
func _translateOut() {
|
||||||
backgroundView.alpha = 0
|
backgroundView.alpha = 0
|
||||||
contentView.transform = .init(translationX: 0, y: view.frame.size.height - contentView.frame.minY + config.margin)
|
contentView.transform = .init(translationX: 0, y: view.frame.size.height - contentView.frame.minY + config.screenEdgeInset)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,15 +22,17 @@ public class Toast: Controller {
|
||||||
/// 内容容器(包括icon、textStack、actionStack)
|
/// 内容容器(包括icon、textStack、actionStack)
|
||||||
public lazy var contentStack: StackView = {
|
public lazy var contentStack: StackView = {
|
||||||
let stack = StackView(axis: .vertical)
|
let stack = StackView(axis: .vertical)
|
||||||
stack.spacing = config.margin * 2
|
stack.spacing = 16
|
||||||
|
config.customContentStack?(stack)
|
||||||
return stack
|
return stack
|
||||||
}()
|
}()
|
||||||
|
|
||||||
/// 信息容器(image+text)
|
/// 信息容器(image+text)
|
||||||
public lazy var infoStack: StackView = {
|
public lazy var infoStack: StackView = {
|
||||||
let stack = StackView(axis: .horizontal)
|
let stack = StackView(axis: .horizontal)
|
||||||
stack.spacing = config.margin
|
stack.spacing = 8
|
||||||
stack.alignment = .top
|
stack.alignment = .top
|
||||||
|
config.customInfoStack?(stack)
|
||||||
return stack
|
return stack
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
@ -39,13 +41,15 @@ public class Toast: Controller {
|
||||||
let stack = StackView(axis: .vertical)
|
let stack = StackView(axis: .vertical)
|
||||||
stack.spacing = config.lineSpace
|
stack.spacing = config.lineSpace
|
||||||
stack.distribution = .equalSpacing
|
stack.distribution = .equalSpacing
|
||||||
|
config.customTextStack?(stack)
|
||||||
return stack
|
return stack
|
||||||
}()
|
}()
|
||||||
|
|
||||||
/// 按钮容器
|
/// 按钮容器
|
||||||
public lazy var actionStack: StackView = {
|
public lazy var actionStack: StackView = {
|
||||||
let stack = StackView(axis: .horizontal)
|
let stack = StackView(axis: .horizontal)
|
||||||
stack.spacing = config.margin
|
stack.spacing = 8
|
||||||
|
config.customActionStack?(stack)
|
||||||
return stack
|
return stack
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@ public class ToastButton: Button {
|
||||||
|
|
||||||
public override func update(config: ProHUD.Configuration, action: Action) {
|
public override func update(config: ProHUD.Configuration, action: Action) {
|
||||||
titleLabel?.font = .boldSystemFont(ofSize: 15)
|
titleLabel?.font = .boldSystemFont(ofSize: 15)
|
||||||
|
layer.cornerRadiusWithContinuous = 8
|
||||||
super.update(config: config, action: action)
|
super.update(config: config, action: action)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,13 @@ public extension Toast {
|
||||||
|
|
||||||
class Configuration: ProHUD.Configuration {
|
class Configuration: ProHUD.Configuration {
|
||||||
|
|
||||||
|
/// 元素与元素之间的距离
|
||||||
|
public var margin = CGFloat(8)
|
||||||
|
|
||||||
|
var customInfoStack: ((_ stack: StackView) -> Void)?
|
||||||
|
public func customInfoStack(handler: @escaping (_ stack: StackView) -> Void) {
|
||||||
|
customInfoStack = handler
|
||||||
|
}
|
||||||
/// 行间距
|
/// 行间距
|
||||||
public var lineSpace = CGFloat(4)
|
public var lineSpace = CGFloat(4)
|
||||||
|
|
||||||
|
|
|
@ -96,8 +96,11 @@ extension Toast: DefaultLayout {
|
||||||
// stacks
|
// stacks
|
||||||
if contentStack.superview != contentView {
|
if contentStack.superview != contentView {
|
||||||
contentView.addSubview(contentStack)
|
contentView.addSubview(contentStack)
|
||||||
contentStack.snp.remakeConstraints { (mk) in
|
contentStack.snp.remakeConstraints { make in
|
||||||
mk.edges.equalToSuperview().inset(config.padding)
|
make.top.equalToSuperview().inset(config.cardEdgeInsets.top)
|
||||||
|
make.left.equalToSuperview().inset(config.cardEdgeInsets.left)
|
||||||
|
make.bottom.equalToSuperview().inset(config.cardEdgeInsets.bottom)
|
||||||
|
make.right.equalToSuperview().inset(config.cardEdgeInsets.right)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
contentStack.insertArrangedSubview(infoStack, at: 0)
|
contentStack.insertArrangedSubview(infoStack, at: 0)
|
||||||
|
|
|
@ -28,7 +28,7 @@ public extension Toast {
|
||||||
static func lazyPush(identifier: String? = nil, file: String = #file, line: Int = #line, handler: @escaping (_ toast: Toast) -> Void, onExists: ((_ toast: Toast) -> Void)? = nil) {
|
static func lazyPush(identifier: String? = nil, file: String = #file, line: Int = #line, handler: @escaping (_ toast: Toast) -> Void, onExists: ((_ toast: Toast) -> Void)? = nil) {
|
||||||
let id = identifier ?? (file + "#\(line)")
|
let id = identifier ?? (file + "#\(line)")
|
||||||
if let vc = find(identifier: id).last {
|
if let vc = find(identifier: id).last {
|
||||||
onExists?(vc)
|
vc.update(handler: onExists ?? handler)
|
||||||
} else {
|
} else {
|
||||||
Toast { toast in
|
Toast { toast in
|
||||||
toast.identifier = id
|
toast.identifier = id
|
||||||
|
|
|
@ -50,7 +50,7 @@ class ToastWindow: Window {
|
||||||
}
|
}
|
||||||
let config = toast.config
|
let config = toast.config
|
||||||
// frame
|
// frame
|
||||||
let width = CGFloat.minimum(UIScreen.main.bounds.width - 2*config.margin, config.cardMaxWidthByDefault)
|
let width = CGFloat.minimum(UIScreen.main.bounds.width - config.cardEdgeInsets.left - config.cardEdgeInsets.right, config.cardMaxWidthByDefault)
|
||||||
toast.view.frame.size = CGSize(width: width, height: config.cardMaxHeightByDefault)
|
toast.view.frame.size = CGSize(width: width, height: config.cardMaxHeightByDefault)
|
||||||
toast.titleLabel.sizeToFit()
|
toast.titleLabel.sizeToFit()
|
||||||
toast.bodyLabel.sizeToFit()
|
toast.bodyLabel.sizeToFit()
|
||||||
|
@ -164,7 +164,7 @@ fileprivate extension Toast {
|
||||||
height = CGFloat.maximum(v.frame.maxY, height)
|
height = CGFloat.maximum(v.frame.maxY, height)
|
||||||
}
|
}
|
||||||
// 上下边间距
|
// 上下边间距
|
||||||
height += 2 * config.padding
|
height += config.cardEdgeInsets.top + config.cardEdgeInsets.bottom
|
||||||
return height
|
return height
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue