import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
addStraightLine()
addStraightAndImaginaryLine()
}
/** 画直线 */
func addStraightLine() {
let lineLayer = CAShapeLayer()
let mdotteShapePath = CGMutablePath()
lineLayer.fillColor = UIColor.clear.cgColor//填充色
lineLayer.strokeColor = UIColor.blue.cgColor//线颜色
lineLayer.lineWidth = 1.0
mdotteShapePath.move(to: .init(x: 20, y: 100))
mdotteShapePath.addLine(to: .init(x: 200, y: 100))
mdotteShapePath.addLine(to: .init(x: 200, y: 200))
lineLayer.path = mdotteShapePath
view.layer.addSublayer(lineLayer)
}
/** 画直虚线 */
func addStraightAndImaginaryLine() {
let dotteShapLayer = CAShapeLayer()
let mdotteShapePath = CGMutablePath()
dotteShapLayer.fillColor = UIColor.clear.cgColor//填充色
dotteShapLayer.strokeColor = UIColor.orange.cgColor//线颜色
dotteShapLayer.lineWidth = 1.0//限宽
mdotteShapePath.move(to: .init(x: 20, y: 100))
mdotteShapePath.addLine(to: .init(x: 20, y: 200))
mdotteShapePath.addLine(to: .init(x: 200, y: 200))
dotteShapLayer.path = mdotteShapePath
dotteShapLayer.lineDashPhase = 1
dotteShapLayer.lineDashPattern = [5,2]//第一个为线条长度,第二个为线条间间隔
view.layer.addSublayer(dotteShapLayer)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}