Swift-同时添加圆角和阴影

2018-04-19  本文已影响23人  路在脚下了

自定义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创建的视图,都是圆角与带阴影的视图。

上一篇 下一篇

猜你喜欢

热点阅读