2019-07-31 17:40:39 +08:00
|
|
|
//
|
|
|
|
// HUDController.swift
|
|
|
|
// ProHUD
|
|
|
|
//
|
|
|
|
// Created by xaoxuu on 2019/7/29.
|
|
|
|
// Copyright © 2019 Titan Studio. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
|
|
|
public class HUDController: UIViewController {
|
|
|
|
|
2019-08-12 20:20:07 +08:00
|
|
|
/// ID标识
|
|
|
|
public var identifier = String(Date().timeIntervalSince1970)
|
|
|
|
|
2019-08-12 17:59:40 +08:00
|
|
|
internal var willAppearCallback: (() -> Void)?
|
2019-08-12 19:02:33 +08:00
|
|
|
internal var didAppearCallback: (() -> Void)?
|
|
|
|
internal var willDisappearCallback: (() -> Void)?
|
|
|
|
internal var didDisappearCallback: (() -> Void)?
|
2020-06-19 19:13:33 +08:00
|
|
|
|
|
|
|
/// 执行动画的图层
|
2020-06-22 13:27:54 +08:00
|
|
|
var animateLayer: CALayer?
|
2020-06-19 19:13:33 +08:00
|
|
|
internal var animation: CAAnimation?
|
2019-08-12 17:59:40 +08:00
|
|
|
|
2019-08-05 14:20:52 +08:00
|
|
|
/// 按钮事件
|
2019-08-08 19:29:03 +08:00
|
|
|
internal var buttonEvents = [UIButton:() -> Void]()
|
2019-07-31 17:40:39 +08:00
|
|
|
|
|
|
|
init() {
|
|
|
|
super.init(nibName: nil, bundle: nil)
|
2019-08-03 17:48:37 +08:00
|
|
|
debug(self, "init")
|
2019-07-31 17:40:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
required public init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
deinit {
|
2019-08-08 09:25:28 +08:00
|
|
|
debug("👌", self, "deinit")
|
2019-07-31 17:40:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override public func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
|
|
|
// Do any additional setup after loading the view.
|
|
|
|
}
|
|
|
|
|
2019-08-12 17:59:40 +08:00
|
|
|
public override func viewWillAppear(_ animated: Bool) {
|
|
|
|
super.viewWillAppear(animated)
|
|
|
|
willAppearCallback?()
|
|
|
|
}
|
2019-08-12 19:02:33 +08:00
|
|
|
public override func viewDidAppear(_ animated: Bool) {
|
|
|
|
super.viewDidAppear(animated)
|
|
|
|
didAppearCallback?()
|
|
|
|
}
|
2019-08-05 14:20:52 +08:00
|
|
|
public override func viewDidDisappear(_ animated: Bool) {
|
|
|
|
super.viewDidDisappear(animated)
|
2019-08-12 19:02:33 +08:00
|
|
|
didDisappearCallback?()
|
|
|
|
}
|
|
|
|
|
|
|
|
public func viewWillAppear(_ callback: (() -> Void)?) {
|
|
|
|
willAppearCallback = callback
|
|
|
|
}
|
|
|
|
public func viewDidAppear(_ callback: (() -> Void)?) {
|
|
|
|
didAppearCallback = callback
|
|
|
|
}
|
|
|
|
|
|
|
|
public func viewWillDisappear(_ callback: (() -> Void)?) {
|
|
|
|
willDisappearCallback = callback
|
|
|
|
}
|
|
|
|
public func viewDidDisappear(_ callback: (() -> Void)?) {
|
|
|
|
didDisappearCallback = callback
|
2019-08-05 14:20:52 +08:00
|
|
|
}
|
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 HUDController {
|
|
|
|
|
|
|
|
func addTouchUpAction(for button: UIButton, action: @escaping () -> Void) {
|
|
|
|
button.addTarget(self, action: #selector(didTappedButton(_:)), for: .touchUpInside)
|
|
|
|
buttonEvents[button] = action
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func didTappedButton(_ sender: UIButton) {
|
|
|
|
if let ac = buttonEvents[sender] {
|
|
|
|
ac()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-06-19 19:13:33 +08:00
|
|
|
|
2020-06-22 13:27:54 +08:00
|
|
|
// MARK: - 动画
|
2020-06-19 19:13:33 +08:00
|
|
|
|
2020-06-22 13:27:54 +08:00
|
|
|
/// 图片动画
|
|
|
|
public protocol LoadingAnimationView: HUDController {
|
2020-06-19 19:13:33 +08:00
|
|
|
var imageView: UIImageView { get }
|
|
|
|
}
|
|
|
|
|
2020-06-22 13:27:54 +08:00
|
|
|
/// 旋转动画
|
|
|
|
public protocol LoadingRotateAnimation: LoadingAnimationView {}
|
|
|
|
public extension LoadingRotateAnimation {
|
|
|
|
|
|
|
|
/// 旋转动画
|
|
|
|
/// - Parameters:
|
|
|
|
/// - layer: 图层
|
|
|
|
/// - direction: 方向
|
|
|
|
/// - speed: 速度
|
|
|
|
func startRotate(_ layer: CALayer? = nil, direction: ProHUD.RotateDirection = .clockwise, speed: CFTimeInterval = 2) {
|
2020-06-19 19:13:33 +08:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
let l = layer ?? self.imageView.layer
|
|
|
|
self.animateLayer = l
|
|
|
|
l.startRotate(direction: direction, speed: speed)
|
2020-06-22 13:27:54 +08:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(self.didEnterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(self.willEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
|
2020-06-19 19:13:33 +08:00
|
|
|
}
|
|
|
|
}
|
2020-06-22 13:27:54 +08:00
|
|
|
|
|
|
|
/// 停止旋转
|
|
|
|
/// - Parameter layer: 图层
|
|
|
|
func endRotate(_ layer: CALayer? = nil) {
|
2020-06-19 19:13:33 +08:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.animateLayer = nil
|
|
|
|
(layer ?? self.imageView.layer)?.endRotate()
|
|
|
|
NotificationCenter.default.removeObserver(self, name: UIApplication.didEnterBackgroundNotification, object: nil)
|
|
|
|
NotificationCenter.default.removeObserver(self, name: UIApplication.willEnterForegroundNotification, object: nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-06-22 13:27:54 +08:00
|
|
|
|
|
|
|
/// 动画扩展
|
|
|
|
extension HUDController {
|
|
|
|
@objc func didEnterBackground() {
|
|
|
|
if let layer = animateLayer {
|
|
|
|
animation = layer.animation(forKey: .rotateKey)
|
|
|
|
layer.timeOffset = layer.convertTime(CACurrentMediaTime(), from: nil)
|
|
|
|
layer.speed = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@objc func willEnterForeground() {
|
|
|
|
if let layer = animateLayer, let ani = animation, layer.speed == 0 {
|
|
|
|
let pauseTime = layer.timeOffset
|
|
|
|
layer.timeOffset = 0
|
|
|
|
let beginTime = layer.convertTime(CACurrentMediaTime(), from: nil) - pauseTime
|
|
|
|
layer.beginTime = beginTime
|
|
|
|
layer.speed = 1
|
|
|
|
layer.add(ani, forKey: .rotateKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|