YCOrderFootView.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // YCOrderFootView.swift
  3. // learnSwift
  4. //
  5. // Created by zyc on 2019/12/23.
  6. // Copyright © 2019 张言超. All rights reserved.
  7. //
  8. import UIKit
  9. @objc protocol YC_FootViewDelegate {
  10. func selectedAllBtnDidPress(button: UIButton, isSel: Bool)
  11. }
  12. class YCOrderFootView: UIView {
  13. /** 全选按钮 */
  14. var selectAllBtn: UIButton?
  15. /** 价格label */
  16. var sumLabel: UILabel?
  17. /** 结算按钮 */
  18. var accountBtn: UIButton?
  19. /** 全选按钮点击 */
  20. var isSelectAll: Bool = false
  21. /** 选中商品的个数 */
  22. var selectCount: NSInteger?
  23. //代理
  24. weak var footDelegate: YC_FootViewDelegate?
  25. // //判断是否全选
  26. // var isAllSel:Bool?
  27. /** 数据 */
  28. private var _xshopModel: YCOrderShopModel?
  29. var shopModel: YCOrderShopModel? {
  30. get {
  31. return _xshopModel
  32. }
  33. set {
  34. _xshopModel = newValue
  35. if _xshopModel!.shopIsAllSelected {
  36. }
  37. }
  38. }
  39. override init(frame: CGRect) {
  40. super.init(frame: frame)
  41. self.customFootViewUI()
  42. }
  43. let selectBtnWH: CGFloat = 30
  44. let boardWidth: CGFloat = 10
  45. let viewHeight: CGFloat = 70
  46. func customFootViewUI() {
  47. self.backgroundColor = UIColor.colorWithCustom(r: 222, g: 222, b: 222)
  48. //左侧按钮
  49. self.selectAllBtn = UIButton.init(frame: CGRect(x: boardWidth, y: (viewHeight - selectBtnWH)*0.5, width: selectBtnWH, height: selectBtnWH))
  50. self.selectAllBtn?.setImage(UIImage.init(named: "GameCenterNewWhite"), for: .normal)
  51. self.selectAllBtn?.setTitle("全选", for: .normal)
  52. self.selectAllBtn?.setTitleColor(UIColor.black, for: .normal)
  53. self.selectAllBtn?.addTarget(self, action: #selector(leftSelectAllBtnDidPress(button:)), for: .touchUpInside)
  54. self.addSubview(self.selectAllBtn!)
  55. self.accountBtn = UIButton.init(frame: CGRect(x: kScreenWidth - (kScreenWidth/3.0 - 2*boardWidth), y: 0, width: kScreenWidth/3.0 - 2*boardWidth, height: viewHeight))
  56. self.accountBtn?.backgroundColor = UIColor.colorWithCustom(r: 226, g: 84, b: 91)
  57. self.accountBtn?.setTitle("去结算", for: .normal)
  58. self.accountBtn?.setTitleColor(.white, for: .normal)
  59. self.accountBtn?.setTitleColor(.lightGray, for: .highlighted)
  60. self.addSubview(self.accountBtn!)
  61. //价格label
  62. self.sumLabel = UILabel.init(frame: CGRect(x: (self.selectAllBtn?.yc_right())! + boardWidth, y: (viewHeight - selectBtnWH)*0.5, width: kScreenWidth - 3*boardWidth - self.selectAllBtn!.yc_width() - (self.accountBtn?.yc_width())!, height: selectBtnWH))
  63. let att: NSAttributedString = NSAttributedString(string: "合计:", attributes: [NSAttributedString.Key.foregroundColor: UIColor.black, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 20)])
  64. let att1: NSAttributedString = NSAttributedString(string: "¥0.00", attributes: [NSAttributedString.Key.foregroundColor: UIColor.red, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 20)])
  65. //定义富文本即有格式的字符串
  66. let attributedStrM: NSMutableAttributedString = NSMutableAttributedString()
  67. attributedStrM.append(att)
  68. attributedStrM.append(att1)
  69. self.addSubview(self.sumLabel!)
  70. self.sumLabel?.attributedText = attributedStrM
  71. }
  72. /** 全选按钮点击了 */
  73. @objc func leftSelectAllBtnDidPress(button: UIButton) {
  74. if self.isSelectAll {
  75. self.selectAllBtn?.setImage(UIImage.init(named: "GameCenterNewWhite"), for: .normal)
  76. self.isSelectAll = false
  77. } else {
  78. self.selectAllBtn?.setImage(UIImage.init(named: "GameCenterNewAppTag"), for: .normal)
  79. self.isSelectAll = true
  80. }
  81. if self.footDelegate != nil {
  82. self.footDelegate?.selectedAllBtnDidPress(button: button, isSel: self.isSelectAll)
  83. }
  84. }
  85. //更新
  86. func updateArr(selectedArr: NSArray, dataArr: NSArray) {
  87. //计算价格
  88. var sumPrice: Float = 0
  89. //选中商品总数
  90. var sumCount: NSInteger = 0
  91. //总的商品数量
  92. var allCount: NSInteger = 0
  93. for index in 0..<dataArr.count {
  94. let shopmodel: YCOrderShopModel = dataArr[index] as! YCOrderShopModel
  95. allCount += shopmodel.goodsArr!.count
  96. }
  97. for xindex in 0..<selectedArr.count {
  98. let selArr: NSMutableArray = selectedArr[xindex] as! NSMutableArray
  99. let count: NSInteger = selArr.count
  100. sumCount += count
  101. if count > 0 {
  102. for jindex in 0..<count {
  103. let goodmodel: YCOrderGoodsModel = selArr[jindex] as! YCOrderGoodsModel
  104. let price: String = goodmodel.goodsPrice!
  105. let priceNum: Float = Float(price)!
  106. let ccount: Int = goodmodel.goodsCount!
  107. sumPrice += priceNum*Float(ccount)
  108. }
  109. }
  110. }
  111. let att: NSAttributedString = NSAttributedString(string: "合计:", attributes: [NSAttributedString.Key.foregroundColor: UIColor.black, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 20)])
  112. let ss: String = String.init(format: "¥%.2f", sumPrice)
  113. let att1: NSAttributedString = NSAttributedString(string: ss, attributes: [NSAttributedString.Key.foregroundColor: UIColor.red, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 20)])
  114. //定义富文本即有格式的字符串
  115. let attributedStrM: NSMutableAttributedString = NSMutableAttributedString()
  116. attributedStrM.append(att)
  117. attributedStrM.append(att1)
  118. self.sumLabel?.attributedText = attributedStrM
  119. self.selectCount = sumCount
  120. let accountBtnTile = sumCount > 0 ? String.init(format: "去结算(%ld)", sumCount) : "去结算"
  121. self.accountBtn?.setTitle(accountBtnTile, for: .normal)
  122. self.isSelectAll = sumCount == allCount ? true : false
  123. if !self.isSelectAll {
  124. self.selectAllBtn?.setImage(UIImage.init(named: "GameCenterNewWhite"), for: .normal)
  125. self.isSelectAll = false
  126. } else {
  127. self.selectAllBtn?.setImage(UIImage.init(named: "GameCenterNewAppTag"), for: .normal)
  128. self.isSelectAll = true
  129. }
  130. }
  131. required init?(coder: NSCoder) {
  132. fatalError("init(coder:) has not been implemented")
  133. }
  134. }