背景UIImageView模糊效果(Swift 4)

时间:2018-03-04 05:46:38

标签: swift uiimageview shadow blur uivisualeffectview

我试图用我的UIImageView实现这种模糊效果: enter image description here

如果可能,我不会。我试过玩UIVisualEffectView和UIBlurEffect,但我没有运气。请问有什么想法吗?谢谢

1 个答案:

答案 0 :(得分:0)

我有一个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:我从其他一些来源获得此代码(我忘了)。