gpt4 book ai didi

ios - CAShapeLayer 形状不显示

转载 作者:可可西里 更新时间:2023-11-01 02:26:04 25 4
gpt4 key购买 nike

我的 CAShapeLayer 没有显示。我正在尝试通过循环 CGPoints 数组来创建形状。我需要使用数组或字典,以便稍后添加不同的 CGPoint 集来创建更复杂的形状。

var shapeLayer = CAShapeLayer()
var color = UIColor.redColor()
var openPath = UIBezierPath()
var closedPath = UIBezierPath()
let points = [CGPoint(x: 30, y: 30), CGPoint(x: 90, y: 30), CGPoint(x: 90, y: 90), CGPoint(x: 90, y: 30), CGPoint(x: 30, y: 30)]
var firstTime = false

override func viewDidLoad()
{
super.viewDidLoad()
setOpenPath()
}

func setOpenPath()
{
for number in points
{
if number != points.first
{
openPath.addLineToPoint(number)
}

if number == points.first
{
if firstTime == false
{
openPath.moveToPoint(number)
}
if firstTime == true
{
openPath.addLineToPoint(number)
setClosePath()
}
firstTime = true
}
}
}

func setClosePath()
{
firstTime = false
closedPath.CGPath = CGPathCreateMutableCopy(openPath.CGPath)
closedPath.closePath()
setShapeLayer()
}

func setShapeLayer()
{
shapeLayer.path = closedPath.CGPath
shapeLayer.fillColor = UIColor.greenColor().CGColor
shapeLayer.fillRule = kCAFillRuleNonZero
self.view.layer.addSublayer(shapeLayer)
}

如果我用下面的代码替换 setOpenPath() 中的代码,脚本可以正常工作。

openPath.moveToPoint(CGPoint(x: 30, y: 30))
openPath.addLineToPoint(CGPoint(x: 90, y: 30))
openPath.addLineToPoint(CGPoint(x: 90, y: 90))
openPath.addLineToPoint(CGPoint(x: 30, y: 90))
openPath.addLineToPoint(CGPoint(x: 30, y: 30))
setClosePath()

最佳答案

points 数组中的点乱七八糟。与下面的代码相比,索引 3 处的元素是错误的。它不会产生闭合路径。正确的元素是

let points = [CGPoint(x: 30, y: 30), CGPoint(x: 90, y: 30), CGPoint(x: 90, y: 90), CGPoint(x: 30, y: 90), CGPoint(x: 30, y: 30)]

也使用其他条件并像这样简化你的循环

func setOpenPath()
{
firstTime = true
for number in points
{
if number != points.first
{
openPath.addLineToPoint(number)
}
else
{
if firstTime
{
openPath.moveToPoint(number)
}
else
{
openPath.addLineToPoint(number)
setClosePath()
}
firstTime = false
}
}
}

关于ios - CAShapeLayer 形状不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27994898/

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