150 lines
4.9 KiB
Swift
150 lines
4.9 KiB
Swift
//
|
|
// SPAlertView.swift
|
|
// MoviaBox
|
|
//
|
|
// Created by 佳尔 on 2025/5/13.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
class SPAlertView: UIView {
|
|
|
|
|
|
var clickSureButton: (() -> Void)?
|
|
var clickCancelButton: (() -> Void)?
|
|
|
|
private var alertWidth: CGFloat = kSPScreenWidth - 52
|
|
private var alertHeight: CGFloat = 284
|
|
|
|
//MARK: UI属性
|
|
private lazy var contentView: UIView = {
|
|
let view = UIView()
|
|
view.backgroundColor = .color202531()
|
|
view.layer.cornerRadius = 18
|
|
view.layer.masksToBounds = true
|
|
view.layer.borderWidth = 1
|
|
view.layer.borderColor = UIColor.color3D4556().cgColor
|
|
return view
|
|
}()
|
|
|
|
private lazy var iconImageView: UIImageView = {
|
|
let imageView = UIImageView()
|
|
return imageView
|
|
}()
|
|
|
|
private lazy var textLabel: UILabel = {
|
|
let label = UILabel()
|
|
label.numberOfLines = 0
|
|
label.font = .fontMedium(ofSize: 20)
|
|
label.textColor = .colorFFFFFF()
|
|
label.textAlignment = .center
|
|
return label
|
|
}()
|
|
|
|
private lazy var cancelButton: UIButton = {
|
|
let button = UIButton(type: .custom)
|
|
button.setTitleColor(.colorFFFFFF(), for: .normal)
|
|
button.titleLabel?.font = .fontMedium(ofSize: 14)
|
|
button.layer.cornerRadius = 18
|
|
button.layer.masksToBounds = true
|
|
button.layer.borderWidth = 1
|
|
button.layer.borderColor = UIColor.colorFFFFFF().cgColor
|
|
button.addTarget(self, action: #selector(handleCancelButton), for: .touchUpInside)
|
|
return button
|
|
}()
|
|
|
|
private lazy var sureButton: UIButton = {
|
|
let button = UIButton(type: .custom)
|
|
button.setBackgroundImage(UIImage(color: .colorFF1F1F()), for: .normal)
|
|
button.setTitleColor(.colorFFFFFF(), for: .normal)
|
|
button.titleLabel?.font = .fontMedium(ofSize: 14)
|
|
button.layer.cornerRadius = 18
|
|
button.layer.masksToBounds = true
|
|
button.addTarget(self, action: #selector(handleSureButton), for: .touchUpInside)
|
|
return button
|
|
}()
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
init(iconImage: UIImage?, text: String, cancelTitle: String, sureTitle: String) {
|
|
super.init(frame: UIScreen.main.bounds)
|
|
self.backgroundColor = .color000000(alpha: 0.8)
|
|
alertHeight = 0
|
|
|
|
// contentView.frame = CGRect(x: 0, y: 0, width: alertWidth, height: alertHeight)
|
|
addSubview(contentView)
|
|
|
|
iconImageView.image = iconImage
|
|
let imageSize = iconImage?.size ?? .zero
|
|
contentView.addSubview(iconImageView)
|
|
iconImageView.frame = CGRect(x: (alertWidth - imageSize.width) / 2, y: 28, width: imageSize.width, height: imageSize.height)
|
|
alertHeight = iconImageView.frame.maxY
|
|
|
|
|
|
textLabel.text = text
|
|
let textSize = textLabel.sizeThatFits(CGSize(width: alertWidth - 50, height: CGFLOAT_MAX))
|
|
contentView.addSubview(textLabel)
|
|
textLabel.frame = CGRect(x: (alertWidth - textSize.width) / 2, y: alertHeight + 25, width: textSize.width, height: textSize.height)
|
|
alertHeight = textLabel.frame.maxY
|
|
|
|
|
|
cancelButton.setTitle(cancelTitle, for: .normal)
|
|
sureButton.setTitle(sureTitle, for: .normal)
|
|
|
|
let buttonWidth = (alertWidth - 60 - 10) / 2
|
|
let buttonHeight = 36.0
|
|
contentView.addSubview(cancelButton)
|
|
contentView.addSubview(sureButton)
|
|
|
|
cancelButton.frame = CGRect(x: 30, y: alertHeight + 38, width: buttonWidth, height: buttonHeight)
|
|
sureButton.frame = CGRect(x: cancelButton.frame.maxX + 10.0, y: cancelButton.frame.minY, width: buttonWidth, height: buttonHeight)
|
|
|
|
alertHeight = sureButton.frame.maxY + 34
|
|
|
|
contentView.frame = CGRect(x: 0, y: 0, width: alertWidth, height: alertHeight)
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
extension SPAlertView {
|
|
// MARK: - 弹出
|
|
@discardableResult func show() -> Self {
|
|
SPAlertWindowManager.manager.createWindow().addSubview(self)
|
|
creatShowAnimation()
|
|
|
|
return self
|
|
}
|
|
func dismiss() {
|
|
removeFromSuperview()
|
|
SPAlertWindowManager.manager.dismissWindow()
|
|
}
|
|
private func creatShowAnimation() {
|
|
contentView.layer.position = center
|
|
contentView.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
|
|
UIView.animate(withDuration: 0.2, animations: {
|
|
self.contentView.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
|
|
}) { finished in
|
|
UIView.animate(withDuration: 0.1, animations: {
|
|
self.contentView.transform = CGAffineTransform.identity
|
|
})
|
|
}
|
|
}
|
|
|
|
@objc private func handleSureButton() {
|
|
self.clickSureButton?()
|
|
self.dismiss()
|
|
}
|
|
|
|
@objc private func handleCancelButton() {
|
|
self.clickCancelButton?()
|
|
self.dismiss()
|
|
}
|
|
}
|