我有两个问题:
- 在 CALayer 中有什么方法可以知道两个圆是否相交?
- 三个圆相交,有什么办法可以突出相交的区域?
我正在使用 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];
}
我的输出看起来像: