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

时间:2018-11-12 03:49:01

标签: 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-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))

这不是四舍五入的矩形。

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

1 个答案:

答案 0 :(得分:3)

自己建立一个圆形的矩形有些棘手。我建议使用UIBezierPath初始化程序init(roundedRect:cornerRadius:)

创建舍入的rect路径

您可以使用append(_:)将舍入的rect路径追加到另一个路径:

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,在底部安装高度为零的矩形,然后将该层用作遮罩层您视图的图层。然后创建一个矩形的CABasicAnimation,该矩形将增长到视图的整个高度。