背景UIImageView模糊效果(Swift 4)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了背景UIImageView模糊效果(Swift 4)相关的知识,希望对你有一定的参考价值。
答案
我有一个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:我从其他一些来源获得此代码(我忘了)。
以上是关于背景UIImageView模糊效果(Swift 4)的主要内容,如果未能解决你的问题,请参考以下文章
对于 Uiimageview 如何在 ios swift 4 中将 CGgradient 背景颜色设置为背景颜色?