ThimraTV/MoviaBox/Base/Extension/UIView+SPAdd.swift
2025-05-09 13:28:26 +08:00

139 lines
4.0 KiB
Swift

//
// UIView+SPAdd.swift
// MoviaBox
//
// Created by on 2025/4/9.
//
import UIKit
import SnapKit
extension UIView {
fileprivate struct AssociatedKeys {
static var sp_tapGesture: Int?
static var sp_effect: Int?
static var sp_circulars: Int?
}
@objc public static func sp_Awake() {
sp_swizzled_instanceMethod("sp", oldClass: self, oldSelector: "layoutSubviews", newClass: self)
}
@objc func sp_layoutSubviews() {
sp_layoutSubviews()
_updateRadius()
if let effectView = effectView, effectView.frame != self.bounds {
effectView.frame = self.bounds
}
}
///
func getFirstResponderView() -> UIView? {
var resultView: UIView? = nil
for view in self.subviews {
if view.isFirstResponder {
resultView = view
break
} else {
resultView = view.getFirstResponderView()
if resultView != nil {
break
}
}
}
return resultView
}
}
//MARK: -------------- --------------
extension UIView {
private var effectView: UIVisualEffectView? {
get {
return objc_getAssociatedObject(self, &AssociatedKeys.sp_effect) as? UIVisualEffectView
}
set {
objc_setAssociatedObject(self, &AssociatedKeys.sp_effect, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
///
func addEffectView(style: UIBlurEffect.Style = .dark) {
if self.effectView == nil {
let blur = UIBlurEffect(style: style)
let effectView = UIVisualEffectView(effect: blur)
self.addSubview(effectView)
self.sendSubviewToBack(effectView)
self.effectView = effectView
}
}
///
func removeEffectView() {
self.effectView?.removeFromSuperview()
self.effectView = nil
}
}
//MARK: -------------- --------------
extension UIView {
private var circulars: SPCirculars? {
get {
return objc_getAssociatedObject(self, &AssociatedKeys.sp_circulars) as? SPCirculars
}
set {
objc_setAssociatedObject(self, &AssociatedKeys.sp_circulars, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
func addRadius(topLeft: CGFloat, topRight: CGFloat, bottomLeft: CGFloat, bottomRight: CGFloat) {
//
self.circulars = SPCirculars(topLeft: topLeft, topRight: topRight, bottomLeft: bottomLeft, bottomRight: bottomRight)
_updateRadius()
}
private func _updateRadius() {
guard let circulars = self.circulars else { return }
let rect = self.bounds
let path = CGMutablePath()
path.addRadiusRectangle(circulars, rect: rect)
let maskLayer = CAShapeLayer()
maskLayer.frame = self.bounds
maskLayer.path = path
self.layer.mask = maskLayer
}
}
//MARK: -------------- --------------
extension UIView {
private var sp_tapGestureBlock: ((_ view: UIView) -> Void)? {
get {
return objc_getAssociatedObject(self,&AssociatedKeys.sp_tapGesture) as? ((_ view: UIView) -> Void)
}
set {
objc_setAssociatedObject(self,&AssociatedKeys.sp_tapGesture, newValue, .OBJC_ASSOCIATION_COPY)
}
}
func sp_addTapGestureRecognizer(_ block: ((_ view: UIView) -> Void)?) {
if sp_tapGestureBlock == nil {
let tap = UITapGestureRecognizer(target: self, action: #selector(handleTapGestureRecognizer(tap:)))
self.addGestureRecognizer(tap)
}
self.sp_tapGestureBlock = block
}
@objc private func handleTapGestureRecognizer(tap: UITapGestureRecognizer) {
if tap.state == .recognized {
self.sp_tapGestureBlock?(self)
}
}
}