ProHUD/Source/HUDView.swift

183 lines
6.3 KiB
Swift
Raw Normal View History

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 10:14:47 +08:00
import Inspire
2019-07-31 17:40:39 +08:00
public extension ProHUD {
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-16 16:50:53 +08:00
///
enum RotateDirection: Double {
case clockwise = 1
case counterclockwise = -1
}
}
internal extension String {
static let rotateKey = "rotationAnimation"
2019-08-12 15:02:36 +08:00
}
internal extension Alert {
class Button: UIButton {
class func actionButton(title: String?) -> Button {
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
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
}
2019-08-12 15:02:36 +08:00
class func forceQuitButton() -> UIButton {
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
}
internal extension Guard {
2019-07-31 17:40:39 +08:00
2019-08-12 15:02:36 +08:00
class Button: UIButton {
class func actionButton(title: String?) -> Button {
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
}
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)
}
}
extension CALayer {
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
}
ani.repeatDuration = .infinity
add(ani, forKey: .rotateKey)
2020-06-16 16:50:53 +08:00
}
}
func endRotate() {
removeAnimation(forKey: .rotateKey)
}
2019-07-31 17:40:39 +08:00
}
2020-06-22 10:14:47 +08:00
extension ProHUD {
static var safeAreaInsets: UIEdgeInsets {
return Inspire.shared.screen.updatedSafeAreaInsets
}
}