10

CGPathCAShapeLayer.

我可以像这样修剪/减去路径吗?

在此处输入图像描述

4

2 回答 2

6

无需进行数学运算,而是可以在绘图时使用kCGBlendModeClear.

// Create the poly.
CGContextBeginPath(context);
CGContextMoveToPoint       (context, a_.x, a_.y);
CGContextAddLineToPoint    (context, b_.x, b_.y);
CGContextAddLineToPoint    (context, c_.x, c_.y);
CGContextAddLineToPoint    (context, d_.x, d_.y);
CGContextClosePath(context);

// Draw with the desired color.
CGContextSetFillColorWithColor(context, self.color.CGColor);
CGContextFillPath(context);

// Create a circle.
CGContextBeginPath(context);
CGContextAddEllipseInRect(context, (CGRect){
    b_.x - self.radius,
    b_.y - self.radius,
    self.radius * 2.0,
    self.radius * 2.0 });
CGContextClosePath(context);

// Draw with Clear (!) blend mode.
CGContextSetBlendMode(context, kCGBlendModeClear);
CGContextSetFillColorWithColor(context, self.color.CGColor);
CGContextFillPath(context);
于 2013-10-14T17:55:09.913 回答
0

基于Geri 的回答,使用Swift 5

context.addPath(path1) // your original CGPath
context.setFillColor(color1) // your color
context.fillPath()
        
context.addPath(path2) // your clearing CGPath
context.setBlendMode(.clear) // draw with clear mode
context.fillPath()
于 2021-07-10T18:48:48.530 回答