如何从顶部到视觉效果视图模糊添加淡入淡出-SWIFT

时间:2019-07-03 16:35:43

标签: ios swift uivisualeffectview

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

示例:

Image

top

1 个答案:

答案 0 :(得分:2)

这对我有用

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()