IHDashView.swift 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //
  2. // IHDashView.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2019/12/12.
  6. // Copyright © 2019 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. class IHDashView: UIView {
  10. convenience init(strokeColor : CGColor? ,gap:Int?,lineWith:Int?) {
  11. self.init()
  12. self.strokeColor = strokeColor
  13. self.gap = gap
  14. self.lineWith = lineWith
  15. }
  16. var strokeColor : CGColor? = UIColor(hexString: "C6CDD5", transparency: 0.5)?.cgColor
  17. var gap : Int? = 3
  18. var lineWith : Int? = 1
  19. override var frame: CGRect{
  20. didSet{
  21. addDashView()
  22. }
  23. }
  24. private override init(frame: CGRect) {
  25. super.init(frame: frame)
  26. }
  27. required init?(coder: NSCoder) {
  28. fatalError("init(coder:) has not been implemented")
  29. }
  30. func addDashView() {
  31. let shapeLayer = CAShapeLayer()
  32. shapeLayer.bounds = self.bounds
  33. shapeLayer.position = CGPoint(x: self.frame.width / 2, y: self.frame.height )
  34. shapeLayer.fillColor = UIColor.clear.cgColor
  35. //设置虚线颜色
  36. shapeLayer.strokeColor = strokeColor
  37. shapeLayer.lineWidth = 1 //设置虚线高度
  38. shapeLayer.lineJoin = CAShapeLayerLineJoin.round
  39. shapeLayer.lineDashPhase = 0
  40. //设置虚线线宽以及间距
  41. shapeLayer.lineDashPattern = [NSNumber(value: gap!), NSNumber(value: lineWith!)]
  42. let path = CGMutablePath()
  43. //设置虚线绘制i起点
  44. path.move(to: CGPoint(x: 0, y: 0))
  45. //设置虚线绘制终点
  46. path.addLine(to: CGPoint(x: self.frame.width, y: 0 ))
  47. shapeLayer.path = path
  48. self.layer.addSublayer(shapeLayer)
  49. }
  50. }