怎么给view添加渐变色:
我们来创建一个View的扩展
extension UIView {
//Colors:渐变色色值数组
func setLayerColors(_ colors:[CGColor]) {
let layer = CAGradientLayer()
layer.frame = bounds
layer.colors = colors
layer.startPoint = CGPoint(x: 0, y: 0)
layer.endPoint = CGPoint(x: 1, y: 0)
self.layer.addSublayer(layer)
}
}
- 调用示例
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let colors = [UIColor.red.cgColor,
UIColor.yellow.cgColor,
UIColor.blue.cgColor
]
view.setLayerColors(colors)
}
}
效果图
效果图
网友评论