1

我有两个矩形(两个封闭的 sub cgpaths)。矩形 B 很小,并且存在于矩形 A 内。其中的所有边。是否有直接的方法来填充矩形 B 外部的颜色区域。

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

A - 紫色 B ​​- 黄色

先画A再画B 先画B,再画A

所以我试着画A,然后画B。我希望B是清晰的颜色(现在我放黄色)但后来我看到A的紫色......

我找到了给出 AnB 的 CGRectIntersection 方法和给出 AuB 的 CGRectUnion 方法。有没有一种方法可以给出 AuB - AnB 的其余区域?

4

5 回答 5

2

You are using a CAShapeLayer. If your fillColor and your strokeColor are both opaque (alpha 1.0), you can simply set the layer's backgroundColor to fill those pixels that are within the layer's bounds but outside of its stroked and filled path.

My test code:

@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

Result:

screen shot of test app

于 2013-06-08T02:12:52.210 回答
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;
    }

问候。

于 2015-07-24T06:33:10.673 回答
0

编辑,找到这段代码的来源

鉴于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);
于 2013-06-08T02:03:50.860 回答
0

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

  1. 将形状 B 剪辑到上下文(-addClip应该这样做)
  2. CGContextClearRect边界
  3. 填充形状 A
于 2013-05-15T19:26:03.257 回答
-1

kCAFillRuleEvenOdd

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

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

在 CAShapeLayer.h 中声明。

参考:CAShapeLayer 类参考

于 2013-05-15T19:17:25.250 回答