【问题标题】:Transparent blurred view is not working透明模糊视图不起作用
【发布时间】:2016-12-07 13:24:43
【问题描述】:

我想在 ImageView 上应用层,稍微模糊它。 我在 imageView 之上放了一个视图,子类如下:

class BlurredView: UIView {
override func draw(_ rect: CGRect) {
    self.backgroundColor = UIColor.clear
    let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.extraLight)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)
    blurEffectView.backgroundColor = UIColor.clear
    blurEffectView.frame = self.bounds
    blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    self.addSubview(blurEffectView)
   }
}

结果与我的预期完全不同。

如何自定义它以使这种模糊更轻,所以图片仍然显示一些细节?

【问题讨论】:

  • 你试过改变模糊视图的 alpha 吗?
  • 这样它是透明的,但似乎没有应用模糊效果
  • 你无法真正自定义UIVisualEffectView的效果。要获得您期望的模糊,您必须自己实现模糊。 Core Image 是应该对您有所帮助的搜索词。
  • 我同意@dasdom - Core Image 有几个你可以使用的模糊滤镜。但首先尝试一件事 - 我看到你有 UIBlurEffectStyle.extraLight。尝试使用 .light 代替。信不信由你,这可能会给你更多你想要的东西。
  • 还有一个建议 - 如果您要创建模糊效果的子类,请直接使用:子类 UIVisualEffectView。

标签: ios swift uiimageview uiblureffect


【解决方案1】:

正如评论中提到的https://stackoverflow.com/users/1387306/chrstpsln,这可以通过更改 UIVisualEffectView 的 alpha 来实现。

作为奖励,您可以添加淡入淡出动画以使过渡感觉更平滑。

这是一个如何通过扩展将此效果添加到任何 viewController 的示例:

extension UIViewController {
    func addBlurEffect() {
        let blurEffect = UIBlurEffect(style: .light)

        let blurVisualEffectView = UIVisualEffectView(effect: blurEffect)
        blurVisualEffectView.frame = UIScreen.main.bounds


        blurVisualEffectView.alpha = 0.1

        // Bonus animation - Or just set the alpha higher
        UIView.animate(withDuration: 0.3) {
            blurVisualEffectView.alpha = 0.90
        }

        view.addSubview(blurVisualEffectView)
    }
}

用法:

let viewController = UIViewController() 
viewController.addBlurEffect()

【讨论】:

    【解决方案2】:

    你不能改变模糊量,你唯一能做的就是改变模糊效果样式:

    UIBlurEffectStyle.light
    UIBlurEffectStyle.extraLight
    UIBlurEffectStyle.dark
    

    尝试更改模糊效果的 alpha。此外,由于您的图像很轻,因此您应该使用灯光样式而不是额外的灯光,以免丢失细节。

    let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light)
    blurEffect.alpha = 0.95
    

    【讨论】:

    • UIBlurEffect 没有 alpha 属性!
    猜你喜欢
    • 2013-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-30
    • 1970-01-01
    • 2021-04-28
    • 2011-03-30
    • 1970-01-01
    相关资源
    最近更新 更多