1

我正在尝试进入 Core Graphics。我试图从 2 个矩形中画出某个形状的那一刻。现在我想加入那些展位来使用渐变和颜色功能。我不确定是否有办法。这是一张图片: 这是一张图片

这是我的代码片段:

-(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;
}
4

2 回答 2

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.
于 2012-07-21T00:41:46.397 回答
1

你有一些选择:

  1. 构造一条新路径,形成两条不相交的独立路径的联合。Core Graphics 不会帮助您使用这种方法,它不提供布尔路径操作。
  2. 使用透明层绘制两个形状(一个接一个)。这将修复重叠区域,但不适用于渐变。
  3. 创建位图上下文以创建蒙版。将两个形状都绘制到蒙版中。然后在原始上下文中剪辑到蒙版,并在形状的边界框上绘制一个矩形。
于 2012-07-21T00:37:17.037 回答