背景UIImageView模糊效果(Swift 4)

问题描述 投票:0回答:1

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

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

swift uiimageview shadow blur uivisualeffectview
1个回答
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:我从其他一些来源获得此代码(我忘了)。

© www.soinside.com 2019 - 2024. All rights reserved.