iOS中的外部填充颜色

时间:2013-05-15 18:22:16

标签: iphone ios cgpath cashapelayer

我有两个矩形(两个闭合的子cgpaths)。 矩形B很小,存在于矩形A内。所有边都在其中。 有没有直接的方法来填充矩形B外部的颜色区域。

CAShapeLayer fillExternalColor类似的东西?如果不是直接的方式,如何以编程方式执行此操作?

A - 紫色 B - 黄色

Drawing A and then drawing B Drawing B and then drawing A

所以我尝试绘制A然后绘制B.我希望B具有清晰的颜色(现在我放黄色)然后我看到A的紫色。

我找到了CGRectIntersection方法,它给出了给出AuB的AnB和CGRectUnion方法。 是否有一种方法可以提供AuB-AnB的剩余区域?

5 个答案:

答案 0 :(得分:2)

您正在使用CAShapeLayer。如果您的fillColorstrokeColor都是不透明的(alpha 1.0),则只需设置图层的backgroundColor即可填充图层范围内但在其描边和填充范围之外的像素路径。

我的测试代码:

@implementation ViewController {
    CAShapeLayer *layer;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    layer = [CAShapeLayer layer];
    layer.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 250, 250)].CGPath;
    layer.fillColor = [UIColor yellowColor].CGColor;
    layer.strokeColor = [UIColor whiteColor].CGColor;
    layer.lineWidth = 4;
    layer.backgroundColor = [UIColor purpleColor].CGColor;
    [self.view.layer addSublayer:layer];
}

- (void)viewDidLayoutSubviews {
    layer.frame = self.view.bounds;
}

@end

结果:

screen shot of test app

答案 1 :(得分:1)

我想在UIImage上添加带有蒙版外部的红色边框内部矩形。

我来到了这个页面。以下代码使用CGContextEOFillPath可以对像我这样的人有所帮助。 (有些代码是从其他页面收集的。)

    -(UIImage ) imageByDrawingBorderRectOnImage:(UIImage )image theRect:(CGRect)theRect
    {
        // begin a graphics context of sufficient size
        UIGraphicsBeginImageContext(image.size);

        // draw original image into the context
        [image drawAtPoint:CGPointZero];

        // get the context for CoreGraphics
        CGContextRef ctx = UIGraphicsGetCurrentContext();

        // set stroking color and to draw rect
        [[UIColor redColor] setStroke];

        // drawing with a red stroke color
        CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);

        // the line width to 3
        CGContextSetLineWidth(ctx, 3.0);

        // Add Stroke Rectangle,
        CGContextStrokeRect(ctx, theRect);

        // Now draw fill outside part with partial alpha gray color
        // drawing with a gray stroke color
        CGMutablePathRef aPath = CGPathCreateMutable();
        // outer rectangle
        CGRect rectangle = CGRectMake( 0, 0, image.size.width, image.size.height);
        CGPathAddRect(aPath, nil, rectangle);
        // innter rectangle
        CGPathAddRect(aPath, nil, theRect);
        // set gray transparent color
        CGContextSetFillColorWithColor(ctx, [UIColor colorWithRed:0.75 green:0.75 blue:0.75 alpha:0.5].CGColor);
        // add the path to Context
        CGContextAddPath(ctx, aPath);
        // This method uses Even-Odd Method to draw in outer rectangle
        CGContextEOFillPath(ctx);

        // make image out of bitmap context
        UIImage *retImage = UIGraphicsGetImageFromCurrentImageContext();

        // free the context
        UIGraphicsEndImageContext();

        return retImage;
    }

问候。

答案 2 :(得分:0)

假设A是较大的形状,B是较小的内部形状,而边界指的是包含A的矩形(可能是视图的边界)

  1. 将形状B剪辑到上下文(-addClip应该这样做)
  2. CGContextClearRect界限
  3. 填充形状A

答案 3 :(得分:0)

修改,找到source of this code

鉴于HoleView : UIView,您必须设置:

self.opaque = NO;
self.backgroudColor = [UIColor clearColor];

并在drawRect:

[[UIColor purpleColor] setFill];
UIRectFill(A);

CGRect gapRectIntersection = CGRectIntersection(B, A);    
[[UIColor clearColor] setFill];
UIRectFill(gapRectIntersection);

答案 4 :(得分:-1)

<强> kCAFillRuleEvenOdd

指定奇偶绕组规则。计算路径交叉的总数。如果交叉的数量是偶数,则该点在路径之外。如果交叉的数量是奇数,则该点在路径内,并且应该填充包含它的区域。

在OS X v10.6及更高版本中可用。

在CAShapeLayer.h中声明。

请参阅:CAShapeLayer Class reference