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