UIBezierPath - 为单个路径绘制多个矩形
UIBezierPath - draw multiple rects for a single path
我可以用 :
绘制一个矩形
let roundedRect = UIBezierPath(roundedRect: rect, cornerRadius: 50)
有了这个,我无法将新的矩形添加到同一路径。 (我需要多个矩形到一个层)
我也可以通过移动点来绘制一个矩形:
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,在底部安装一个零高度矩形,并将该层设为遮罩视图层的层。然后创建矩形的 CABasicAnimation 增长到视图的整个高度。
我可以用 :
绘制一个矩形let roundedRect = UIBezierPath(roundedRect: rect, cornerRadius: 50)
有了这个,我无法将新的矩形添加到同一路径。 (我需要多个矩形到一个层)
我也可以通过移动点来绘制一个矩形:
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,在底部安装一个零高度矩形,并将该层设为遮罩视图层的层。然后创建矩形的 CABasicAnimation 增长到视图的整个高度。