IHBatteryView.swift 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // IHBatteryView.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2020/1/10.
  6. // Copyright © 2020 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. class IHBatteryView: UIView {
  10. //0~ 100
  11. var batteryValue : Int?{
  12. didSet{
  13. setBatteryValue(self.batteryValue ?? 0)
  14. }
  15. }
  16. //电池宽度
  17. private var b_width :CGFloat?
  18. //电池高度
  19. private var b_height :CGFloat?
  20. //电池高度
  21. private var b_lineW :CGFloat?
  22. private var batteryView : UIView?
  23. private var layerRight = CAShapeLayer()
  24. private var batteryLayer = CAShapeLayer()
  25. override init(frame: CGRect) {
  26. super.init(frame: frame)
  27. drawBattery()
  28. }
  29. required init?(coder: NSCoder) {
  30. fatalError("init(coder:) has not been implemented")
  31. }
  32. // override func awakeFromNib() {
  33. // super.awakeFromNib()
  34. // drawBattery()
  35. // }
  36. func drawBattery() {
  37. let b_x : CGFloat = 1.59
  38. let b_y : CGFloat = 1.59
  39. b_height = self.bounds.size.height - 2
  40. b_width = self.bounds.size.width - 5
  41. b_lineW = 1.59
  42. //画电池(电池左边)
  43. let pathLeft = UIBezierPath(roundedRect: CGRect(x: b_x, y: b_y, width: b_width!, height: b_height!), cornerRadius: 2)
  44. batteryLayer.lineWidth = b_lineW!
  45. batteryLayer.strokeColor = UIColor(hexString: "#05CFAB")?.cgColor
  46. batteryLayer.fillColor = UIColor.clear.cgColor
  47. batteryLayer.path = pathLeft.cgPath
  48. self.layer.addSublayer(batteryLayer)
  49. //画电池(右边电池箭头)
  50. let pathRight = UIBezierPath()
  51. pathRight.move(to: CGPoint(x: b_x + b_width! + 1, y: b_y + b_height! / 3))
  52. pathRight.addLine(to: CGPoint(x: b_x + b_width! + 1, y: b_y + b_height! * 2 / 3))
  53. layerRight.lineWidth = 2
  54. layerRight.strokeColor = UIColor(hexString: "#05CFAB")?.cgColor
  55. layerRight.fillColor = UIColor.clear.cgColor
  56. layerRight.path = pathRight.cgPath
  57. self.layer.addSublayer(layerRight)
  58. //电池内部填充
  59. batteryView = UIView(frame: CGRect(x: b_x + 1.59, y: b_y + b_lineW!, width: 0, height: b_height! - b_lineW! * 2))
  60. //batteryView?.layer.cornerRadius = 1
  61. batteryView?.backgroundColor = UIColor(hexString: "#05CFAB")
  62. addSubview(batteryView!)
  63. }
  64. func setBatteryValue(_ value: Int) {
  65. if value < 10{
  66. batteryLayer.strokeColor = UIColor(hexString: "#FF0C4D")?.cgColor
  67. batteryView?.backgroundColor = UIColor(hexString: "#FF0C4D")
  68. layerRight.strokeColor = UIColor(hexString: "#FF0C4D")?.cgColor
  69. }else{
  70. batteryLayer.strokeColor = UIColor(hexString: "#05CFAB")?.cgColor
  71. batteryView?.backgroundColor = UIColor(hexString: "#05CFAB")
  72. layerRight.strokeColor = UIColor(hexString: "#05CFAB")?.cgColor
  73. }
  74. var rect = batteryView?.frame
  75. rect?.size.width = (CGFloat(value) * ( b_width! - b_lineW! * 2 ) ) / 100
  76. batteryView!.frame = rect!
  77. }
  78. }