2017-02-07 3 views
0

스위프트 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-loop 사이클을 만드는 것이지만, 그것은 깨끗한 방법이 아니라고 생각합니다.

+0

모든 베 지어 경로를 추가하여 단일 경로를 만든 다음 결과를 CAShapeLayer 경로로 설정할 수 있습니다. 추가 정보 : https://developer.apple.com/reference/uikit/uibezierpath/1624377-append – aleberguer

답변

3

이 뭔가를 (이 놀이터 코드) 수행 할 수 있습니다

playground exemple

당신은 하나 개의 계층이 방법이있을 것이다 :

여기
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 

이가 놀이터에서 모습입니다

관련 문제