CNLiveHomeLanmuCell.swift 3.1 KB
//
//  CNLiveHomeLanmuCell.swift
//  metaCode
//
//  Created by zx on 2025/2/20.
//

import Foundation

typealias CNLiveHomeLanmuImgClickBlock = (_ imgModel:CNLiveDownSlideContentsModel)->Void;

class CNLiveHomeLanmuCell:UITableViewCell{
    
    var imgClickBlock : CNLiveHomeLanmuImgClickBlock?
    //slideModel
    var _slideModel : CNLiveDownSlideModel?
    var slideModel : CNLiveDownSlideModel? {
        get{
            _slideModel
        }
        set{
            _slideModel = newValue!
            for obj in contentView.subviews{
                if obj.tag > 100{
                    obj.removeFromSuperview()
                }
            }
            self.setupNewUI(model: slideModel!)
        }
    }
        
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
        
    private func setupUI() {
        contentView.backgroundColor = .clear
        self.backgroundColor = .clear
        self.contentView.clipsToBounds = true
    }
    
    func setupNewUI(model:CNLiveDownSlideModel){
        var showNum:Int = model.contents.count>=4 ? 4 : model.contents.count
        for (idx,mdl) in model.contents.enumerated(){
            if idx >= 4 { break}
            
            let leftPadding:CGFloat = 30.0
            let imgWH:CGFloat = 44.0
            let jianJuPadding:CGFloat = (KSreenWidth-2*leftPadding-CGFloat(showNum)*imgWH)/CGFloat(showNum-1)
            let imgX:CGFloat = leftPadding+CGFloat(idx)*(imgWH+jianJuPadding)
            let imgY:CGFloat = 0.0
            let iconImgView = UIImageView(frame: CGRect(x: imgX, y: imgY, width: imgWH, height: imgWH))
            iconImgView.contentMode = .scaleAspectFill
            iconImgView.tag = 1000+idx
            iconImgView.kf.setImage(with: URL(string: mdl.poster))
            iconImgView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(iconImgViewAction)))
            iconImgView.isUserInteractionEnabled = true
            self.contentView.addSubview(iconImgView)
            
            
            let titleLbl = UILabel(frame: CGRect(x: 0, y: iconImgView.bottom+2.5, width: 50, height: 20))
            titleLbl.centerX = iconImgView.centerX
            titleLbl.font = Font_15
            titleLbl.textColor = HexColor("#000102")
            titleLbl.text = mdl.title
            titleLbl.tag = 1000+idx
            self.contentView.addSubview(titleLbl)
            titleLbl.snp.makeConstraints { make in
                make.centerX.equalTo(iconImgView.snp.centerX)
                make.top.equalTo(iconImgView.snp.bottom).offset(2.5)
            }
        }

    }
    @objc func iconImgViewAction(ges: UITapGestureRecognizer){
        showLoading(message: "")
        let imageView = ges.view as! UIImageView
        let index = imageView.tag-1000
        if let slideModel = self.slideModel?.contents[index]{
            self.imgClickBlock?(slideModel)
        }
    }
    
}