自定义View,在init方法中
layer.contentsScale = UIScreen.main.scale;
// 默认是0,不显示阴影
layer.shadowOpacity = 1;
layer.shadowRadius = 6.0;
// width: 正是向右偏移 height:正是向下偏移
layer.shadowOffset = CGSize.init(width: 0, height: 3);
layer.shadowColor = UIColor.red.cgColor
//设置缓存
layer.shouldRasterize = true;
//设置抗锯齿边缘
layer.rasterizationScale = UIScreen.main.scale;
在layoutSubViews()中
// 设置阴影形状
layer.shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: bounds.size.height/2).cgPath;
// 设置圆角
layer.cornerRadius = bounds.size.height/2
利用这个自定义View创建的视图,都是圆角与带阴影的视图。
网友评论