2012-07-20 13 views
1

我正在嘗試進入Core Graphics。 我正試圖從2個方面中抽出一定的形狀。現在我想加入這些展位,使用漸變和顏色功能。我不確定是否有辦法。 這裏是一個圖片: Here is a picture在覈心圖形中加入兩個Rects

這裏是我的代碼片段:

-(void)drawRoundedRect:(CGRect)rect 
{ 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 

    CGRect frame1 = CGRectMake(self.bounds.origin.x, self.bounds.origin.y+45, self.bounds.size.width, self.bounds.size.height-45); 

    CGRect frame2 = CGRectMake(self.bounds.size.width-80, self.bounds.origin.y+25, 80, 50); 


    CGPathRef roundedRectPath1 = [self newPathForRoundedRect:frame1 radius:15]; 
    CGPathRef roundedRectPath2 = [self newPathForRoundedRect:frame2 radius:11]; 
    CGContextSetRGBFillColor(ctx, 200, 200, 200, 0.5); 


    CGContextAddPath(ctx, roundedRectPath1); 
    CGContextFillPath(ctx); 

    CGPathRelease(roundedRectPath1); 
    CGContextAddPath(ctx, roundedRectPath2); 
    CGContextFillPath(ctx); 
    CGPathRelease(roundedRectPath2); 


} 

-(CGPathRef) newPathForRoundedRect:(CGRect)rect radius:(CGFloat)radius 
{ 

    CGMutablePathRef retPath = CGPathCreateMutable(); 

    CGRect innerRect = CGRectInset(rect, radius, radius); 

    CGFloat inside_right = innerRect.origin.x + innerRect.size.width; 
    CGFloat outside_right = rect.origin.x + rect.size.width; 
    CGFloat inside_bottom = innerRect.origin.y + innerRect.size.height; 
    CGFloat outside_bottom = rect.origin.y + rect.size.height; 

    CGFloat inside_top = innerRect.origin.y; 
    CGFloat outside_top = rect.origin.y; 
    CGFloat outside_left = rect.origin.x; 

    CGPathMoveToPoint(retPath, NULL, innerRect.origin.x, outside_top); 

    CGPathAddLineToPoint(retPath, NULL, inside_right, outside_top); 
    CGPathAddArcToPoint(retPath, NULL, outside_right, outside_top, outside_right, inside_top, radius); 
    CGPathAddLineToPoint(retPath, NULL, outside_right, inside_bottom); 
    CGPathAddArcToPoint(retPath, NULL, outside_right, outside_bottom, inside_right, outside_bottom, radius); 

    CGPathAddLineToPoint(retPath, NULL, innerRect.origin.x, outside_bottom); 
    CGPathAddArcToPoint(retPath, NULL, outside_left, outside_bottom, outside_left, inside_bottom, radius); 
    CGPathAddLineToPoint(retPath, NULL, outside_left, inside_top); 
    CGPathAddArcToPoint(retPath, NULL, outside_left, outside_top, innerRect.origin.x, outside_top, radius); 

    CGPathCloseSubpath(retPath); 

    return retPath; 
} 
+0

CGContextAddPath是否工作?文檔說它將給定路徑的所有子路徑添加到當前路徑。所以在執行FillPath之前調用CGContextAddPath兩次? – webjprgm 2012-07-20 23:53:51

回答

2

你在這裏工作太多了。只需使用UIBezierPath的+bezierPathWithRoundedRect:cornerRadius:– appendPath:方法即可。

CGRect rect1, rect2; 
CGFloat radius; 

    // fill in the values you want for the rects and the radius 
UIBezierPath *result = 
    [UIBezierPath bezierPathWithRoundedRect: rect1 cornerRadius:radius]; 
[result appendPath: 
    [UIBezierPath bezierPathWithRoundedRect: rect2 cornerRadius:radius]; 

// result is now a path comprising both of the roundrects. You can fill it with a gradient like any other path. 
1

你有一些選擇:

  1. 構造形成兩個獨立的路徑的工會沒有新路徑交叉點。核心圖形不會幫助你採用這種方法,它不提供布爾型路徑操作。
  2. 使用透明圖層繪製兩個形狀(一個接一個)。這將修復重疊區域,但它不適用於漸變。
  3. 創建位圖上下文以創建掩碼。在掩模中繪製兩種形狀。然後在原始上下文中將其裁剪到面罩上,然後在您的形狀的邊框上繪製矩形。