This commit is contained in:
2026-09-24 16:25:22 +08:00
commit 7428184f01
1198 changed files with 314515 additions and 0 deletions
@@ -0,0 +1,117 @@
import UIKit
class XdBlur {
///
/// - Parameters:
/// - view:
/// - style: Light, Dark, ExtraLight
/// - intensity: 0-1
/// - backgroundColor:
static func applyBlur(
to view: UIView,
style: UIBlurEffect.Style = .light,
intensity: CGFloat = 0.8,
backgroundColor: UIColor? = nil
) {
//
view.subviews
.filter { $0 is UIVisualEffectView }
.forEach { $0.removeFromSuperview() }
//
let blurEffect = UIBlurEffect(style: style)
let blurView = UIVisualEffectView(effect: blurEffect)
//
if #available(iOS 13.0, *) {
let vibrancyView = UIVisualEffectView(effect:
UIVibrancyEffect(blurEffect: blurEffect, style: .fill)
)
blurView.contentView.addSubview(vibrancyView)
}
// frame
blurView.frame = view.bounds
blurView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
//
blurView.alpha = intensity
//
if let backgroundColor = backgroundColor {
let colorView = UIView(frame: blurView.bounds)
colorView.backgroundColor = backgroundColor
colorView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
blurView.contentView.addSubview(colorView)
}
//
view.insertSubview(blurView, at: 0)
}
///
/// - Parameter view:
static func removeBlur(from view: UIView) {
view.subviews
.filter { $0 is UIVisualEffectView }
.forEach { $0.removeFromSuperview() }
}
///
/// - Parameters:
/// - view:
/// - radius:
/// - tintColor:
static func applyCustomBlur(
to view: UIView,
radius: CGFloat = 10,
tintColor: UIColor? = nil
) {
//
let blurEffect = UIBlurEffect(style: .light)
let blurView = UIVisualEffectView(effect: blurEffect)
//
let vibrancyView = UIVisualEffectView(effect:
UIVibrancyEffect(blurEffect: blurEffect)
)
blurView.frame = view.bounds
blurView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
//
if let tintColor = tintColor {
let colorView = UIView(frame: blurView.bounds)
colorView.backgroundColor = tintColor
colorView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
blurView.contentView.addSubview(colorView)
}
view.insertSubview(blurView, at: 0)
}
static func applyBlurSelf(ele: UIView ,bg:UIView, blur: CGFloat, backgroundColor: UIColor = .clear) {
// Create snapshot of element
UIGraphicsBeginImageContextWithOptions(ele.bounds.size, false, 0)
ele.layer.render(in: UIGraphicsGetCurrentContext()!)
let snapshot = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
// Create blur effect
let effectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
effectView.frame = bg.bounds
// Create color overlay
let colorView = UIView(frame: bg.bounds)
colorView.backgroundColor = backgroundColor
colorView.alpha = blur
// Add views to background
bg.addSubview(effectView)
bg.addSubview(colorView)
// Set snapshot as background
let imageView = UIImageView(image: snapshot)
imageView.frame = bg.bounds
bg.insertSubview(imageView, at: 0)
}
}