2015-07-03 59 views
9

我設法創建圓角,但我有與第一圓角麻煩(右下)如何將圓角添加到UIBezierPath自定義矩形?

問:

  • 我可以的(moveToPoint)前加一個(addArcWithCenter)方法方法 ?
  • 我該如何擺脫矩形開頭的直線(右下角)?

這裏是我的自定義的矩形和一個屏幕截圖代碼:

let path = UIBezierPath() 
path.moveToPoint(CGPoint(x: 300, y: 0)) 
path.addArcWithCenter(CGPoint(x: 300-10, y: 50), radius: 10 , startAngle: 0 , endAngle: CGFloat(M_PI/2) , clockwise: true) //1st rounded corner 
path.addArcWithCenter(CGPoint(x: 200, y: 50), radius:10, startAngle: CGFloat(2 * M_PI/3), endAngle:CGFloat(M_PI) , clockwise: true)// 2rd rounded corner 
path.addArcWithCenter(CGPoint(x: 200, y: 10), radius:10, startAngle: CGFloat(M_PI), endAngle:CGFloat(3 * M_PI/2), clockwise: true)// 3rd rounded corner 
// little triangle at the bottom 
path.addLineToPoint(CGPoint(x:240 , y:0)) 
path.addLineToPoint(CGPoint(x: 245, y: -10)) 
path.addLineToPoint(CGPoint(x:250, y: 0)) 
path.addArcWithCenter(CGPoint(x: 290, y: 10), radius: 10, startAngle: CGFloat(3 * M_PI/2), endAngle: CGFloat(2 * M_PI), clockwise: true) 
path.closePath() 

enter image description here

+0

那麼什麼是休息你實際上得到同樣的效果。 – NSDeveloper

回答

5

沒關係,我居然找到了解決辦法。

而是用直線啓動代碼:

path.moveToPoint(CGPoint(x: 300, y: 0)) 

我,而不是用一個弧(右上)開始:

path.addArcWithCenter(CGPoint(x: 300-10, y: 50), radius: 10 , startAngle: 0 , endAngle: CGFloat(M_PI/2) , clockwise: true) //1st rounded corner 

,並通過這樣做,我有四個圓角我只需要在代碼末尾添加一條直線:

path.closePath() 

這裏是鱈魚e和截圖:

let path = UIBezierPath() 
path.addArcWithCenter(CGPoint(x: 300-10, y: 50), radius: 10 , startAngle: 0 , endAngle: CGFloat(M_PI/2) , clockwise: true) //1st rounded corner 
path.addArcWithCenter(CGPoint(x: 200, y: 50), radius:10, startAngle: CGFloat(2 * M_PI/3), endAngle:CGFloat(M_PI) , clockwise: true)// 2rd rounded corner 
path.addArcWithCenter(CGPoint(x: 200, y: 10), radius:10, startAngle: CGFloat(M_PI), endAngle:CGFloat(3 * M_PI/2), clockwise: true)// 3rd rounded corner 
// little triangle 
path.addLineToPoint(CGPoint(x:240 , y:0)) 
path.addLineToPoint(CGPoint(x: 245, y: -10)) 
path.addLineToPoint(CGPoint(x:250, y: 0)) 
path.addArcWithCenter(CGPoint(x: 290, y: 10), radius: 10, startAngle: CGFloat(3 * M_PI/2), endAngle: CGFloat(2 * M_PI), clockwise: true) 
path.addLineToPoint(CGPoint(x:300 , y:50)) 
path.closePath() 

enter image description here

+1

很高興你找到它。這實際上是我所描述的。 :D – Fogmeister

+1

爲了改善這一點,你應該把半徑,高度和寬度都變成變量。通過這種方式,您可以通過更改一個值來製作任意大小的泡泡並具有任何角落半徑。 – Fogmeister

+0

我應該把x和y放在變量中嗎?但是它們不同於一行代碼與另一行代碼。你能解釋更多嗎?謝謝 – AziCode

1

您無法自動做到這一點。您必須縮短線條,然後使用您希望轉角半徑所在半徑的弧線。

所以。而不是向x添加一行,然後將該行添加到x-radius,y。 然後添加弧線。然後下一行從x,y + radius開始。

+0

你能告訴我一個例子嗎? – AziCode

+1

@AziCode明天我可以。現在是凌晨1點30分了。 :-) – Fogmeister

14

我認爲你所做的事過於複雜。 UIBezierPath給你UIBezierPath(roundedRect:)爲什麼不使用它?描邊圓角矩形;擦掉你要放小三角形的地方;添加三角形;填充複合路徑;並撫摸缺失的三角形兩側。這樣的(這僅僅是一些代碼,我碰巧有躺在附近 - 你應該改變數以適應當然你的形狀,):

let con = UIGraphicsGetCurrentContext() 
CGContextTranslateCTM(con, 10, 10) 
UIColor.blueColor().setStroke() 
UIColor.blueColor().colorWithAlphaComponent(0.4).setFill() 
let p = UIBezierPath(roundedRect: CGRectMake(0,0,250,180), cornerRadius: 10) 
p.stroke() 
CGContextClearRect(con, CGRectMake(20,170,10,11)) 
let pts = [ 
    CGPointMake(20,180), CGPointMake(20,200), 
    CGPointMake(20,200), CGPointMake(30,180) 
] 
p.moveToPoint(pts[0]) 
p.addLineToPoint(pts[1]) 
p.addLineToPoint(pts[3]) 
p.fill() 
CGContextStrokeLineSegments(con, pts, 4) 

enter image description here

相關問題