2016-11-23 68 views
0

下面的代碼:addArc(withCenter)關閉路徑

let size = CGSize(width: 200, height: 30) 
    let rect = CGRect(origin: .zero, size: size) 

    let path1 = UIBezierPath() 
    path1.move(to: CGPoint(x: 10, y: 5)) 
    path1.addLine(to: CGPoint(x: 180, y: 5)) 
    path1.addArc(withCenter: CGPoint(x: 180, y: 20), radius: 15, 
    startAngle: (3.14159/2), endAngle: (3 * 3.14159/2), clockwise: false) 

產生這樣的:

enter image description here

好吧,我失去的東西嗎?我不想關閉這條路。我從來不打電話給path1.close()。我想從弧的末端添加另一條直線,而不是從封閉的版本開始。基本上,我不希望半圈被關閉,我該怎麼做?

回答

1

你需要在-90度開始你的弧度並以+90度結束。你也需要改變方向。你需要做如下:

path1.addArc(withCenter: CGPoint(x: 180, y: 20), radius: 15, startAngle: -.pi/2, endAngle: .pi/2, clockwise: true) 

如果你想完成的形狀也應該是這樣的:

let path1 = UIBezierPath() 
path1.move(to: CGPoint(x: 10, y: 5)) 
path1.addLine(to: CGPoint(x: 180, y: 5)) 
path1.addArc(withCenter: CGPoint(x: 180, y: 20), radius: 15, startAngle: -.pi/2, endAngle: .pi/2, clockwise: true) 
path1.addLine(to: CGPoint(x: 10, y: 35)) 
path1.addArc(withCenter: CGPoint(x: 10, y: 20), radius: 15, startAngle: .pi/2, endAngle:-.pi/2 , clockwise: true) 

enter image description here

1

希望這將幫助你實現你的結果

let size = CGRect(origin: .zero, size: CGSize(width : 200 , height : 30)) 

     let path = UIBezierPath() 
     path.move(to: CGPoint(x: 10, y: 100)) 
     path.addLine(to: CGPoint(x: 180, y: 100)) 
     path.addArc(withCenter: CGPoint(x:180 , y: 85), radius: 15, startAngle: (3.14159/2), endAngle: (3 * 3.14159/2), clockwise: false) 

    path.addLine(to: CGPoint(x: 10, y: 70)) //y = radius * 2 

上面的代碼將在您的畫布中繪製此圖。 enter image description here