IHButtonView.swift 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // IHButtonView.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2020/3/17.
  6. // Copyright © 2020 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. protocol IHButtonViewDelegate : NSObjectProtocol{
  10. func numberofItem(in buttonView : IHButtonView)->Int
  11. func buttonView(_ buttonView: IHButtonView,titleFor item:Int) ->String
  12. func buttonView(_ buttonView: IHButtonView,didSelected item:Int)
  13. }
  14. class IHButtonView: UIView {
  15. weak var delegate : IHButtonViewDelegate?
  16. var buttonArr :[UIButton]? = [UIButton]()
  17. override init(frame: CGRect) {
  18. super.init(frame: frame)
  19. }
  20. required init?(coder: NSCoder) {
  21. fatalError("init(coder:) has not been implemented")
  22. }
  23. func reloadData() {
  24. self.removeSubviews()
  25. createButtons()
  26. }
  27. //创建按钮
  28. func createButtons() {
  29. let bgViewWidth :CGFloat = KSCREENWIDTH - 40 - 20
  30. let space :CGFloat = 17
  31. var buttonX :CGFloat = 10
  32. var buttonY : CGFloat = 10
  33. // let buttonW :CGFloat = (bgViewWidth - 3 * space) / 4
  34. let buttonH :CGFloat = 30
  35. let count = delegate?.numberofItem(in: self )
  36. for i in 0..<count! {
  37. let title = delegate?.buttonView(self, titleFor: i)
  38. // let col : Int = i % 4 //列
  39. // let row : Int = i / 4 // 行
  40. let str_width = title!.ga_widthForComment(font: UIFont(name: PingFangSC_Regular, size: 13)!, height: 30)
  41. let btnW = str_width + 20
  42. if buttonX + btnW > bgViewWidth{
  43. buttonX = 20
  44. buttonY += 40
  45. }
  46. let btn = UIButton(type: .custom)
  47. btn.frame = CGRect(x: buttonX, y: buttonY, width: btnW, height: buttonH)
  48. btn.setTitle(title, for: .normal)
  49. btn.backgroundColor = UIColor(hexString: "#FFFFFF")
  50. btn.setTitleColor(UIColor(hexString: "#333333"), for: .normal)
  51. btn.setTitleColor(UIColor(hexString: "#FFFFFF"), for: .selected)
  52. btn.titleLabel?.font = UIFont(name: PingFangSC_Regular, size: 13)
  53. btn.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
  54. btn.tag = 1023243 + i
  55. btn.layer.masksToBounds = true
  56. btn.layer.cornerRadius = 4
  57. if i == 0 {
  58. btn.isSelected = true
  59. btn.backgroundColor = UIColor(hexString: "#573F95")
  60. }
  61. self.addSubview(btn)
  62. self.buttonArr?.append(btn)
  63. buttonX += (btnW + 10)
  64. }
  65. }
  66. @objc func buttonClick(_ sender:UIButton) {
  67. for btn in buttonArr! {
  68. btn.isSelected = false
  69. btn.backgroundColor = UIColor(hexString: "#FFFFFF")
  70. }
  71. sender.isSelected = true
  72. sender.backgroundColor = UIColor(hexString: "#573F95")
  73. if let delegate = self.delegate {
  74. delegate.buttonView(self, didSelected: sender.tag - 1023243)
  75. }
  76. }
  77. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  78. super.touchesBegan(touches, with: event)
  79. log.debug("点击了 - buttonView")
  80. }
  81. }