ThimraTV/ThimraTV/Base/View/SPFadeEdgeImageView.swift
2025-06-20 18:46:17 +08:00

54 lines
1.6 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// SPFadeEdgeImageView.swift
// ThimraTV
//
// Created by on 2025/4/28.
//
import UIKit
class SPFadeEdgeImageView: UIImageView {
private var blurredImage: UIImage?
override func layoutSubviews() {
super.layoutSubviews()
applyCircularFade()
}
///
func applyCircularFade(radius: CGFloat? = nil) {
let maskLayer = CALayer()
maskLayer.frame = bounds
//
UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0)
guard let ctx = UIGraphicsGetCurrentContext() else { return }
let center = CGPoint(x: bounds.midX, y: bounds.midY)
let maxRadius = radius ?? min(bounds.width, bounds.height) / 2
let colorSpace = CGColorSpaceCreateDeviceGray()
let colors: [CGFloat] = [1, 1, 0, 0] // 10
let locations: [CGFloat] = [0, 1]
if let gradient = CGGradient(colorSpace: colorSpace, colorComponents: colors, locations: locations, count: 2) {
ctx.drawRadialGradient(
gradient,
startCenter: center, startRadius: 0,
endCenter: center, endRadius: maxRadius,
options: .drawsAfterEndLocation
)
}
let maskImage = UIGraphicsGetImageFromCurrentImageContext()?.cgImage
UIGraphicsEndImageContext()
maskLayer.contents = maskImage
self.layer.mask = maskLayer
}
}