我有一个NSVisualEffectView
的例子。也许你可以尝试将其转换为UIVisualEffectView
:
func applyBlurToWindow() {
// define the visual effect view
self.blurView = NSVisualEffectView(frame: NSRect(x: 0, y: 0, width: (NSApplication.shared.windows.first?.frame.width)!, height: (NSApplication.shared.windows.first?.frame.height)!))
// this is default value but is here for clarity
blurView?.blendingMode = NSVisualEffectView.BlendingMode.behindWindow
// set the background to always be the dark blur
blurView?.material = NSVisualEffectView.Material.mediumLight
blurView?.wantsLayer = true
blurView?.layer?.backgroundColor = NSColor.clear.cgColor
blurView?.layer?.masksToBounds = true
blurView?.layerUsesCoreImageFilters = true
blurView?.layer?.needsDisplayOnBoundsChange = true
let satFilter = CIFilter(name: "CIColorControls")
satFilter?.setDefaults()
satFilter?.setValue(NSNumber(value: 0.0), forKey: "inputSaturation")
let blurFilter = CIFilter(name: "CIGaussianBlur")
blurFilter?.setDefaults()
blurFilter?.setValue(NSNumber(value: 2.0), forKey: "inputRadius")
blurView?.layer?.backgroundFilters = [satFilter!, blurFilter!]
// set it to always be blurry regardless of window state
if let window = NSApplication.shared.windows.first {
window.acceptsMouseMovedEvents = true
blurView?.state = .active
window.contentView?.addSubview(blurView!)
}
}
func removeBlurredView(){
blurView?.state = .inactive
blurView?.layer?.backgroundFilters = nil
self.blurView?.removeFromSuperview()
}
PS:我从其他一些来源获得此代码(我忘了)。