答案 0 :(得分:2)
答案很大程度上取决于设计师用来创建渐变的内容。有一些工具,如Invision,为开发人员提供有用的信息,如渐变颜色和点。但是,如果他们只是在Photoshop中创建它,那么它很难以编程方式重现,也许你应该只是要求一个渐变的图像。
另一个解决方案是在Xcode中使用@IBDesignable
渐变视图;许多开发人员创建了它们,包括我,主要是因为过去不得不经常处理这个问题。这种方法的一个主要优点是,它可以非常容易地调整颜色,甚至可以根据需要设置动画。
我有一个开源示例available on GitHub,而且我写了extended explanation of how it works。
我很快就尝试使用我的项目制作像你这样的渐变,这就是它在Xcode中的样子: -
关键部分是属性检查器面板的顶部,您可以在其中轻松设置开始和结束渐变颜色,以及渐变角度 - 您的角度大约为310度。
您需要的主要代码段粘贴在下方。请点击上面的链接,了解它的工作原理。
import UIKit
@IBDesignable
class LDGradientView: UIView {
// the gradient start colour
@IBInspectable var startColor: UIColor? {
didSet {
updateGradient()
}
}
// the gradient end colour
@IBInspectable var endColor: UIColor? {
didSet {
updateGradient()
}
}
// the gradient angle, in degrees anticlockwise from 0 (east/right)
@IBInspectable var angle: CGFloat = 270 {
didSet {
updateGradient()
}
}
// the gradient layer
private var gradient: CAGradientLayer?
// initializers
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
installGradient()
}
override init(frame: CGRect) {
super.init(frame: frame)
installGradient()
}
// Create a gradient and install it on the layer
private func installGradient() {
// if there's already a gradient installed on the layer, remove it
if let gradient = self.gradient {
gradient.removeFromSuperlayer()
}
let gradient = createGradient()
self.layer.addSublayer(gradient)
self.gradient = gradient
}
// Update an existing gradient
private func updateGradient() {
if let gradient = self.gradient {
let startColor = self.startColor ?? UIColor.clear
let endColor = self.endColor ?? UIColor.clear
gradient.colors = [startColor.cgColor, endColor.cgColor]
let (start, end) = gradientPointsForAngle(self.angle)
gradient.startPoint = start
gradient.endPoint = end
}
}
// create gradient layer
private func createGradient() -> CAGradientLayer {
let gradient = CAGradientLayer()
gradient.frame = self.bounds
return gradient
}
// create vector pointing in direction of angle
private func gradientPointsForAngle(_ angle: CGFloat) -> (CGPoint, CGPoint) {
// get vector start and end points
let end = pointForAngle(angle)
//let start = pointForAngle(angle+180.0)
let start = oppositePoint(end)
// convert to gradient space
let p0 = transformToGradientSpace(start)
let p1 = transformToGradientSpace(end)
return (p0, p1)
}
// get a point corresponding to the angle
private func pointForAngle(_ angle: CGFloat) -> CGPoint {
// convert degrees to radians
let radians = angle * .pi / 180.0
var x = cos(radians)
var y = sin(radians)
// (x,y) is in terms unit circle. Extrapolate to unit square to get full vector length
if (fabs(x) > fabs(y)) {
// extrapolate x to unit length
x = x > 0 ? 1 : -1
y = x * tan(radians)
} else {
// extrapolate y to unit length
y = y > 0 ? 1 : -1
x = y / tan(radians)
}
return CGPoint(x: x, y: y)
}
// transform point in unit space to gradient space
private func transformToGradientSpace(_ point: CGPoint) -> CGPoint {
// input point is in signed unit space: (-1,-1) to (1,1)
// convert to gradient space: (0,0) to (1,1), with flipped Y axis
return CGPoint(x: (point.x + 1) * 0.5, y: 1.0 - (point.y + 1) * 0.5)
}
// return the opposite point in the signed unit square
private func oppositePoint(_ point: CGPoint) -> CGPoint {
return CGPoint(x: -point.x, y: -point.y)
}
// ensure the gradient gets initialized when the view is created in IB
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
installGradient()
updateGradient()
}
}