【问题标题】:How can I add fade from top to visual effect view Blur - SWIFT如何从顶部添加淡入淡出到视觉效果视图模糊 - SWIFT
【发布时间】:2019-07-03 16:35:43
【问题描述】:

如何从顶部添加淡入淡出到视觉效果视图模糊? 我希望顶部边缘不可见。

例子:

let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(blurEffectView)

【问题讨论】:

  • 我认为您不会使用简单的模糊效果视图来做到这一点。您更有可能需要通过 CIFilter 传递图像。
  • @matt 不能使用 Shape Layer 完成吗?
  • 如果我们忽略“模糊”部分,您可以使用渐变蒙版轻松做到这一点。但是当你应用模糊时会发生什么并不那么清楚,因为它以一种奇怪的方式合成。这就是为什么我建议您使用滤镜进行模糊处理,现在您可以对结果进行渐变蒙版(作为滤镜的一部分或作为单独的蒙版)。
  • @matt 我想添加一个视频,然后我不能使用 CIFilter
  • 您的问题中没有视频。我建议你重新开始,更清楚地说明你真正想要做什么以及为什么它不起作用。

标签: ios swift uivisualeffectview


【解决方案1】:

这对我有用

extension UIView{
    func createGradientBlur() {
        let gradientLayer = CAGradientLayer()
        gradientLayer.colors = [
        UIColor.white.withAlphaComponent(0).cgColor,
        UIColor.white.withAlphaComponent(1).cgColor]
        let viewEffect = UIBlurEffect(style: .light)
        let effectView = UIVisualEffectView(effect: viewEffect)
        effectView.frame = CGRect(x: self.bounds.origin.x, y: self.bounds.size.height, width: self.bounds.width, height: self.bounds.size.height)
        gradientLayer.frame = effectView.bounds
        gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.0)
        gradientLayer.endPoint = CGPoint(x: 0.0 , y: 0.3)
        effectView.autoresizingMask = [.flexibleHeight]
        effectView.layer.mask = gradientLayer
        effectView.isUserInteractionEnabled = false //Use this to pass touches under this blur effect
        addSubview(effectView)

    }
}

使用方法:

var myView = UIView()
myView.createGradientBlur()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多