SearchContentListCollectionCell.swift 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //
  2. // RecommendSimilarCollectionCell.swift
  3. // RainbowPlanet
  4. //
  5. // Created by Christopher on 2019/6/14.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. // 相关推荐のCollectionCell
  8. import UIKit
  9. import RxSwift
  10. import Lottie
  11. import Kingfisher
  12. class SearchContentListCollectionCell: UICollectionViewCell {
  13. deinit {
  14. NXLLog("deinit")
  15. }
  16. let disposeBag = DisposeBag()
  17. typealias LikeSelectBlock = (_ isLiked: Int) -> Void
  18. var likeSelectBlock : LikeSelectBlock?
  19. class func cellWith(collectionView:UICollectionView,indexPath:IndexPath) -> SearchContentListCollectionCell {
  20. let ID = "RecommendSimilarCollectionCell"
  21. collectionView.register(SearchContentListCollectionCell.self, forCellWithReuseIdentifier: ID)
  22. let cell : SearchContentListCollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: ID, for: indexPath) as! SearchContentListCollectionCell
  23. cell.indexPath = indexPath
  24. return cell
  25. }
  26. //MARK: - indexPath
  27. var indexPath: IndexPath?{
  28. didSet {
  29. if indexPath?.row == 0 {
  30. contentImageView.snp.remakeConstraints { (make) in
  31. make.top.left.right.equalToSuperview()
  32. make.height.equalTo((kScreenWidth-15)/2)
  33. }
  34. }else {
  35. contentImageView.snp.remakeConstraints { (make) in
  36. make.top.left.right.equalToSuperview()
  37. make.height.equalTo(240*kScaleWidth)
  38. }
  39. }
  40. }
  41. }
  42. //MARK: - 初始化
  43. override init(frame: CGRect) {
  44. super.init(frame: frame)
  45. backgroundColor = kffffffColor
  46. setupViews()
  47. setupLayouts()
  48. setupdata()
  49. }
  50. required init?(coder aDecoder: NSCoder) {
  51. fatalError("init(coder:) has not been implemented")
  52. }
  53. //MRAK: - 设置View
  54. private func setupViews() {
  55. self.cornerRadius = 4
  56. self.masksToBounds = true
  57. addSubview(contentImageView)
  58. addSubview(titleLabel)
  59. addSubview(avatarButton)
  60. addSubview(nameButton)
  61. addSubview(likeBtn)
  62. addSubview(likeLabel)
  63. contentImageView.addSubview(pauseImageView)
  64. likeBtn.imageView?.addSubview(praiseAnimationView)
  65. }
  66. private func setupLayouts() {
  67. contentImageView.snp.makeConstraints { (make) in
  68. make.top.left.right.equalToSuperview()
  69. }
  70. titleLabel.snp.makeConstraints { (make) in
  71. make.top.equalTo(contentImageView.snp_bottom).offset(15)
  72. make.left.equalTo(contentImageView.snp_left).offset(10)
  73. make.right.equalTo(contentImageView.snp_right).offset(-10)
  74. make.height.lessThanOrEqualTo(37)
  75. }
  76. avatarButton.snp.makeConstraints { (make) in
  77. make.top.equalTo(titleLabel.snp_bottom).offset(10)
  78. make.left.equalToSuperview().offset(10)
  79. make.size.equalTo(18)
  80. }
  81. nameButton.snp.makeConstraints { (make) in
  82. make.centerY.equalTo(avatarButton)
  83. make.left.equalTo(avatarButton.snp_right).offset(5)
  84. make.height.equalTo(18)
  85. make.right.equalTo(likeBtn.snp_left).offset(-5)
  86. }
  87. likeLabel.snp.makeConstraints { (make) in
  88. make.centerY.equalTo(avatarButton)
  89. make.right.equalTo(-13)
  90. make.left.equalTo(likeBtn.snp.right)
  91. }
  92. likeBtn.snp.makeConstraints { (make) in
  93. make.centerY.equalTo(avatarButton)
  94. make.right.equalTo(likeLabel.snp.left).offset(-3)
  95. make.size.equalTo(18)
  96. }
  97. praiseAnimationView.snp.makeConstraints { (make) in
  98. make.edges.equalToSuperview()
  99. }
  100. pauseImageView.snp_makeConstraints { (make) in
  101. make.center.equalToSuperview()
  102. }
  103. }
  104. func setupdata() {
  105. avatarButton.rx.tap.subscribe(onNext: {
  106. [weak self] (data) in
  107. self?.nameButtonAction()
  108. }).disposed(by: disposeBag)
  109. likeBtn.rx.tap.subscribe(onNext: { [weak self] (data) in
  110. if UserModel.isTokenNil() {
  111. kAppDelegate.setLogin()
  112. }else {
  113. if self?.communityPostDataModel?.isLike == 0 {
  114. self?.praiseAnimationView.isHidden = false
  115. self?.praiseAnimationView.play(completion: { [weak self] (_) in
  116. self?.praiseAnimationView.isHidden = true
  117. })
  118. }
  119. VirusViewModel.shared.likeVirueRecordAddApi(communityPostDataModel: (self?.communityPostDataModel)!, cell: self!)
  120. }
  121. }).disposed(by: disposeBag)
  122. }
  123. private lazy var contentImageView: UIImageView = {
  124. let contentImageView = UIImageView()
  125. contentImageView.image = kImage(name: "default_pic")
  126. contentImageView.contentMode = .scaleAspectFill
  127. contentImageView.clipsToBounds = true
  128. return contentImageView
  129. }()
  130. private lazy var titleLabel: UILabel = {
  131. let titleLabel = UILabel()
  132. titleLabel.textColor = k262626Color
  133. titleLabel.font = kMediumFont13
  134. titleLabel.textAlignment = .left
  135. titleLabel.numberOfLines = 2
  136. return titleLabel
  137. }()
  138. typealias UserClosure = () -> Void
  139. var userClosure : UserClosure?
  140. private lazy var avatarButton: UIButton = {
  141. let avatarButton = UIButton(type: UIButton.ButtonType.custom)
  142. avatarButton.setImage(kImage(name: "default_avatar"), for: UIControl.State.normal)
  143. avatarButton.cornerRadius = 18/2
  144. avatarButton.masksToBounds = true
  145. return avatarButton
  146. }()
  147. private lazy var nameButton: UILabel = {
  148. let nameButton = UILabel()
  149. nameButton.text = "昵称"
  150. nameButton.textColor = k262626Color
  151. nameButton.font = kRegularFont12
  152. nameButton.textAlignment = .left
  153. nameButton.addTapGesture(1, target: self, action: #selector(nameButtonAction))
  154. return nameButton
  155. }()
  156. @objc func nameButtonAction() {
  157. if UserModel.isTokenNil() {
  158. kAppDelegate.setLogin()
  159. }else {
  160. if let userClosure = self.userClosure {
  161. userClosure()
  162. }
  163. }
  164. }
  165. lazy var likeBtn: UIButton = {
  166. let likeBtn = UIButton(type: UIButton.ButtonType.custom)
  167. likeBtn.setImage(kImage(name: "btn_praise"), for: UIControl.State.normal)
  168. likeBtn.setImage(kImage(name: "btn_praise_pre_36px"), for: UIControl.State.selected)
  169. return likeBtn
  170. }()
  171. lazy var likeLabel: UILabel = {
  172. let likeLabel = UILabel()
  173. likeLabel.textColor = k999999Color
  174. likeLabel.font = kRegularFont12
  175. likeLabel.sizeToFit()
  176. return likeLabel
  177. }()
  178. private lazy var praiseAnimationView: AnimationView = {
  179. let praiseAnimationView = AnimationView()
  180. let animation = Animation.named("verywell")
  181. praiseAnimationView.animation = animation
  182. praiseAnimationView.contentMode = .scaleAspectFit
  183. praiseAnimationView.loopMode = .playOnce
  184. praiseAnimationView.isHidden = true
  185. return praiseAnimationView
  186. }()
  187. private lazy var pauseImageView: UIImageView = {
  188. let pauseImageView = UIImageView.init(image: kImage(name: "btn_pause"))
  189. pauseImageView.isUserInteractionEnabled = true
  190. return pauseImageView
  191. }()
  192. var communityPostDataModel : CommunityPostDataModel? {
  193. didSet {
  194. contentImageView.kf.setImage(with: kURLThumbnailsImage(name: communityPostDataModel?.img ?? "",size: CGSize(width: self.size.width, height: (kScreenWidth-15)/2)), placeholder: kImage(name: "default_pic"))
  195. if communityPostDataModel?.title == "" || communityPostDataModel?.title == nil {
  196. titleLabel.text = "\(communityPostDataModel?.content?.replacingOccurrences(of: "\n", with: "").replacingOccurrences(of: "\r", with: "").prefix(20) ?? "")"
  197. }else {
  198. titleLabel.text = "\(communityPostDataModel?.title?.replacingOccurrences(of: "\n", with: "").replacingOccurrences(of: "\r", with: "").prefix(20) ?? "")"
  199. }
  200. avatarButton.kf.setImage(with: kURLImage(name: communityPostDataModel?.avatar ?? ""), for: UIControl.State.normal, placeholder: kImage(name: "default_avatar"))
  201. //KingfisherManager.shared.cache.clearMemoryCache()
  202. nameButton.text = communityPostDataModel?.username
  203. likeLabel.text = "\(communityPostDataModel?.praiseCount ?? 0)"
  204. if communityPostDataModel?.isLike == 0 {
  205. likeBtn.isSelected = false
  206. }else {
  207. likeBtn.isSelected = true
  208. }
  209. if PostType(rawValue: communityPostDataModel?.type ?? "video") == .video {
  210. pauseImageView.isHidden = false
  211. }else {
  212. pauseImageView.isHidden = true
  213. }
  214. likeLabel.sizeToFit()
  215. nameButton.snp.remakeConstraints { (make) in
  216. make.centerY.equalTo(avatarButton)
  217. make.left.equalTo(avatarButton.snp_right).offset(5)
  218. make.height.equalTo(18)
  219. make.width.equalTo((((kScreenWidth - 15)/2) - 20)-18-likeLabel.width-13-28)
  220. }
  221. }
  222. }
  223. }