MerchantsMenuContent.swift 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //
  2. // MerchantsMenuContent.swift
  3. // xingchuangke
  4. //
  5. // Created by Apple on 2021/3/3.
  6. // Copyright © 2021 Virgil. All rights reserved.
  7. //
  8. class MerchantsMenuContent: UIView {
  9. @IBOutlet weak var backView: UIView!
  10. var arrDataMj = NSMutableArray()
  11. var dicModel = NSDictionary()
  12. var btn = UIButton()
  13. var str = ""
  14. var currentTag = 0
  15. var selectedBtn = UIButton()
  16. public var callBackForSure: ((NSDictionary)->())?
  17. var w: CGFloat = 0
  18. var h: CGFloat = 15
  19. func returnData( ) {
  20. for i in 0..<self.arrDataMj.count {
  21. let dicNew = NSMutableDictionary(dictionary: self.arrDataMj[i] as! NSDictionary)
  22. let name = (dicNew as NSDictionary).object(forKey: "machineTypeName") as! String
  23. let button = UIButton(type: .custom)
  24. button.backgroundColor = .colorWithCustom(r: 241, g: 241, b: 241)
  25. button.layer.cornerRadius = 3.0
  26. button.layer.masksToBounds = true
  27. button.setFontSize(fontSize: 11)
  28. button.setTitle(name, for: .normal)
  29. button.setTitleColor(.black, for: .normal)
  30. button.tag = i
  31. if i == 0 {
  32. selectedBtn = button
  33. }
  34. button.addTarget(self, action: #selector(rewardClick(sender:)), for: .touchUpInside)
  35. let length = (backView.frame.size.width-10*3)/2
  36. button.frame = CGRect(x: 10 + w, y: h, width: length, height: 30)
  37. //当button的位置超出右侧边缘时换行
  38. if 10+w+length > backView.frame.size.width {
  39. w = 0
  40. h = h + button.frame.size.height + 10
  41. button.frame = CGRect(x: 10+w, y: h, width: length, height: 30)
  42. }
  43. w = button.frame.size.width + button.frame.origin.x
  44. backView.addSubview(button)
  45. }
  46. }
  47. @objc func rewardClick(sender : UIButton ) {
  48. self.btn = sender
  49. sender.isSelected = !sender.isSelected
  50. selectedBtn.setTitleColor(.black, for: .selected)
  51. selectedBtn = btn
  52. selectedBtn.setTitleColor(.red, for: .selected)
  53. if sender.isSelected {
  54. self.dicModel = self.arrDataMj[sender.tag] as! NSDictionary
  55. self.str = self.dicModel["machineTypeName"] as! String
  56. sender.setTitleColor(.red, for: .selected)
  57. }
  58. }
  59. @IBAction func btnMenuClick(_ sender: Any) {
  60. let btn = (sender as! UIButton)
  61. switch btn.tag {
  62. case 1007: // 重置
  63. do {
  64. self.btn.isSelected = false
  65. self.dicModel = ["machineTypeId": ""]
  66. }
  67. break
  68. case 1008: // 确定
  69. if callBackForSure != nil{
  70. callBackForSure!(self.dicModel)
  71. }
  72. break
  73. default: break
  74. }
  75. }
  76. }