# ***CALayer层***

2019-04-24  本文已影响0人  79d12e22ec53

CALayer 边框

let view
view.layer.borderWidth = 20
view.layer.borderColor = UIColor.black.cgColor

CALayer是定义在QuartzCore框架中的,所以需要使用具有跨平台特性的CGColorRef数据类型

CALayer 投影

view.layer.shadowColor = UIColor.black.cgColor
view.layer,shadowOffset = CGSize(width: , height: )
view.layer.shadowOpacity = 0.45 //不透明度
view.layer.shadowRadius = 5.0

CALayer 设置圆角

//设置四个圆角
view.layer.cornerRadius = 40
view.layer.masksToBounds = true //一定要设置遮罩
//(headImgView?.frame.width)!/2 设置为圆形

// 设置部分圆角
view.layer.maskedCorners = [CACornerMask.layerMinXMinYCorner , CACornerMask.layerMaxXMinYCorner]
view.layer.cornerRadius = 40
view.layer.masksToBounds = true

CALayer 渐变

let view
let gradientLayer = CAGradientLayer()

let fromColor = UIColor.yellow.cgColor
let midColor = UIColor.blue.cgColor
let toColor = UIColor.red.cgColor

gradientLayer.colors = [fromColor, midColor, toColor]
gradientLayer.startPoint = CGPoint
gradientLayer.endPoint
gradientLayer.locations = [0, 0.3, 1] // 设置渐变层各颜色点在颜色线中的分布情况,0.3表示位于渐变线30%的位置
gradientLayer.layer.addSublayer(gradientLayer)
self.view.addSubview(gradientLayer)
上一篇下一篇

猜你喜欢

热点阅读