【问题标题】:Background UIImageView Blur effect (Swift 4)背景 UIImageView 模糊效果(Swift 4)
【发布时间】:2018-03-04 05:46:37
【问题描述】:

我正在尝试用我的 UIImageView 来实现这种模糊效果:

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

【问题讨论】:

    标签: swift uiimageview shadow blur uivisualeffectview


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回答!我当然会尝试去做,但几乎所有的方法都不一样。
    猜你喜欢
    • 2017-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-06
    • 2021-11-12
    相关资源
    最近更新 更多