这是我的代码:
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时,为什么会出现这么多红色?感谢。
答案 0 :(得分:1)
尝试
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]