objective-c – 将多个CGPath合并在一起以创建一条路径

我使用几个CGRects和Ellipses做一个复杂的形状.一旦创建了路径,我就想打破这条路.当我敲击路径时,它会触发每个形状.有没有办法可以将每个形状合并成单个路径,所以笔画不相交?
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,CGRectMake((58 - 48) / 2,48,48));
CGPathAddEllipseInRect(aPath,thirds,thirds * 2,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];

UPDATE

我尝试添加路径在一起,我得到完全相同的结果

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

CGPathAddpath(aPath,CGPathCreateWithEllipseInRect(CGRectMake((58 - 48) / 2,48),nil));
CGPathAddpath(aPath,CGPathCreateWithEllipseInRect(CGRectMake(0,58),nil));

解决方法

如果您想要一个仅描述路径构图轮廓的路径,则需要对参与的形状执行布尔运算.
那里有一些很好的教程.
例如.:
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,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];

这将形成以下形状:

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...