objective-c - 将多个 CGPath 合并在一起以形成一条路径

标签 objective-c cgpath

我正在使用一些 CGRect 和椭圆制作一个复杂的形状。一旦它被创建,我想抚摸那条路径。当我抚摸路径时,它抚摸每个形状。有没有一种方法可以将每个形状合并到一条路径中,这样笔划就不会相交?

enter image description here

int thirds = self.height / 3;

CGPathRef aPath = CGPathCreateMutable();

CGRect rectangle = CGRectMake((58 - 10) / 2, 46, 10, self.height - 58);
CGPathAddRect(aPath, nil, rectangle);

CGPathAddEllipseInRect(aPath, nil, CGRectMake((58 - 48) / 2, 0, 48, 48));
CGPathAddEllipseInRect(aPath, nil, CGRectMake((58 - 48) / 2, thirds, 48, 48));
CGPathAddEllipseInRect(aPath, nil, CGRectMake((58 - 48) / 2, thirds * 2, 48, 48));
CGPathAddEllipseInRect(aPath, nil, CGRectMake(0, self.height - 58, 58, 58));
CGPathCloseSubpath(aPath);
CGPathRef other = CGPathCreateCopy(aPath);

CAShapeLayer *square = [CAShapeLayer layer];
square.fillColor = [UIColor colorWithRed:36/255.0f green:56/255.0f blue:82/255.0f alpha:1.0f].CGColor;
square.strokeColor = [UIColor colorWithRed:50/255.0f green:70/255.0f blue:96/255.0f alpha:1.0f].CGColor;
square.path = other;
[self.layer addSublayer:square];

更新

我试着把路径加在一起,我得到了完全相同的结果

CGPathAddPath(aPath, nil, CGPathCreateWithRect(rectangle, nil));

CGPathAddPath(aPath, nil, CGPathCreateWithEllipseInRect(CGRectMake((58 - 48) / 2, 0, 48, 48), nil));
CGPathAddPath(aPath, nil, CGPathCreateWithEllipseInRect(CGRectMake((58 - 48) / 2, thirds, 48, 48), nil));
CGPathAddPath(aPath, nil, CGPathCreateWithEllipseInRect(CGRectMake((58 - 48) / 2, thirds * 2, 48, 48), nil));
CGPathAddPath(aPath, nil, CGPathCreateWithEllipseInRect(CGRectMake(0, self.height - 58, 58, 58), nil));

最佳答案

如果您想要一条仅描述路径组合轮廓的路径,则需要对参与的形状执行 bool 运算。
那里有一些很好的教程。
例如。: http://losingfight.com/blog/2011/07/07/how-to-implement-boolean-operations-on-bezier-paths-part-1/

如果你只是想实现轮廓的视觉外观,你可以绘制 2 个形状图层:

  • 一个填充通过 CGPathCreateCopyByStrokingPath 获得的路径和
  • 在顶部填充您的原始路径

使用您发布的代码中的形状:

int thirds = self.height / 3;

CGMutablePathRef aPath = CGPathCreateMutable();

CGRect rectangle = CGRectMake((58 - 10) / 2, 46, 10, self.height - 58);
CGPathAddRect(aPath, nil, rectangle);

CGPathAddEllipseInRect(aPath, nil, CGRectMake((58 - 48) / 2, 0, 48, 48));
CGPathAddEllipseInRect(aPath, nil, CGRectMake((58 - 48) / 2, thirds, 48, 48));
CGPathAddEllipseInRect(aPath, nil, CGRectMake((58 - 48) / 2, thirds * 2, 48, 48));
CGPathAddEllipseInRect(aPath, nil, CGRectMake(0, self.height - 58, 58, 58));
CGPathCloseSubpath(aPath);

CGPathRef other = CGPathCreateCopyByStrokingPath(aPath, NULL, 12.0, kCGLineCapRound, kCGLineJoinRound, 1.0);
CAShapeLayer *square = [CAShapeLayer layer];
square.fillColor = [UIColor colorWithRed:36/255.0f green:56/255.0f blue:82/255.0f alpha:1.0f].CGColor;
square.path = other;
[self.view.layer addSublayer:square];

CAShapeLayer *square2 = [CAShapeLayer layer];
square2.fillColor = [UIColor colorWithRed:50/255.0f green:70/255.0f blue:96/255.0f alpha:1.0f].CGColor;
square2.path = aPath;
[self.view.layer addSublayer:square2];

这将绘制以下形状:
enter image description here

关于objective-c - 将多个 CGPath 合并在一起以形成一条路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17539397/

相关文章:

objective-c - 使用异步函数和 Reactive-Cocoa 转换值数组

ios - 在后台检查服务器端应用程序通知,在 Swift 中

ios - CGRect 上的 Swift 3 CGAffineTransform

uikit - CAShapeLayer 缓慢的用户交互

ios - 如何在 SpriteKit 中为带有物理体的迷宫创建路径

ios - 如何在未端到端连接的 quartz 上下文中绘制线条?

iOS PUSH 通知类型选项?警报与横幅?

objective-c - 解决涉及枚举的 Swift.h 和 Bridging-Header.h 循环引用

objective-c - 比较 NSCFString 和 NSCFBoolean

swift - 如何为 CGpath 数组的绘制设置动画?