2019-07-31 17:40:39 +08:00
|
|
|
//
|
|
|
|
// HUDView.swift
|
|
|
|
// ProHUD
|
|
|
|
//
|
|
|
|
// Created by xaoxuu on 2019/7/29.
|
|
|
|
// Copyright © 2019 Titan Studio. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2020-06-22 13:27:54 +08:00
|
|
|
|
|
|
|
// MARK: - public
|
2019-07-31 17:40:39 +08:00
|
|
|
|
|
|
|
public extension ProHUD {
|
|
|
|
|
2020-06-22 13:27:54 +08:00
|
|
|
/// 堆栈视图容器
|
2019-07-31 17:40:39 +08:00
|
|
|
class StackContainer: UIStackView {
|
|
|
|
|
|
|
|
public override init(frame: CGRect) {
|
|
|
|
super.init(frame: frame)
|
2019-08-03 17:48:37 +08:00
|
|
|
spacing = cfg.alert.margin
|
2019-07-31 17:40:39 +08:00
|
|
|
distribution = .fill
|
|
|
|
alignment = .center
|
|
|
|
axis = .vertical
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
required init(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-06-22 13:27:54 +08:00
|
|
|
|
2020-06-16 16:50:53 +08:00
|
|
|
/// 旋转方向
|
|
|
|
enum RotateDirection: Double {
|
2020-06-22 13:27:54 +08:00
|
|
|
/// 顺时针
|
2020-06-16 16:50:53 +08:00
|
|
|
case clockwise = 1
|
2020-06-22 13:27:54 +08:00
|
|
|
/// 逆时针
|
2020-06-16 16:50:53 +08:00
|
|
|
case counterclockwise = -1
|
|
|
|
}
|
2020-06-19 19:13:33 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-06-22 13:27:54 +08:00
|
|
|
// MARK: - internal
|
2019-08-12 15:02:36 +08:00
|
|
|
|
2020-06-22 13:27:54 +08:00
|
|
|
// MARK: 弹窗
|
2019-08-12 15:02:36 +08:00
|
|
|
internal extension Alert {
|
2020-06-22 13:27:54 +08:00
|
|
|
|
|
|
|
/// 弹窗的按钮
|
2019-08-12 15:02:36 +08:00
|
|
|
class Button: UIButton {
|
2020-06-22 13:27:54 +08:00
|
|
|
|
|
|
|
/// 创建操作按钮
|
|
|
|
/// - Parameter title: 标题
|
|
|
|
/// - Returns: 按钮
|
|
|
|
static func createActionButton(title: String?) -> Button {
|
2019-08-12 15:02:36 +08:00
|
|
|
let btn = Button(type: .system)
|
|
|
|
btn.setTitle(title, for: .normal)
|
|
|
|
btn.layer.cornerRadius = cfg.alert.cornerRadius / 2
|
|
|
|
btn.titleLabel?.font = cfg.alert.buttonFont
|
|
|
|
return btn
|
|
|
|
}
|
2019-07-31 17:40:39 +08:00
|
|
|
|
2020-06-22 13:27:54 +08:00
|
|
|
/// 更新按钮
|
|
|
|
/// - Parameter style: 样式
|
2019-08-12 15:02:36 +08:00
|
|
|
func update(style: UIAlertAction.Style) {
|
|
|
|
let pd = CGFloat(8)
|
|
|
|
if style != .cancel {
|
|
|
|
backgroundColor = cfg.dynamicColor.withAlphaComponent(0.04)
|
|
|
|
contentEdgeInsets = .init(top: pd*1.5, left: pd*1.5, bottom: pd*1.5, right: pd*1.5)
|
2019-08-06 20:09:16 +08:00
|
|
|
} else {
|
2019-08-12 15:02:36 +08:00
|
|
|
backgroundColor = .clear
|
|
|
|
contentEdgeInsets = .init(top: pd*0.5, left: pd*1.5, bottom: pd*0.5, right: pd*1.5)
|
2019-08-03 16:22:50 +08:00
|
|
|
}
|
2019-08-12 15:02:36 +08:00
|
|
|
switch style {
|
|
|
|
case .default:
|
|
|
|
setTitleColor(tintColor, for: .normal)
|
|
|
|
case .destructive:
|
|
|
|
setTitleColor(.init(red: 244/255, green: 67/255, blue: 54/255, alpha: 1), for: .normal)
|
|
|
|
case .cancel:
|
|
|
|
setTitleColor(cfg.secondaryLabelColor, for: .normal)
|
|
|
|
@unknown default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
tag = style.rawValue
|
2019-07-31 17:40:39 +08:00
|
|
|
}
|
|
|
|
|
2020-06-22 13:27:54 +08:00
|
|
|
/// 创建隐藏按钮
|
|
|
|
/// - Returns: 按钮
|
|
|
|
static func createHideButton() -> UIButton {
|
2019-08-12 15:02:36 +08:00
|
|
|
let btn = Button(type: .system)
|
|
|
|
let pd = cfg.alert.padding/2
|
|
|
|
btn.contentEdgeInsets = .init(top: pd*1.5, left: pd*1.5, bottom: pd*1.5, right: pd*1.5)
|
|
|
|
btn.imageEdgeInsets.right = pd*1.5
|
|
|
|
btn.setTitleColor(UIColor(red:1.00, green:0.55, blue:0.21, alpha:1.00), for: .normal)
|
|
|
|
btn.titleLabel?.font = cfg.alert.buttonFont
|
|
|
|
return btn
|
2019-07-31 17:40:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-08-12 15:02:36 +08:00
|
|
|
}
|
|
|
|
|
2020-06-22 13:27:54 +08:00
|
|
|
// MARK: 操作表
|
2019-08-12 15:02:36 +08:00
|
|
|
internal extension Guard {
|
2019-07-31 17:40:39 +08:00
|
|
|
|
2020-06-22 13:27:54 +08:00
|
|
|
/// 操作表的按钮
|
2019-08-12 15:02:36 +08:00
|
|
|
class Button: UIButton {
|
2020-06-22 13:27:54 +08:00
|
|
|
|
|
|
|
/// 创建操作按钮
|
|
|
|
/// - Parameter title: 标题
|
|
|
|
/// - Returns: 按钮
|
|
|
|
static func createActionButton(title: String?) -> Button {
|
2019-08-12 15:02:36 +08:00
|
|
|
let btn = Button(type: .system)
|
|
|
|
btn.setTitle(title, for: .normal)
|
|
|
|
btn.layer.cornerRadius = cfg.guard.buttonCornerRadius
|
|
|
|
btn.titleLabel?.font = cfg.guard.buttonFont
|
|
|
|
return btn
|
|
|
|
}
|
|
|
|
|
|
|
|
func update(style: UIAlertAction.Style) {
|
|
|
|
let pd = CGFloat(8)
|
|
|
|
if style != .cancel {
|
|
|
|
contentEdgeInsets = .init(top: pd*1.5+2, left: pd*1.5, bottom: pd*1.5+2, right: pd*1.5)
|
|
|
|
} else {
|
|
|
|
contentEdgeInsets = .init(top: pd*1+2, left: pd*1.5, bottom: pd*1+2, right: pd*1.5)
|
|
|
|
}
|
|
|
|
switch style {
|
|
|
|
case .default:
|
|
|
|
backgroundColor = tintColor
|
|
|
|
setTitleColor(.white, for: .normal)
|
|
|
|
case .destructive:
|
|
|
|
backgroundColor = .init(red: 244/255, green: 67/255, blue: 54/255, alpha: 1)
|
|
|
|
setTitleColor(.white, for: .normal)
|
|
|
|
case .cancel:
|
|
|
|
backgroundColor = .clear
|
|
|
|
setTitleColor(cfg.secondaryLabelColor, for: .normal)
|
|
|
|
@unknown default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
tag = style.rawValue
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-07-31 17:40:39 +08:00
|
|
|
}
|
|
|
|
|
2020-06-22 13:27:54 +08:00
|
|
|
// MARK: - 动画
|
|
|
|
|
2019-07-31 17:40:39 +08:00
|
|
|
internal extension UIView {
|
|
|
|
|
2019-08-12 15:02:36 +08:00
|
|
|
private class func animateEaseOut(duration: TimeInterval = 1, delay: TimeInterval = 0, animations: @escaping () -> Void, completion: ((Bool) -> Void)? = nil) {
|
2019-07-31 17:40:39 +08:00
|
|
|
animate(withDuration: duration, delay: delay, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: [.allowUserInteraction, .curveEaseOut], animations: animations, completion: completion)
|
|
|
|
}
|
|
|
|
|
2019-08-02 13:55:23 +08:00
|
|
|
class func animateForAlertBuildIn(animations: @escaping () -> Void) {
|
|
|
|
animateEaseOut(duration: 0.8, delay: 0, animations: animations, completion: nil)
|
|
|
|
}
|
|
|
|
class func animateForAlertBuildOut(animations: @escaping () -> Void, completion: ((Bool) -> Void)? = nil) {
|
|
|
|
animateEaseOut(duration: 0.38, delay: 0, animations: animations, completion: completion)
|
|
|
|
}
|
2019-07-31 17:40:39 +08:00
|
|
|
|
2019-08-02 13:55:23 +08:00
|
|
|
class func animateForAlert(animations: @escaping () -> Void, completion: ((Bool) -> Void)? = nil) {
|
|
|
|
animateEaseOut(duration: 1, delay: 0, animations: animations, completion: completion)
|
2019-07-31 17:40:39 +08:00
|
|
|
}
|
2019-08-12 15:02:36 +08:00
|
|
|
class func animateForAlert(animations: @escaping () -> Void) {
|
|
|
|
animateForAlert(animations: animations, completion: nil)
|
2019-07-31 21:08:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class func animateForToast(animations: @escaping () -> Void, completion: ((Bool) -> Void)? = nil) {
|
2019-08-12 15:02:36 +08:00
|
|
|
animateEaseOut(duration: 1.2, delay: 0, animations: animations, completion: completion)
|
2019-07-31 21:08:25 +08:00
|
|
|
}
|
2019-08-12 15:02:36 +08:00
|
|
|
class func animateForToast(animations: @escaping () -> Void) {
|
|
|
|
animateForToast(animations: animations, completion: nil)
|
2019-08-03 16:22:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class func animateForGuard(animations: @escaping () -> Void, completion: ((Bool) -> Void)? = nil) {
|
|
|
|
animateEaseOut(duration: 0.6, delay: 0, animations: animations, completion: completion)
|
|
|
|
}
|
2019-08-12 15:02:36 +08:00
|
|
|
class func animateForGuard(animations: @escaping () -> Void) {
|
|
|
|
animateForGuard(animations: animations, completion: nil)
|
|
|
|
}
|
|
|
|
|
2020-06-19 19:13:33 +08:00
|
|
|
}
|
|
|
|
|
2020-06-22 13:27:54 +08:00
|
|
|
|
2020-06-19 19:13:33 +08:00
|
|
|
extension CALayer {
|
2020-06-22 13:27:54 +08:00
|
|
|
|
|
|
|
/// 开始旋转
|
|
|
|
/// - Parameters:
|
|
|
|
/// - direction: 方向
|
|
|
|
/// - speed: 速度
|
2020-06-19 19:13:33 +08:00
|
|
|
func startRotate(direction: ProHUD.RotateDirection, speed: CFTimeInterval) {
|
|
|
|
if animation(forKey: .rotateKey) == nil {
|
|
|
|
let ani = CABasicAnimation(keyPath: "transform.rotation.z")
|
|
|
|
ani.toValue = direction.rawValue * Double.pi * 2.0
|
|
|
|
if speed > 0 {
|
|
|
|
ani.duration = 2 / speed
|
2020-06-16 16:50:53 +08:00
|
|
|
}
|
2020-06-19 19:13:33 +08:00
|
|
|
ani.repeatDuration = .infinity
|
|
|
|
add(ani, forKey: .rotateKey)
|
2020-06-16 16:50:53 +08:00
|
|
|
}
|
|
|
|
}
|
2020-06-22 13:27:54 +08:00
|
|
|
|
|
|
|
/// 结束旋转
|
2020-06-19 19:13:33 +08:00
|
|
|
func endRotate() {
|
|
|
|
removeAnimation(forKey: .rotateKey)
|
|
|
|
}
|
2020-06-22 13:27:54 +08:00
|
|
|
|
2020-06-22 10:14:47 +08:00
|
|
|
}
|