2015-07-03 63 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

相关问题