123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- //
- // RecommendSimilarCollectionCell.swift
- // RainbowPlanet
- //
- // Created by Christopher on 2019/6/14.
- // Copyright © 2019 RainbowPlanet. All rights reserved.
- // 相关推荐のCollectionCell
- import UIKit
- import RxSwift
- import Lottie
- import Kingfisher
- class SearchContentListCollectionCell: UICollectionViewCell {
- deinit {
- NXLLog("deinit")
- }
-
- let disposeBag = DisposeBag()
-
- typealias LikeSelectBlock = (_ isLiked: Int) -> Void
- var likeSelectBlock : LikeSelectBlock?
-
- class func cellWith(collectionView:UICollectionView,indexPath:IndexPath) -> SearchContentListCollectionCell {
- let ID = "RecommendSimilarCollectionCell"
- collectionView.register(SearchContentListCollectionCell.self, forCellWithReuseIdentifier: ID)
- let cell : SearchContentListCollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: ID, for: indexPath) as! SearchContentListCollectionCell
- cell.indexPath = indexPath
- return cell
- }
- //MARK: - indexPath
- var indexPath: IndexPath?{
- didSet {
- if indexPath?.row == 0 {
- contentImageView.snp.remakeConstraints { (make) in
- make.top.left.right.equalToSuperview()
- make.height.equalTo((kScreenWidth-15)/2)
- }
- }else {
- contentImageView.snp.remakeConstraints { (make) in
- make.top.left.right.equalToSuperview()
- make.height.equalTo(240*kScaleWidth)
- }
- }
- }
- }
- //MARK: - 初始化
- override init(frame: CGRect) {
- super.init(frame: frame)
- backgroundColor = kffffffColor
- setupViews()
- setupLayouts()
- setupdata()
- }
-
- required init?(coder aDecoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
-
- //MRAK: - 设置View
- private func setupViews() {
- self.cornerRadius = 4
- self.masksToBounds = true
- addSubview(contentImageView)
- addSubview(titleLabel)
- addSubview(avatarButton)
- addSubview(nameButton)
- addSubview(likeBtn)
- addSubview(likeLabel)
- contentImageView.addSubview(pauseImageView)
- likeBtn.imageView?.addSubview(praiseAnimationView)
- }
-
- private func setupLayouts() {
- contentImageView.snp.makeConstraints { (make) in
- make.top.left.right.equalToSuperview()
- }
- titleLabel.snp.makeConstraints { (make) in
- make.top.equalTo(contentImageView.snp_bottom).offset(15)
- make.left.equalTo(contentImageView.snp_left).offset(10)
- make.right.equalTo(contentImageView.snp_right).offset(-10)
- make.height.lessThanOrEqualTo(37)
- }
- avatarButton.snp.makeConstraints { (make) in
- make.top.equalTo(titleLabel.snp_bottom).offset(10)
- make.left.equalToSuperview().offset(10)
- make.size.equalTo(18)
- }
-
- nameButton.snp.makeConstraints { (make) in
- make.centerY.equalTo(avatarButton)
- make.left.equalTo(avatarButton.snp_right).offset(5)
- make.height.equalTo(18)
- make.right.equalTo(likeBtn.snp_left).offset(-5)
- }
-
- likeLabel.snp.makeConstraints { (make) in
- make.centerY.equalTo(avatarButton)
- make.right.equalTo(-13)
- make.left.equalTo(likeBtn.snp.right)
- }
-
- likeBtn.snp.makeConstraints { (make) in
- make.centerY.equalTo(avatarButton)
- make.right.equalTo(likeLabel.snp.left).offset(-3)
- make.size.equalTo(18)
- }
-
-
-
- praiseAnimationView.snp.makeConstraints { (make) in
- make.edges.equalToSuperview()
- }
- pauseImageView.snp_makeConstraints { (make) in
- make.center.equalToSuperview()
- }
-
- }
-
- func setupdata() {
- avatarButton.rx.tap.subscribe(onNext: {
- [weak self] (data) in
- self?.nameButtonAction()
-
- }).disposed(by: disposeBag)
-
- likeBtn.rx.tap.subscribe(onNext: { [weak self] (data) in
- if UserModel.isTokenNil() {
- kAppDelegate.setLogin()
- }else {
- if self?.communityPostDataModel?.isLike == 0 {
- self?.praiseAnimationView.isHidden = false
- self?.praiseAnimationView.play(completion: { [weak self] (_) in
- self?.praiseAnimationView.isHidden = true
- })
- }
- VirusViewModel.shared.likeVirueRecordAddApi(communityPostDataModel: (self?.communityPostDataModel)!, cell: self!)
- }
- }).disposed(by: disposeBag)
- }
-
- private lazy var contentImageView: UIImageView = {
- let contentImageView = UIImageView()
- contentImageView.image = kImage(name: "default_pic")
- contentImageView.contentMode = .scaleAspectFill
- contentImageView.clipsToBounds = true
- return contentImageView
- }()
-
- private lazy var titleLabel: UILabel = {
- let titleLabel = UILabel()
- titleLabel.textColor = k262626Color
- titleLabel.font = kMediumFont13
- titleLabel.textAlignment = .left
- titleLabel.numberOfLines = 2
- return titleLabel
- }()
-
- typealias UserClosure = () -> Void
- var userClosure : UserClosure?
-
- private lazy var avatarButton: UIButton = {
- let avatarButton = UIButton(type: UIButton.ButtonType.custom)
- avatarButton.setImage(kImage(name: "default_avatar"), for: UIControl.State.normal)
- avatarButton.cornerRadius = 18/2
- avatarButton.masksToBounds = true
-
- return avatarButton
- }()
-
-
- private lazy var nameButton: UILabel = {
- let nameButton = UILabel()
- nameButton.text = "昵称"
- nameButton.textColor = k262626Color
- nameButton.font = kRegularFont12
- nameButton.textAlignment = .left
- nameButton.addTapGesture(1, target: self, action: #selector(nameButtonAction))
- return nameButton
- }()
-
- @objc func nameButtonAction() {
- if UserModel.isTokenNil() {
- kAppDelegate.setLogin()
- }else {
- if let userClosure = self.userClosure {
- userClosure()
- }
- }
- }
-
- lazy var likeBtn: UIButton = {
- let likeBtn = UIButton(type: UIButton.ButtonType.custom)
- likeBtn.setImage(kImage(name: "btn_praise"), for: UIControl.State.normal)
- likeBtn.setImage(kImage(name: "btn_praise_pre_36px"), for: UIControl.State.selected)
-
- return likeBtn
- }()
-
- lazy var likeLabel: UILabel = {
- let likeLabel = UILabel()
- likeLabel.textColor = k999999Color
- likeLabel.font = kRegularFont12
- likeLabel.sizeToFit()
- return likeLabel
- }()
-
- private lazy var praiseAnimationView: AnimationView = {
- let praiseAnimationView = AnimationView()
- let animation = Animation.named("verywell")
- praiseAnimationView.animation = animation
- praiseAnimationView.contentMode = .scaleAspectFit
- praiseAnimationView.loopMode = .playOnce
- praiseAnimationView.isHidden = true
- return praiseAnimationView
- }()
-
- private lazy var pauseImageView: UIImageView = {
- let pauseImageView = UIImageView.init(image: kImage(name: "btn_pause"))
- pauseImageView.isUserInteractionEnabled = true
- return pauseImageView
- }()
-
- var communityPostDataModel : CommunityPostDataModel? {
- didSet {
- contentImageView.kf.setImage(with: kURLThumbnailsImage(name: communityPostDataModel?.img ?? "",size: CGSize(width: self.size.width, height: (kScreenWidth-15)/2)), placeholder: kImage(name: "default_pic"))
-
- if communityPostDataModel?.title == "" || communityPostDataModel?.title == nil {
- titleLabel.text = "\(communityPostDataModel?.content?.replacingOccurrences(of: "\n", with: "").replacingOccurrences(of: "\r", with: "").prefix(20) ?? "")"
- }else {
- titleLabel.text = "\(communityPostDataModel?.title?.replacingOccurrences(of: "\n", with: "").replacingOccurrences(of: "\r", with: "").prefix(20) ?? "")"
- }
- avatarButton.kf.setImage(with: kURLImage(name: communityPostDataModel?.avatar ?? ""), for: UIControl.State.normal, placeholder: kImage(name: "default_avatar"))
- //KingfisherManager.shared.cache.clearMemoryCache()
- nameButton.text = communityPostDataModel?.username
- likeLabel.text = "\(communityPostDataModel?.praiseCount ?? 0)"
- if communityPostDataModel?.isLike == 0 {
- likeBtn.isSelected = false
- }else {
- likeBtn.isSelected = true
- }
- if PostType(rawValue: communityPostDataModel?.type ?? "video") == .video {
- pauseImageView.isHidden = false
- }else {
- pauseImageView.isHidden = true
- }
- likeLabel.sizeToFit()
- nameButton.snp.remakeConstraints { (make) in
- make.centerY.equalTo(avatarButton)
- make.left.equalTo(avatarButton.snp_right).offset(5)
- make.height.equalTo(18)
- make.width.equalTo((((kScreenWidth - 15)/2) - 20)-18-likeLabel.width-13-28)
- }
-
- }
- }
- }
|