gpt4 book ai didi

arrays - 如何从多个 BezierPath 创建多个路径

转载 作者:搜寻专家 更新时间:2023-11-01 05:56:21 25 4
gpt4 key购买 nike

我正在使用 swift 2.2,我正在处理这样的问题:我有一组 UIBezier 对象,我需要为它们创建一个笔划作为一条路径。

我曾经有一个特殊的功能,但这种方法的优点是它创建了多个层。这符合我的要求,因为我需要一层

 func createStroke(line: UIBezierPath) {

self.currentPath = CAShapeLayer()
currentPath.path = line.CGPath
currentPath.strokeColor = UIColor.blackColor().CGColor
currentPath.fillColor = UIColor.clearColor().CGColor
currentPath.lineWidth = 1
self.view.layer.addSublayer(currentPath)

}

从贝塞尔曲线数组创建多路径的最佳方法是什么?第一个想法是创建一个 for 循环,但我认为这不是一个干净的方法。

最佳答案

你可以这样做(这是 playground 代码):

let myView = UIView(frame:CGRect(x: 100, y: 100, width: 250, height: 250))
myView.backgroundColor = UIColor.white
let myLayer = CAShapeLayer()

func createPath(i: Int) -> UIBezierPath {
let path = UIBezierPath()
path.move(to: CGPoint(x: 2*i, y: 9*i - 4))
path.addLine(to: CGPoint(x: 10 + 5*i, y: 20 + 4*i))

return path
}

func createStroke(line: UIBezierPath) {
myLayer.path = line.cgPath
myLayer.strokeColor = UIColor.black.cgColor
myLayer.fillColor = UIColor.black.cgColor
myLayer.lineWidth = 1
}

let paths = [createPath(i: 0), createPath(i: 15), createPath(i: 8), createPath(i: 10)]
let myPath = UIBezierPath()
for path in paths {
myPath.append(path) // THIS IS THE IMPORTANT PART
}
createStroke(line: myPath)
myView.layer.addSublayer(myLayer)

myView

这是它在 Playground 上的样子:

playground exemple

这样你只有一层

关于arrays - 如何从多个 BezierPath 创建多个路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42091599/

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