167 lines
4.6 KiB
Swift
167 lines
4.6 KiB
Swift
//
|
|
// SRBaseAlert.swift
|
|
// SynthReel
|
|
//
|
|
// Created by CSGY on 2025/11/27.
|
|
// Copyright © 2025 SR. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
class SRBaseAlert: UIView {
|
|
|
|
var clickHighlightButton: (() -> Void)?
|
|
|
|
var contentWidth: CGFloat = UIScreen.width - 70 {
|
|
didSet {
|
|
containerView.snp.updateConstraints { make in
|
|
make.width.equalTo(contentWidth)
|
|
}
|
|
}
|
|
}
|
|
|
|
private(set) var containerView: UIView = {
|
|
let view = UIView()
|
|
return view
|
|
}()
|
|
|
|
private(set) var contentView: UIView = {
|
|
let view = UIView()
|
|
view.backgroundColor = ._404040
|
|
view.layer.cornerRadius = 16
|
|
view.layer.masksToBounds = true
|
|
return view
|
|
}()
|
|
|
|
private(set) lazy var closeButton: UIButton = {
|
|
let button = UIButton(type: .custom)
|
|
button.setImage(UIImage(named: "Close"), for: .normal)
|
|
button.addTarget(self, action: #selector(dismiss), for: .touchUpInside)
|
|
return button
|
|
}()
|
|
|
|
lazy var highlightButton: UIButton = {
|
|
let button = UIButton(type: .custom)
|
|
button.layer.cornerRadius = 16
|
|
button.layer.masksToBounds = true
|
|
button.setTitleColor(.srGreen, for: .normal)
|
|
button.setBackgroundImage(.suerButtonBg, for: .normal)
|
|
button.titleLabel?.font = .font(ofSize: 14, weight: .semibold)
|
|
button.addTarget(self, action: #selector(handleHighlightButton), for: .touchUpInside)
|
|
return button
|
|
}()
|
|
|
|
override init(frame: CGRect) {
|
|
super.init(frame: frame)
|
|
backgroundColor = ._000000.withAlphaComponent(0.5)
|
|
|
|
addSubview(containerView)
|
|
containerView.addSubview(contentView)
|
|
containerView.addSubview(closeButton)
|
|
|
|
containerView.snp.makeConstraints { make in
|
|
make.center.equalToSuperview()
|
|
make.width.equalTo(contentWidth)
|
|
}
|
|
|
|
contentView.snp.makeConstraints { make in
|
|
make.left.right.top.equalToSuperview()
|
|
make.bottom.equalToSuperview().offset(-66)
|
|
}
|
|
|
|
closeButton.snp.makeConstraints { make in
|
|
make.centerX.equalToSuperview()
|
|
make.bottom.equalToSuperview()
|
|
}
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
@discardableResult
|
|
@objc func show(in view: UIView? = nil) -> Self {
|
|
guard self.superview == nil else { return self }
|
|
|
|
var inView: UIView
|
|
if let view = view {
|
|
inView = view
|
|
} else {
|
|
inView = SRBaseAlert.Window.manager.createWindow()
|
|
}
|
|
|
|
inView.addSubview(self)
|
|
self.frame = inView.bounds
|
|
showAnimation()
|
|
|
|
return self
|
|
}
|
|
@objc func dismiss() {
|
|
dismissAnimation()
|
|
}
|
|
|
|
@objc func handleHighlightButton() {
|
|
self.dismissAnimation()
|
|
self.clickHighlightButton?()
|
|
}
|
|
|
|
}
|
|
|
|
extension SRBaseAlert {
|
|
private func showAnimation() {
|
|
containerView.transform = CGAffineTransform(translationX: 0, y: 200)
|
|
|
|
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0) {
|
|
self.containerView.transform = CGAffineTransform.identity
|
|
}
|
|
}
|
|
|
|
private func dismissAnimation() {
|
|
|
|
UIView.animate(withDuration: 0.3) {
|
|
self.alpha = 0
|
|
self.containerView.transform = CGAffineTransform(translationX: 0, y: 500)
|
|
} completion: { _ in
|
|
self.removeFromSuperview()
|
|
SRBaseAlert.Window.manager.dismissWindow()
|
|
}
|
|
}
|
|
}
|
|
|
|
extension SRBaseAlert {
|
|
class Window {
|
|
static let manager = Window()
|
|
|
|
private(set) var window: UIWindow?
|
|
|
|
func createWindow() -> UIWindow {
|
|
guard let window = window else {
|
|
let window = UIWindow(windowScene: SRTool.windowScene!)
|
|
window.backgroundColor = .clear
|
|
window.windowLevel = .alert
|
|
window.isHidden = false
|
|
self.window = window
|
|
return window
|
|
}
|
|
return window
|
|
}
|
|
|
|
func dismissWindow() {
|
|
guard let window = self.window else { return }
|
|
|
|
var isHidden = true
|
|
|
|
window.subviews.forEach {
|
|
if $0.isKind(of: SRBaseAlert.self) {
|
|
isHidden = false
|
|
}
|
|
}
|
|
if isHidden {
|
|
window.isHidden = true
|
|
self.window = nil
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|