// // IHDashView.swift // Inhealth // // Created by weclouds on 2019/12/12. // Copyright © 2019 weclouds. All rights reserved. // import UIKit class IHDashView: UIView { convenience init(strokeColor : CGColor? ,gap:Int?,lineWith:Int?) { self.init() self.strokeColor = strokeColor self.gap = gap self.lineWith = lineWith } var strokeColor : CGColor? = UIColor(hexString: "C6CDD5", transparency: 0.5)?.cgColor var gap : Int? = 3 var lineWith : Int? = 1 override var frame: CGRect{ didSet{ addDashView() } } private override init(frame: CGRect) { super.init(frame: frame) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } func addDashView() { let shapeLayer = CAShapeLayer() shapeLayer.bounds = self.bounds shapeLayer.position = CGPoint(x: self.frame.width / 2, y: self.frame.height ) shapeLayer.fillColor = UIColor.clear.cgColor //设置虚线颜色 shapeLayer.strokeColor = strokeColor shapeLayer.lineWidth = 1 //设置虚线高度 shapeLayer.lineJoin = CAShapeLayerLineJoin.round shapeLayer.lineDashPhase = 0 //设置虚线线宽以及间距 shapeLayer.lineDashPattern = [NSNumber(value: gap!), NSNumber(value: lineWith!)] let path = CGMutablePath() //设置虚线绘制i起点 path.move(to: CGPoint(x: 0, y: 0)) //设置虚线绘制终点 path.addLine(to: CGPoint(x: self.frame.width, y: 0 )) shapeLayer.path = path self.layer.addSublayer(shapeLayer) } }