IHSubsectionView.swift 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //
  2. // IHSubsectionView.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2020/1/16.
  6. // Copyright © 2020 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. class IHSubsectionView: NSObject {
  10. static let share = IHSubsectionView()
  11. private var goodView:UIView?
  12. private var normalView:UIView?
  13. private var poorView :UIView?
  14. func showLevel(good:Int,normal:Int,poor:Int,InView view:UIView) {
  15. if goodView != nil {
  16. goodView?.removeFromSuperview()
  17. }
  18. if normalView != nil {
  19. normalView?.removeFromSuperview()
  20. }
  21. if poorView != nil {
  22. normalView?.removeFromSuperview()
  23. }
  24. goodView = UIView()
  25. view.addSubview(goodView!)
  26. normalView = UIView()
  27. view.addSubview(normalView!)
  28. poorView = UIView()
  29. view.addSubview(poorView!)
  30. let total = good + normal + poor
  31. let goodx = 0
  32. let goodWidth = view.bounds.size.width * CGFloat(good) / CGFloat(total)
  33. let normalx = Int(goodWidth)
  34. let normalWidth = view.bounds.size.width * CGFloat(normal) / CGFloat(total)
  35. let poorx = Int(goodWidth) + Int(normalWidth)
  36. let poorWidth = Int(view.bounds.size.width) * poor / total
  37. goodView?.frame = CGRect(x: goodx, y: 0, width: Int(goodWidth), height: 4)
  38. normalView?.frame = CGRect(x: normalx, y: 0, width: Int(normalWidth), height: 4)
  39. poorView?.frame = CGRect(x: poorx, y: 0, width: poorWidth, height: 4)
  40. // #05CFAB
  41. goodView?.backgroundColor = UIColor(hexString: "#573F95")
  42. normalView?.backgroundColor = UIColor(hexString: "#573F95")
  43. poorView?.backgroundColor = UIColor(hexString: "#FF0C4D")
  44. }
  45. }