|
| 1 | +// |
| 2 | +// AlertManager.swift |
| 3 | +// Scene-Examples |
| 4 | +// |
| 5 | +// Created by zhaoyongqiang on 2021/11/10. |
| 6 | +// |
| 7 | + |
| 8 | +import UIKit |
| 9 | +import AVFoundation |
| 10 | + |
| 11 | +public let cl_screenWidht = UIScreen.main.bounds.width |
| 12 | +public let cl_screenHeight = UIScreen.main.bounds.height |
| 13 | +class AlertManager: NSObject { |
| 14 | + private struct AlertViewCache { |
| 15 | + var view: UIView? |
| 16 | + var index: Int = 0 |
| 17 | + } |
| 18 | + enum AlertPosition { |
| 19 | + case center |
| 20 | + case bottom |
| 21 | + } |
| 22 | + |
| 23 | + private static var vc: UIViewController? |
| 24 | + private static var containerView: UIView? |
| 25 | + private static var currentPosition: AlertPosition = .center |
| 26 | + private static var viewCache: [AlertViewCache] = [] |
| 27 | + private static var bottomAnchor: NSLayoutConstraint? |
| 28 | + |
| 29 | + public static func show(view: UIView, |
| 30 | + alertPostion: AlertPosition = .center, |
| 31 | + didCoverDismiss: Bool = true) { |
| 32 | + let index = viewCache.isEmpty ? 0 : viewCache.count |
| 33 | + viewCache.append(AlertViewCache(view: view, index: index)) |
| 34 | + currentPosition = alertPostion |
| 35 | + if vc == nil { |
| 36 | + containerView = UIButton(frame: CGRect(x: 0, y: 0, width: cl_screenWidht, height: cl_screenHeight)) |
| 37 | + containerView?.backgroundColor = UIColor(red: 0.0/255, green: 0.0/255, blue: 0.0/255, alpha: 0.0) |
| 38 | + } |
| 39 | + if didCoverDismiss { |
| 40 | + (containerView as? UIButton)?.addTarget(self, action: #selector(tapView), for: .touchUpInside) |
| 41 | + } |
| 42 | + guard let containerView = containerView else { return } |
| 43 | + containerView.addSubview(view) |
| 44 | + view.translatesAutoresizingMaskIntoConstraints = false |
| 45 | + view.alpha = 0 |
| 46 | + if alertPostion == .center { |
| 47 | + view.centerXAnchor.constraint(equalTo: containerView.centerXAnchor).isActive = true |
| 48 | + view.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true |
| 49 | + }else{ |
| 50 | + bottomAnchor = view.bottomAnchor.constraint(equalTo: containerView.bottomAnchor) |
| 51 | + view.leadingAnchor.constraint(equalTo: containerView.leadingAnchor).isActive = true |
| 52 | + view.trailingAnchor.constraint(equalTo: containerView.trailingAnchor).isActive = true |
| 53 | + } |
| 54 | + if vc == nil { |
| 55 | + vc = UIViewController() |
| 56 | + vc?.view.backgroundColor = UIColor.clear |
| 57 | + vc?.view.addSubview(containerView) |
| 58 | + vc?.modalPresentationStyle = .custom |
| 59 | + UIViewController.cl_topViewController()?.present(vc!, animated: false) { |
| 60 | + showAlertPostion(alertPostion: alertPostion, view: view) |
| 61 | + } |
| 62 | + } else { |
| 63 | + showAlertPostion(alertPostion: alertPostion, view: view) |
| 64 | + } |
| 65 | + //注册键盘出现通知 |
| 66 | + NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: UIApplication.keyboardWillShowNotification, object: nil) |
| 67 | + |
| 68 | + //注册键盘隐藏通知 |
| 69 | + NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIApplication.keyboardWillHideNotification, object: nil) |
| 70 | + } |
| 71 | + |
| 72 | + private static func showAlertPostion(alertPostion: AlertPosition, view: UIView) { |
| 73 | + containerView?.layoutIfNeeded() |
| 74 | + if alertPostion == .center { |
| 75 | + showCenterView(view: view) |
| 76 | + }else{ |
| 77 | + bottomAnchor?.constant = view.frame.height |
| 78 | + bottomAnchor?.isActive = true |
| 79 | + containerView?.layoutIfNeeded() |
| 80 | + showBottomView(view: view) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private static func showCenterView(view: UIView){ |
| 85 | + if !viewCache.isEmpty { |
| 86 | + viewCache.forEach({ $0.view?.alpha = 0 }) |
| 87 | + } |
| 88 | + UIView.animate(withDuration: 0.25, animations: { |
| 89 | + containerView?.backgroundColor = UIColor(red: 0.0/255, |
| 90 | + green: 0.0/255, |
| 91 | + blue: 0.0/255, |
| 92 | + alpha: 0.5) |
| 93 | + view.alpha = 1.0 |
| 94 | + }) |
| 95 | + } |
| 96 | + |
| 97 | + private static func showBottomView(view: UIView){ |
| 98 | + if !viewCache.isEmpty { |
| 99 | + viewCache.forEach({ $0.view?.alpha = 0 }) |
| 100 | + } |
| 101 | + view.alpha = 1.0 |
| 102 | + bottomAnchor?.constant = 0 |
| 103 | + bottomAnchor?.isActive = true |
| 104 | + UIView.animate(withDuration: 0.25, animations: { |
| 105 | + containerView?.backgroundColor = UIColor(red: 0.0/255, |
| 106 | + green: 0.0/255, |
| 107 | + blue: 0.0/255, |
| 108 | + alpha: 0.5) |
| 109 | + containerView?.superview?.layoutIfNeeded() |
| 110 | + }) |
| 111 | + } |
| 112 | + |
| 113 | + static func updateViewHeight() { |
| 114 | + UIView.animate(withDuration: 0.25, animations: { |
| 115 | + containerView?.layoutIfNeeded() |
| 116 | + }) |
| 117 | + } |
| 118 | + |
| 119 | + static func hiddenView(all: Bool = true, completion: (() -> Void)? = nil){ |
| 120 | + if currentPosition == .bottom { |
| 121 | + guard let lastView = viewCache.last?.view else { return } |
| 122 | + bottomAnchor?.constant = lastView.frame.height |
| 123 | + bottomAnchor?.isActive = true |
| 124 | + } |
| 125 | + UIView.animate(withDuration: 0.25, animations: { |
| 126 | + if all || viewCache.isEmpty { |
| 127 | + containerView?.backgroundColor = UIColor(red: 255.0/255, |
| 128 | + green: 255.0/255, |
| 129 | + blue: 255.0/255, |
| 130 | + alpha: 0.0) |
| 131 | + containerView?.layoutIfNeeded() |
| 132 | + } |
| 133 | + if currentPosition == .center { |
| 134 | + viewCache.last?.view?.alpha = 0 |
| 135 | + } |
| 136 | + }, completion: { (_) in |
| 137 | + if all || viewCache.isEmpty { |
| 138 | + vc?.dismiss(animated: false, completion: completion) |
| 139 | + vc = nil |
| 140 | + } else { |
| 141 | + viewCache.removeLast() |
| 142 | + viewCache.last?.view?.alpha = 1 |
| 143 | + } |
| 144 | + }) |
| 145 | + } |
| 146 | + |
| 147 | + @objc |
| 148 | + private static func tapView(){ |
| 149 | + DispatchQueue.main.asyncAfter(deadline: DispatchTime(uptimeNanoseconds: UInt64(0.1))) { |
| 150 | + self.hiddenView() |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private static var originFrame:CGRect = .zero |
| 155 | + // 键盘显示 |
| 156 | + @objc private static func keyboardWillShow(notification: Notification) { |
| 157 | + let keyboardHeight = (notification.userInfo?["UIKeyboardBoundsUserInfoKey"] as? CGRect)?.height |
| 158 | + let y = cl_screenHeight - (keyboardHeight ?? 304) - containerView!.frame.height |
| 159 | + if originFrame.origin.y != y { |
| 160 | + originFrame = containerView!.frame |
| 161 | + } |
| 162 | + UIView.animate(withDuration: 0.25) { |
| 163 | + containerView?.frame.origin.y = y |
| 164 | + } |
| 165 | + } |
| 166 | + // 键盘隐藏 |
| 167 | + @objc private static func keyboardWillHide(notification: Notification) { |
| 168 | + UIView.animate(withDuration: 0.25) { |
| 169 | + containerView?.frame = originFrame |
| 170 | + } completion: { _ in |
| 171 | + if currentPosition == .bottom { |
| 172 | + hiddenView() |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | +} |
| 177 | + |
0 commit comments