///创建一个高为1的线-->实际使用
func config() {
let lineView = UIView(frame: CGRect(x: 10, y: 300, width: self.view.bounds.width - 20, height: 1))
self.view.addSubview(lineView)
drawDashLine(lineView: lineView, lineLength: 10, lineSpacing: 5, lineColor: UIColor.red)
}
/**
* 通过 CAShapeLayer 方式绘制虚线
* lineView: 需要绘制成虚线的view
* lineLength: 线宽
* lineSpacing: 线间距
**/
func drawDashLine(lineView : UIView, lineLength : Int ,lineSpacing : Int,lineColor : UIColor){
let shapeLayer = CAShapeLayer()
shapeLayer.bounds = lineView.bounds
//CALayer类型,anchorPoint默认(0.5,0.5)
shapeLayer.anchorPoint = CGPoint(x: 0, y: 0)
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.strokeColor = lineColor.cgColor
shapeLayer.lineWidth = lineView.frame.size.height
shapeLayer.lineJoin = kCALineJoinRound
shapeLayer.lineDashPattern = [NSNumber(value: lineLength), NSNumber(value: lineSpacing)]
let path = CGMutablePath()
path.move(to: CGPoint(x: 0, y: 0))
path.addLine(to: CGPoint(x: lineView.frame.size.width, y: 0))
shapeLayer.path = path
lineView.layer.addSublayer(shapeLayer)
}