1.实现
、、、
//MARK:- 绘制虚线
/**
* 绘制虚线
*/
func drawDottedLine(_ rect: CGRect, _ radius: CGFloat, _ color: UIColor) {
let layer = CAShapeLayer()
layer.bounds = CGRect(x: 0, y: 0, width: rect.width, height: rect.height)
layer.position = CGPoint(x: rect.midX, y: rect.midY)
layer.path = UIBezierPath(rect: layer.bounds).cgPath
layer.path = UIBezierPath(roundedRect: layer.bounds, cornerRadius: radius).cgPath
layer.lineWidth = 1/UIScreen.main.scale
//虚线边框
layer.lineDashPattern = [NSNumber(value: 5), NSNumber(value: 5)]
layer.fillColor = UIColor.clear.cgColor
layer.strokeColor = color.cgColor
self.layer.addSublayer(layer)
}
、、、
2.使用方法
、、、
let view1 = UIView(frame: CGRect(x: 50, y: 100, width: 300, height: 30))
view1.backgroundColor = UIColor.white
view1.drawDottedLine(view1.bounds, 7, UIColor.gray)
view.addSubview(view1)
、、、
31305B6A-6631-4E30-95D4-0D4DAEA256F4.png
网友评论