backView是要添加阴影的视图,思路是backView设置corner圆角,再添加一个带阴影的父视图,代码如下:
self.backView.layer.cornerRadius = 30;
self.backView.layer.masksToBounds = true;
let shadowView = UIView.init(frame: self.backView.frame)
self.addSubview(shadowView)
shadowView.layer.shadowColor = color;
shadowView.layer.shadowOffset = CGSize(width:0, height:2);
shadowView.layer.shadowOpacity = 1;
shadowView.layer.shadowRadius = 4;
shadowView.layer.cornerRadius = 30;
shadowView.clipsToBounds = false;
shadowView.addSubview(self.backView)
clipsToBounds这个属性的意思是子视图超出边界的区域是否裁剪