答案 0 :(得分:7)
您可以使用具有2对重复颜色的渐变:
let gradient: CAGradientLayer = CAGradientLayer()
gradient.frame = button.bounds
gradient.colors = [
UIColor.greenColor().CGColor,
UIColor.greenColor().CGColor,
UIColor.blackColor().CGColor,
UIColor.blackColor().CGColor
]
/* repeat the central location to have solid colors */
gradient.locations = [0, 0.5, 0.5, 1.0]
/* make it horizontal */
gradient.startPoint = CGPointMake(0, 0.5)
gradient.endPoint = CGPointMake(1, 0.5)
button.layer.insertSublayer(gradient, atIndex: 0)
您可以使用开始/结束点更改演奏方向:
gradient.startPoint = CGPointMake(0, 0)
gradient.endPoint = CGPointMake(1, 1)
答案 1 :(得分:2)
您可以使用CAGradientLayer
来制作
let layer : CAGradientLayer = CAGradientLayer()
layer.frame.size = button.frame.size
layer.startPoint = CGPointZero
layer.endPoint = CGPointMake(1, 0)
let colorGreen = UIColor.greenColor().CGColor
let colorBlack = UIColor.blackColor().CGColor
layer.colors = [colorGreen, colorGreen, colorBlack, colorBlack]
layer.locations = [0.0, 0.5, 0.5, 1.0]
button.layer.insertSublayer(layer, atIndex: 0)