gpt4 book ai didi

ios - CAGradientLayer 位置不正确

转载 作者:行者123 更新时间:2023-11-28 10:39:51 26 4
gpt4 key购买 nike

我关注了this用于设置渐变位置的帖子。

我的代码:

let color1 = UIColor(red: 0.0/255.0, green: 231.0/255.0, blue: 198.0/255.0, alpha: 0.9).cgColor as CGColor
let color2 = UIColor(red: 0.0/255.0, green: 198.0/255.0, blue: 255.0/255.0, alpha: 0.9).cgColor as CGColor
self.gradientLayer.colors = [color2, color1, color2]
self.gradientLayer.locations = [0.0, 0.10, 0.9]
self.gradientLayer.startPoint = CGPoint(x: 0, y: 0)
self.gradientLayer.endPoint = CGPoint(x: 1, y: 1)
viewMain.layer.insertSublayer(self.gradientLayer, at: 0)

我的输出:

enter image description here

预期输出:

enter image description here

最佳答案

为了便于说明,我更改了 startPoint/endPoint。此代码可在 XCode Playground 中使用。那不是最好的 Swifty 代码,而是专注于逻辑/解释而不是纯 Swift 代码

共享设置:

import Foundation
import UIKit

let viewsWidth: CGFloat = 300.0
let viewsHeight: CGFloat = 400.0

修复代码的方法是修改位置:

let viewMain: UIView = UIView(frame: CGRect(x: 0, y: 0, width: viewsWidth, height: viewsHeight))
let color1 = UIColor(red: 0.0/255.0, green: 231.0/255.0, blue: 198.0/255.0, alpha: 0.9).cgColor
let color2 = UIColor(red: 0.0/255.0, green: 198.0/255.0, blue: 255.0/255.0, alpha: 0.9).cgColor
var gradientLayer = CAGradientLayer.init()
gradientLayer.frame = viewMain.bounds
gradientLayer.colors = [color2, color1, color1, color2]
gradientLayer.locations = [0.0, 0.2, 0.8, 1.0]
gradientLayer.startPoint = CGPoint(x: 0, y: 0.5)
gradientLayer.endPoint = CGPoint(x: 1, y: 0.5)
viewMain.layer.insertSublayer(gradientLayer, at: 0)

viewMain //That's just for the Show Result action in Playground

主视图:

enter image description here

现在一些解释。

让我们先在每个位置画一条线:

let otherView = UIView(frame: viewMain.bounds)
if let locations = gradientLayer.locations, let colors = gradientLayer.colors {
for (index, aLocation) in locations.enumerated() {
let aLine = UIView(frame: CGRect(x: viewsWidth*CGFloat(aLocation.floatValue)-2,
y: 0,
width: 4,
height: viewsHeight))
aLine.backgroundColor = UIColor(cgColor: colors[index] as! CGColor)
otherView.addSubview(aLine)
}
}

otherView //That's just for the Show Result action in Playground

其他 View :

enter image description here

现在,让我们为每条线绘制一个渐变,但只有一条(其余颜色设置为白色以简化)。

if let locations = gradientLayer.locations, let colors = gradientLayer.colors {
let offset = 5
let bigView = UIView(frame: CGRect(x: 0,
y: 0,
width: viewsWidth * CGFloat(locations.count) + CGFloat(offset),
height: viewsHeight))

for (index, _) in locations.enumerated() {

let aGradientView = UIView(frame: CGRect(x: 0 + (viewsWidth + CGFloat(offset))*CGFloat(index),
y: 0,
width: viewsWidth,
height: viewsHeight))
let aGradientLayer = CAGradientLayer()
aGradientLayer.frame = aGradientView.bounds

var aGradientColors = [CGColor](repeating: UIColor.white.cgColor, count: colors.count)
aGradientColors[index] = colors[index] as! CGColor
aGradientLayer.colors = aGradientColors
aGradientLayer.locations = gradientLayer.locations
aGradientLayer.startPoint = CGPoint(x: 0.0, y: 0.5)
aGradientLayer.endPoint = CGPoint(x: 1.0, y: 0.5)
aGradientView.layer.insertSublayer(aGradientLayer, at: 0)

aGradientView.layer.borderColor = UIColor.black.cgColor
aGradientView.layer.borderWidth = 2.0
aGradientView.clipsToBounds = false
bigView.addSubview(aGradientView)
}

bigView //That's just for the Show Result action in Playground
}

大视野: enter image description here

所显示的插图不言自明。

在每条“线”(图表 2),当您想要应用渐变(图表 3)时,颜色会从该线变为下一条线。这就是为什么您的第一个位置没有给出预期结果的原因。您需要放置中间步骤。

关于ios - CAGradientLayer 位置不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51189592/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com