Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何从多个BezierPath创建多路径_Arrays_Swift_Uibezierpath_Cashapelayer_Bezier - Fatal编程技术网

Arrays 如何从多个BezierPath创建多路径

Arrays 如何从多个BezierPath创建多路径,arrays,swift,uibezierpath,cashapelayer,bezier,Arrays,Swift,Uibezierpath,Cashapelayer,Bezier,我正在使用swift 2.2,我正在处理这样的问题: 我有一个UIBezier对象数组,我需要为它们创建一个笔划作为一条路径 我以前有一个特殊的功能,但是这种方法的优点是,它创建了几个层。 这符合我的要求,因为我需要一层 func createStroke(line: UIBezierPath) { self.currentPath = CAShapeLayer() currentPath.path = line.CGPath currentP

我正在使用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循环循环,但我认为它不是一个干净的方式。

你可以做这样的事情(这是游乐场代码):

下面是它在操场上的样子:


这样您将只有一个层

您可以这样做(这是游乐场代码):

下面是它在操场上的样子:


这样您将只有一个层

您可以通过附加所有bezier路径来创建单个路径,然后将结果设置为CAShapeLayer路径。更多信息:您可以通过附加所有bezier路径来创建单个路径,然后将结果设置为CAShapeLayer路径。更多信息:
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