gpt4 book ai didi

iphone - CAShapeLayer : Finding the intersected area of 3 circles

转载 作者:行者123 更新时间:2023-11-29 10:58:05 26 4
gpt4 key购买 nike

我有两个问题:

  1. 在 CALayer 中有什么方法可以知道两个圆是否相交?
  2. 三个圆圈相交,有什么办法可以突出相交的区域?

我正在使用 CAShapeLayer 绘制我的圆圈:

- (CAShapeLayer *)circleForRadius:(CGFloat)iRadius withColor:(CGColorRef)iColor andDashPattern:(BOOL)isDashPattern {
CAShapeLayer *aSignalcircle = [CAShapeLayer layer];
aSignalcircle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0 * iRadius, 2.0 * iRadius) cornerRadius:iRadius].CGPath;
aSignalcircle.position = CGPointMake(0.0 - iRadius, 0.0 - iRadius);
aSignalcircle.fillColor = [UIColor clearColor].CGColor;
aSignalcircle.strokeColor = iColor;
aSignalcircle.lineWidth = kPSSignalStrokeWidth;

if (self.enableShadow) {
aSignalcircle.shadowColor = [UIColor blackColor].CGColor;
aSignalcircle.shadowOpacity = 0.5;
aSignalcircle.shadowOffset = CGSizeMake(0, 0.25);
aSignalcircle.shadowRadius = 0.5;
}

if (isDashPattern) {
aSignalcircle.lineDashPattern = @[@1, @1];
}

return aSignalcircle;

我尝试使用以下代码剪切上下文路径,但输出未按预期出现:

- (void)drawRect:(CGRect)rect
{
CGFloat iRadius = 20.0;
// Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
CAShapeLayer *aSignalcircle = [CAShapeLayer layer];
aSignalcircle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0 * iRadius, 2.0 * iRadius) cornerRadius:iRadius].CGPath;
aSignalcircle.position = CGPointMake(10.0 , 10.0);
aSignalcircle.fillColor = [UIColor clearColor].CGColor;
aSignalcircle.strokeColor = [UIColor redColor].CGColor;
aSignalcircle.lineWidth = 1.0;
CGContextAddPath(context, aSignalcircle.path);
CGContextClip(context);

CAShapeLayer *aSignalcircle1 = [CAShapeLayer layer];
aSignalcircle1.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0 * iRadius, 2.0 * iRadius) cornerRadius:iRadius].CGPath;
aSignalcircle1.position = CGPointMake(30.0 , 10.0);
aSignalcircle1.fillColor = [UIColor clearColor].CGColor;
aSignalcircle1.strokeColor = [UIColor redColor].CGColor;
aSignalcircle1.lineWidth = 1.0;
CGContextAddPath(context, aSignalcircle1.path);
CGContextClip(context);


CAShapeLayer *aSignalcircle2 = [CAShapeLayer layer];
aSignalcircle2.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0 * iRadius, 2.0 * iRadius) cornerRadius:iRadius].CGPath;
aSignalcircle2.position = CGPointMake(25.0 , 30.0);
aSignalcircle2.fillColor = [UIColor clearColor].CGColor;
aSignalcircle2.strokeColor = [UIColor redColor].CGColor;
aSignalcircle2.lineWidth = 1.0;
CGContextAddPath(context, aSignalcircle2.path);

[[UIColor grayColor] set];
CGContextFillPath(context);

// Temp addition to see how the circles are looking on screen
[self.layer addSublayer:aSignalcircle];
[self.layer addSublayer:aSignalcircle1];
[self.layer addSublayer:aSignalcircle2];
}

我的输出看起来像:

enter image description here

最佳答案

对于第二个问题,可以使用如下代码高亮相交区域:

// Add first path and clip
CGContextAddPath(context, circle1.path);
CGContextClip(context);

// Add second path and clip
CGContextAddPath(context, circle2.path);
CGContextClip(context);

// Add third path and draw it. This means it'll be drawn within the clipping area
CGContextAddPath(context, circle3.path);
[[UIColor redColor] set];
CGContextFillPath(context);

关于iphone - CAShapeLayer : Finding the intersected area of 3 circles,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17307162/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com