目前我有一个应用于UI
ImageView的CALayer,如下所示:
let l: CALayer = imageView.layer l.masksToBounds = true l.cornerRadius = 20.0
我想实现如下所示的模糊效果:
我尝试过以下方法:
let blurEffect = UIBlurEffect(style: .light) let visualEffectView = UIVisualEffectView(effect: blurEffect) imageView.addSubview(visualEffectView)
以及:
var visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light)) visualEffectView.frame = staffImageView.bounds imageView.addSubview(visualEffectView)
但是,两者都没有达到我想要的效果.
有人可以提供帮助吗?这似乎相当简单,但我无法实现它.
更新:
我试着玩渐变但没有太多运气:
var maskLayer = CAGradientLayer() maskLayer.frame = cell.imageView.bounds let black = UIColor.white.cgColor let clear = UIColor(white: 1,alpha: 0).cgColor maskLayer.colors = [black,clear] maskLayer.locations = [0.0,0.10] maskLayer.startPoint = CGPoint(x: 1,y: 0) maskLayer.endPoint = CGPoint(x: 1,y: 1) cell.imageView.layer.addSublayer(maskLayer)
结果:
解决方法
斯威夫特4
let maskLayer = CAGradientLayer() maskLayer.frame = yourImageView.bounds maskLayer.shadowRadius = 5 maskLayer.shadowPath = CGPath(roundedRect: YourImagView.bounds.insetBy(dx: 5,dy: 5),cornerWidth: 10,cornerHeight: 10,transform: nil) maskLayer.shadowOpacity = 1 maskLayer.shadowOffset = CGSize.zero maskLayer.shadowColor = UIColor.white.cgColor yourImageView.layer.mask = maskLayer