我有两个UIBezierPath
s,一个表示图像部分的多边形,另一个是要在其上绘制的路径。
我需要找到它们之间的交集,以便只有该交集区域内的点才会被着色。
有没有方法在UIBezierPath
可以找到两条路径之间的交点 - 或新路径?
我不知道获取作为两条路径的交集的新路径的方法,但您可以通过使用每个路径的剪切属性来填充或以其他方式绘制交集。
在此示例中,有两条路径:正方形和圆形:
let path1 = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 100, height: 100))
let path2 = UIBezierPath(ovalIn: CGRect(x:50, y:50, width: 100, height: 100))
我制作了一个渲染器来绘制这些,但你可以在drawRect
或任何地方:
let renderer = UIGraphicsImageRenderer(bounds: CGRect(x: 0, y: 0, width: 200, height: 200))
let image = renderer.image {
context in
// You wouldn't actually stroke the paths, this is just to illustrate where they are
UIColor.gray.setStroke()
path1.stroke()
path2.stroke()
// You would just do this part
path1.addClip()
path2.addClip()
UIColor.red.setFill()
context.fill(context.format.bounds)
}
生成的图像如下所示(为了清楚起见,我已经描画了每个路径,如代码注释中所示,实际上您只需执行填充部分):
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)