2017-02-07 2 views
0

Je travaille avec swift 2.2 et j'ai affaire à un tel problème: J'ai un tableau d'objets UIBezier et j'ai besoin de créer un trait pour eux comme un seul chemin.Comment créer un chemin multiple à partir de plusieurs BezierPath

J'avais l'habitude d'avoir une fonction spéciale pour cela, mais les avantages de cette approche, qu'il a créé plusieurs couches. Cela va de pair avec mes exigences, comme je l'ai besoin d'une couche

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) 

    } 

Quelle est la meilleure façon de créer un chemin multiple d'un tableau de mes lignes Bézier? La première idée est de créer un cycle en boucle, mais je considère que ce n'est pas une façon propre.

+0

Vous pouvez créer un chemin unique en ajoutant tous les chemins Bézier, puis définir le résultat comme chemin CAShapeLayer. Plus d'infos: https://developer.apple.com/reference/uikit/uibezierpath/1624377-append – aleberguer

Répondre

3

Vous pouvez faire quelque chose comme ça (ce code est en terrain de jeu):

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 

voici à quoi il ressemble dans la cour:

playground exemple

Vous n'aurez une couche de cette façon