Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift UIBezierPath-为单个路径绘制多个矩形_Swift_Uibezierpath - Fatal编程技术网

Swift UIBezierPath-为单个路径绘制多个矩形

Swift UIBezierPath-为单个路径绘制多个矩形,swift,uibezierpath,Swift,Uibezierpath,我可以使用以下命令绘制单个矩形: let roundedRect = UIBezierPath(roundedRect: rect, cornerRadius: 50) 有了这个,我就不能在同一条路径上添加新的rect了。(我需要多个矩形到一个层) 我也可以通过移动点来绘制矩形: path.move(to: CGPoint(x:point1.x-rectWidth/2.0,y:point1.y) ) path.addLine(to: CGPoint(x: point1.x-re

我可以使用以下命令绘制单个矩形:

let roundedRect = UIBezierPath(roundedRect: rect, cornerRadius: 50)
有了这个,我就不能在同一条路径上添加新的rect了。(我需要多个矩形到一个层)

我也可以通过移动点来绘制矩形:

    path.move(to: CGPoint(x:point1.x-rectWidth/2.0,y:point1.y) )
    path.addLine(to: CGPoint(x: point1.x-rectWidth/2.0, y: point2.y))
    path.addLine(to: CGPoint(x: point1.x+rectWidth/2.0, y: point2.y))
    path.addLine(to: CGPoint(x: point1.x+rectWidth/2.0, y: point1.y))
    path.addLine(to: CGPoint(x:point1.x-rectWidth/2.0,y:point1.y))
这将不是一个四舍五入的矩形


我可以使用哪种方法获得圆角矩形?

自己构建圆角矩形有些棘手。我建议使用UIBezierPath初始值设定项
init(roundedRect:cornerRadius:)

您可以使用
append(:)
将圆角矩形路径追加到另一个路径:

var path = UIBezierPath() //Create an empty path

//Add a rounded rect to the path
path.append(UIBezierPath(roundedRect: rect1, cornerRadius: radius1))

//Add another rounded rect to the path
path.append(UIBezierPath(roundedRect: rect2, cornerRadius: radius2))

//Lather, rinse, repeat.
path.append(UIBezierPath(roundedRect: rect3, cornerRadius: radius3))
编辑:
要从底部为所有矩形绘制动画(如喷墨打印机),请创建一个与视图大小相同的CAShapeLayer,在底部安装一个零高度矩形,并使该层成为视图层的遮罩层。然后创建一个矩形的CABASICANIATION,并将其扩展到视图的整个高度。

为什么不创建多个
CAShapeLayers
?像视图一样,层也有子层。@dfd,OP从来没有提到过层。我们不知道他们的路径是否就是这样做的。@Duncac问题说“我需要多个矩形到一个层”。事实就是如此。我错过了这一点。所以你创建了多个贝塞尔实例,并将它们的路径提取到一个。表演可以吗(问起来可能很愚蠢)@Curnelious performance-wise你担心哪一部分?谢谢,有了这个,我找不到一个方法来制作动画。你知道如何从底部制作动画吗?可以与CBanimation一起使用您想要哪种动画?一次绘制一个矩形,然后立即绘制每个矩形?或者你想像喷墨打印机一样,从底部向上“绘画”吗?请参阅我答案的编辑。可以使用具有矩形形状的遮罩层,该矩形形状从视图的零高度增长到全高度。