给 UIView 添加虚线

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class ViewController: UIViewController {
@IBOutlet weak var targetView: UIView!
override func viewDidLoad() {
super.viewDidLoad()

let shapeLayer = CAShapeLayer()
shapeLayer.strokeColor = UIColor.red.cgColor
shapeLayer.fillColor = nil
shapeLayer.lineDashPattern = [3, 2, 6, 4]
shapeLayer.frame = targetView.bounds
shapeLayer.lineWidth = 0.5
shapeLayer.lineCap = .square
shapeLayer.path = UIBezierPath(rect: targetView.bounds).cgPath
targetView.layer.addSublayer(shapeLayer)
}
}

Demo

https://oss.hdvsyu.com/IMG_0A2DFA41E0CB-1.jpeg