这是我的代码:
let gradient = CAGradientLayer()
gradient.opacity = 0.8
let startColor = UIColor(red: 1, green: 0, blue: 0, alpha: 1).cgColor
let endColor = UIColor(red: 0, green: 1, blue: 0, alpha: 1).cgColor
gradient.colors = [startColor, endColor, startColor]
gradient.locations = [0.0, 0.1, 0.95]
gradient.frame = viewInStack1Background.frame
viewInStack1Background.layer.addSublayer(gradient)
let gradient2 = CAGradientLayer()
gradient2.colors = [startColor, UIColor.clear.cgColor, startColor]
gradient2.locations = [0.0, 0.1, 0.95]
gradient2.opacity = 0.8
gradient2.startPoint = CGPoint(x: 0.0, y: 0.5)
gradient2.endPoint = CGPoint(x: 1.0, y: 0.5)
gradient2.frame = viewInStack1Background.frame
viewInStack1Background.layer.addSublayer(gradient2)
我想要外面是红色,里面是绿色。现在看来:
为什么当起始位置设置为0.95时会有这么多红色?谢谢。
最佳答案
尝试
gradient.locations = [0.0, 0.5, 1.0]
从中间过渡到全绿色——现在你在左边有10%的全绿色。如果你想更快地过渡到绿色,那就试试
gradient.colors = [startColor, endColor, endColor, startColor]
gradient.locations = [0.0, 0.05, 0.95, 1.0]
关于swift - 渐变开始较早,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45694797/