let layer = CAGradientLayer()
layer.frame = view.bounds
///设置颜色
layer.colors = [color("61ccc8").cgColor,color("61ccc8").cgColor,kRGBColorAlpha(r: 97, g: 204, b: 200, alpha: 0.1).cgColor]
///设置颜色渐变的位置 (我这里是横向 中间点开始变化)
layer.locations = [0,0.5,1]
///开始的坐标点
layer.startPoint = CGPoint(x: 0, y: 0)
///结束的坐标点
layer.endPoint = CGPoint(x: 1, y: 0)
view.layer.addSublayer(layer)
很多时候会动态设置渐变色的范围,即会调整layer的frame,而设置layer的frame可能会触发视图变化的动画。此时我们需要关闭layer隐式动画:
[CATransaction begin];
[CATransaction setDisableActions:YES];
layer.frame = self.bounds;
[CATransaction commit];