【发布时间】:2019-09-04 05:12:42
【问题描述】:
【问题讨论】:
【问题讨论】:
您希望 Start PointX 和 End PointX 为 0。然后将 Start PointY 设置为 0,End PointY 设置为 1。这将给出一个从顶部到底部的渐变,从顶部的顶部颜色和底部的底部颜色开始.
【讨论】:
老实说,我不知道您可以通过情节提要添加渐变,但如果您通过代码执行此操作,您将有更多选择。比如更多的颜色、颜色位置等
// Create a gradient layer
let gradient: CAGradientLayer = CAGradientLayer()
// Create an array of colours you can use, as many colours as you require
gradient.colors = [.blue.cgColor, .red.cgColor, .orange.cgColor].cgColor
// Chose the locations for your colors, 0.5 is center
gradient.locations = [0.0, 0.5, 1.0]
// Start and end points so this goes from y: 0.0 to y: 1.0 so top to bottom
gradient.startPoint = CGPoint(x: 1.0, y: 0.0)
gradient.endPoint = CGPoint(x: 1.0, y: 1.0)
// Set the frame
gradient.frame = CGRect(x: 0.0, y: 0.0, width: yourView.frame.size.width, height: yourView.frame.size.height)
// Add to view
yourView.layer.insertSublayer(gradient, at: 0)
【讨论】: