2011-12-09 57 views
1

讓石英畫出我想要的形狀有些麻煩。基本上我要一個形狀像這樣:帶三角形的IOS石英氣泡

bubble view

我能得到圓潤的泡沫,但是當我嘗試在三角形添加它出錯。以下是我通常會得到:

enter image description here

感謝您的時間。

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGRect currentFrame = self.bounds; 

CGContextSetLineJoin(context, kCGLineJoinRound); 
CGContextSetLineWidth(context, self.BorderWidth); 
CGContextSetStrokeColorWithColor(context, self.BorderColor.CGColor); 
CGContextSetFillColorWithColor(context, self.FillColor.CGColor); 


float pad = BorderWidth + 0.5f; 
float width = currentFrame.size.width - BorderWidth - 0.5f; 
float height = currentFrame.size.height - BorderWidth - 0.5f; 
float rounding = BorderRadius - BorderWidth; 

CGContextMoveToPoint(context,pad + TriangleSize.width, pad); 

//top 
CGContextAddArcToPoint(context, 
         width, 
         pad, 
         width, 
         height, 
         rounding); 
//right 
CGContextAddArcToPoint(context, 
         width, 
         height, 
         round(width/2.0f), 
         height, 
         rounding); 
//bottom 
CGContextAddArcToPoint(context, 
         TriangleSize.width + pad, 
         height, 
         pad, 
         pad , 
         rounding); 

//left 
CGContextAddArcToPoint(context, 
         TriangleSize.width, 
         pad + TriangleSize.height*3, 
         width, 
         pad, 
         0); 

CGContextAddLineToPoint(context,-TriangleSize.width - pad,TriangleSize.height); 
CGContextAddLineToPoint(context,pad + TriangleSize.width, pad + TriangleSize.height); 

CGContextAddArcToPoint(context, 
         pad + TriangleSize.width, 
         pad - TriangleSize.height, 
         width, 
         height, 
         rounding); 

CGContextClosePath(context); 
CGContextDrawPath(context, kCGPathFillStroke); 

回答

2

看起來像所有我需要做的是發佈一個問題來弄清楚。 :)反正這裏是我用得到它的工作,如果有人發現了這個代碼後的道路:

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGRect currentFrame = self.bounds; 

CGContextSetLineJoin(context, kCGLineJoinRound); 
CGContextSetLineWidth(context, self.BorderWidth); 
CGContextSetStrokeColorWithColor(context, self.BorderColor.CGColor); 
CGContextSetFillColorWithColor(context, self.FillColor.CGColor); 


float pad = BorderWidth + 0.5f; 
float width = currentFrame.size.width - BorderWidth - 0.5f; 
float height = currentFrame.size.height - BorderWidth - 0.5f; 
float rounding = BorderRadius - BorderWidth; 
float pos = (height/3); //height/2 //setting this to a third as I want the arrow to be a bit higher than the middle 

CGContextMoveToPoint(context,pad*3 + TriangleSize.width, pad); 
//top 
CGContextAddArcToPoint(context, 
         width, 
         pad, 
         width, 
         height, 
         rounding); 
//right 
CGContextAddArcToPoint(context, 
         width, 
         height, 
         round(width/2.0f), 
         height, 
         rounding); 
//bottom 
CGContextAddArcToPoint(context, 
         pad + TriangleSize.width, 
         height, 
         pad, 
         pad , 
         rounding); 

CGContextAddLineToPoint(context, TriangleSize.width,pos + TriangleSize.height); 
CGContextAddLineToPoint(context, 0,pos); 
CGContextAddLineToPoint(context, TriangleSize.width,pos - TriangleSize.height); 
//left 
CGContextAddArcToPoint(context, 
         pad + TriangleSize.width, 
         pad, 
         width, 
         pad, 
         rounding); 


CGContextClosePath(context); 
CGContextDrawPath(context, kCGPathFillStroke); 

// Draw a clipping path for the fill 
CGContextBeginPath(context); 
CGContextMoveToPoint(context,pad*3 + TriangleSize.width, pad); 
CGContextAddArcToPoint(context, width, pad, width, height, rounding); 
CGContextAddArcToPoint(context, width, height, round(width/2.0f), height,rounding); 
CGContextAddArcToPoint(context, pad + TriangleSize.width,height, pad, pad, rounding); 
CGContextAddLineToPoint(context, TriangleSize.width,pos + TriangleSize.height); 
CGContextAddLineToPoint(context, 0,pos); 
CGContextAddLineToPoint(context, TriangleSize.width,pos - TriangleSize.height); 
CGContextAddArcToPoint(context, pad + TriangleSize.width,pad,width, pad, rounding); 

CGContextClosePath(context); 
CGContextClip(context); 
+0

你在設置boarderwidth,boarderradius和三角形尺寸 – Jarod

+0

看到這一點:https://github.com /daltoniam/GPLib-iOS/blob/master/GPLib/Views/GPBubbleView.m,但我使用BorderRadius = 8; BorderWidth = 0.5; TriangleSize = CGSizeMake(8,8); – daltoniam