SelfRecommendationView.swift 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //
  2. // SelfRecommendationView.swift
  3. // RainbowPlanet
  4. //
  5. // Created by 南鑫林 on 2019/4/19.
  6. // Copyright © 2019 南鑫林. All rights reserved.
  7. //
  8. import UIKit
  9. class SelfRecommendationView: BaseView {
  10. typealias GoShoppingBlock = () -> Void
  11. var goShoppingBlock : GoShoppingBlock?
  12. typealias DidSelectGoShoppingBlock = (_ indexPath: IndexPath) -> Void
  13. var didSelectGoShoppingBlock : DidSelectGoShoppingBlock?
  14. typealias ChangeLocationBlock = (_ locationAddress: String) ->Void
  15. var changeLocationBlock : ChangeLocationBlock?
  16. var locationAddress: String? {
  17. didSet {
  18. locationAddressLable.text = locationAddress
  19. }
  20. }
  21. var selfMentionAddressArray : Array<SelfMentionAddressModel>?{
  22. didSet {
  23. if selfMentionAddressArray?.isEmpty ?? true {
  24. collectionView.endFooterNoMoreData()
  25. }else {
  26. collectionView.isHiddenFooter(false)
  27. }
  28. collectionView.reloadData()
  29. }
  30. }
  31. override func setupViews() {
  32. backgroundColor = kf7f8faColor
  33. addSubview(locationBgImageView)
  34. locationBgImageView.addSubview(locationTitleLable)
  35. locationBgImageView.addSubview(locationAddressLable)
  36. locationBgImageView.addSubview(changeLocationButton)
  37. addSubview(collectionView)
  38. let emptyView = DIYEmptyView.emptyActionView(withImageStr: "default_page_location", titleStr: "当前位置没有自提点,先进商城逛逛吧", detailStr: nil, btnTitleStr: "去商城") {
  39. [weak self] in
  40. /// 跳转到商场
  41. if let goShoppingBlock = self?.goShoppingBlock {
  42. goShoppingBlock()
  43. }
  44. }
  45. emptyView?.subViewMargin = 25
  46. emptyView?.actionBtnFont = kRegularFont16
  47. emptyView?.actionBtnHeight = 36
  48. emptyView?.actionBtnHorizontalMargin = 56
  49. emptyView?.actionBtnCornerRadius = 36/2
  50. emptyView?.actionBtnTitleColor = kffffffColor
  51. emptyView?.actionBtnBackGroundColor = kFFA42FColor
  52. emptyView?.contentViewY = kScaleValue(value: 60)
  53. collectionView.ly_emptyView = emptyView
  54. collectionView.ly_startLoading()
  55. }
  56. override func setupLayouts() {
  57. locationTitleLable.snp.makeConstraints { (make) in
  58. make.top.equalToSuperview().offset(kScaleValue(value: 22))
  59. make.centerX.equalToSuperview()
  60. }
  61. locationAddressLable.snp.makeConstraints { (make) in
  62. make.top.equalTo(locationTitleLable.snp.bottom).offset(kScaleValue(value: 12))
  63. make.left.equalToSuperview().offset(kScaleValue(value: 20))
  64. make.right.equalToSuperview().offset(kScaleValue(value: -20))
  65. }
  66. changeLocationButton.snp.makeConstraints { (make) in
  67. make.top.equalTo(locationAddressLable.snp.bottom).offset(kScaleValue(value: 16))
  68. make.width.equalTo(kScaleValue(value: 80))
  69. make.height.equalTo(kScaleValue(value: 24))
  70. make.centerX.equalToSuperview()
  71. }
  72. locationBgImageView.snp.makeConstraints { (make) in
  73. make.top.left.right.equalToSuperview()
  74. make.bottom.equalTo(changeLocationButton).offset(kScaleValue(value: 22))
  75. }
  76. collectionView.snp.makeConstraints { (make) in
  77. make.top.equalTo(locationBgImageView.snp.bottom)
  78. make.left.right.bottom.equalToSuperview()
  79. }
  80. }
  81. private lazy var locationBgImageView: UIImageView = {
  82. let locationBgImageView = UIImageView()
  83. locationBgImageView.image = kImage(name: "my_bg")
  84. locationBgImageView.isUserInteractionEnabled = true
  85. return locationBgImageView
  86. }()
  87. private lazy var locationTitleLable: UILabel = {
  88. let locationTitleLable = UILabel()
  89. locationTitleLable.text = "当前位置"
  90. locationTitleLable.textColor = kffffffColor
  91. locationTitleLable.font = kScaleRegularFont14
  92. return locationTitleLable
  93. }()
  94. private lazy var locationAddressLable: UILabel = {
  95. let locationAddressLable = UILabel()
  96. let locationModel = LocationModel.shared().object()
  97. locationAddressLable.text = locationModel!.toString()
  98. locationAddressLable.textColor = kffffffColor
  99. locationAddressLable.font = kScaleBoldFont16
  100. locationAddressLable.textAlignment = .center
  101. locationAddressLable.numberOfLines = 0
  102. return locationAddressLable
  103. }()
  104. private lazy var changeLocationButton: UIButton = {
  105. let changeLocationButton = UIButton(type: UIButton.ButtonType.custom)
  106. changeLocationButton.setTitle("修改位置", for: UIControl.State.normal)
  107. changeLocationButton.setBackgroundImage(UIImage.imageWithColor(color: kffffffColor), for: UIControl.State.normal)
  108. changeLocationButton.setTitleColor(kffb04AColor, for: UIControl.State.normal)
  109. changeLocationButton.titleLabel?.font = kScaleRegularFont13
  110. changeLocationButton.cornerRadius = kScaleValue(value: 12)
  111. changeLocationButton.masksToBounds = true
  112. changeLocationButton.rx.tap.subscribe(onNext: {
  113. [weak self] in
  114. if let changeLocationBlock = self?.changeLocationBlock {
  115. changeLocationBlock(self?.locationAddressLable.text ?? "")
  116. }
  117. }).disposed(by: disposeBag)
  118. return changeLocationButton
  119. }()
  120. lazy var collectionView: UICollectionView = {
  121. let collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: collectionViewLayout)
  122. collectionView.backgroundColor = kf7f8faColor
  123. collectionView.delegate = self;
  124. collectionView.dataSource = self;
  125. collectionView.showsVerticalScrollIndicator = false
  126. collectionView.showsHorizontalScrollIndicator = false
  127. return collectionView
  128. }()
  129. private lazy var collectionViewLayout: UICollectionViewFlowLayout = {
  130. let collectionViewLayout = UICollectionViewFlowLayout.init()
  131. collectionViewLayout.minimumLineSpacing = 0
  132. collectionViewLayout.minimumInteritemSpacing = 0
  133. collectionViewLayout.estimatedItemSize = CGSize(width:kScreenWidth-kScaleValue(value: 28), height: kScaleValue(value: 110))
  134. collectionViewLayout.sectionHeadersPinToVisibleBounds = true
  135. return collectionViewLayout
  136. }()
  137. }
  138. extension SelfRecommendationView : UICollectionViewDelegateFlowLayout,UICollectionViewDataSource {
  139. func numberOfSections(in collectionView: UICollectionView) -> Int {
  140. return selfMentionAddressArray?.isEmpty ?? true ? 0 : 1
  141. }
  142. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  143. switch section {
  144. case 0:
  145. return selfMentionAddressArray?.isEmpty ?? true ? 0 : selfMentionAddressArray?.count ?? 0
  146. default:
  147. return 0
  148. }
  149. }
  150. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  151. switch indexPath.section {
  152. case 0:
  153. let cell = SelfRecommendationCollectionViewCell.cellWith(collectionView: collectionView, indexPath: indexPath)
  154. cell.selfMentionAddressModel = selfMentionAddressArray![indexPath.row]
  155. return cell
  156. default:
  157. return UICollectionViewCell()
  158. }
  159. }
  160. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  161. if let didSelectGoShoppingBlock = self.didSelectGoShoppingBlock {
  162. didSelectGoShoppingBlock(indexPath)
  163. }
  164. }
  165. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
  166. switch section {
  167. case 0:
  168. return UIEdgeInsets(top: 0, left: kScaleValue(value: 10), bottom: kScaleValue(value: 10), right: kScaleValue(value: 10))
  169. default:
  170. return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  171. }
  172. }
  173. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  174. switch section {
  175. case 0:
  176. return kScaleValue(value: 10)
  177. default:
  178. return 0
  179. }
  180. }
  181. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  182. if kind == UICollectionView.elementKindSectionHeader {
  183. switch indexPath.section {
  184. case 0:
  185. let headerView = SelfRecommendationHeaderCollectionReusableView.headerWith(collectionView: collectionView, kind: UICollectionView.elementKindSectionHeader, indexPath: indexPath)
  186. return headerView
  187. default:
  188. return UICollectionReusableView()
  189. }
  190. }else {
  191. return UICollectionReusableView()
  192. }
  193. }
  194. // 返回HeadView的宽高
  195. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  196. switch section {
  197. case 0:
  198. return CGSize(width:kScreenWidth, height: kScaleValue(value: 57))
  199. default:
  200. return CGSize(width:kScreenWidth, height: 0)
  201. }
  202. }
  203. }