CNLiveDownSlideRecommendCell.swift 4.8 KB
//
//  CNLiveDownSlideRecommendCell.swift
//  metaCode
//
//  Created by zx on 2023/10/20.
//  type:4 推荐课程 推荐活动

import Foundation

///推荐课程左间距
let DownSlideRecommendLeftGap: CGFloat = 15
///推荐课程宽高
let DownSlideRecommendWH: CGFloat = (KSreenWidth-DownSlideRecommendLeftGap*3)*0.5
///type5 竖版短剧图片的高
let DownSlideTwoDuanJuImageH: CGFloat = 217/150.0*DownSlideRecommendWH
///type5 竖版短剧图片的宽
let DownSlideTwoDuanJuImageW: CGFloat = (KSreenWidth-DownSlideRecommendLeftGap*5)*0.5
///type5 竖版短剧cell的高
let DownSlideTwoDuanJuH: CGFloat = 15+17+30+15+217/150.0*DownSlideRecommendWH+10+20+3+33+25+10
///type7 实时热榜的高
let DownSlideRealHotListH: CGFloat = CGFloat(DownSlideSingleCourseW*140/345.0)+154
//播放页 实时热榜的高
let DownSlideRealHotListDetaiImgH: CGFloat = CGFloat(KSreenWidth*152/375.0)
let DownSlideRealHotListDetaiCollectCellH: CGFloat = CGFloat(KSreenWidth*154/375.0)
let DownSlideRealHotListDetaiCoverH: CGFloat = CGFloat(KSreenWidth*(152-65)/375.0)//cellectCell盖住图片的高
//实时热榜详情页总高
let DownSlideRealHotListDetaiH: CGFloat = DownSlideRealHotListDetaiImgH+DownSlideRealHotListDetaiCollectCellH-DownSlideRealHotListDetaiCoverH



class CNLiveDownSlideRecommendCell: UITableViewCell {

    //slideModel
    var _slideModel : CNLiveDownSlideModel?
    var slideModel : CNLiveDownSlideModel? {
        get{
            _slideModel
        }
        set{
            _slideModel = newValue!
            setupNewUI(model: _slideModel!)
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        contentView.backgroundColor =  HexColor(kCNDownSlideBackgroundColorHex)
        contentView.clipsToBounds = true
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupNewUI(model:CNLiveDownSlideModel){
        //总块数
        let contentsNum = model.contents.count
        //model行数
        let row = model.rowNum
        //总显示块数/循环数
        var totalNum = (contentsNum >= row*2) ? row*2 : contentsNum
        if row == 0{//row为0 , contents全部显示
            totalNum = contentsNum
        }
        //lieIndex 当前是contents的第0列还是第1列
        var lieIndex = 0
        //hangIndex 当前是第几行,1...n
        var hangIndex = 1
        for i in 0..<totalNum{//i是第几行
            if lieIndex == 0{
                //显示左边的块
                let contentMdl = model.contents[(hangIndex-1)*2+(lieIndex)]
                let imageView = UIImageView(frame: CGRectMake(DownSlideRecommendLeftGap, CGFloat(hangIndex-1)*DownSlideRecommendWH+DownSlideRecommendLeftGap*CGFloat(hangIndex-1), CGFloat(DownSlideRecommendWH), CGFloat(DownSlideRecommendWH)))
                imageView.kf.setImage(with: URL(string:contentMdl.poster))
                imageView.tag = i
                imageView.layer.cornerRadius = 14
                imageView.clipsToBounds = true
                imageView.isUserInteractionEnabled = true
                imageView.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(pushWebAction)))
                contentView.addSubview(imageView)
                lieIndex = 1
            }else{
                //lieIndex==1显示右边的块
                let contentMdl = model.contents[(hangIndex-1)*2+(lieIndex)]
                let imageView = UIImageView(frame: CGRectMake(CGFloat(DownSlideRecommendLeftGap*2)+CGFloat(DownSlideRecommendWH), CGFloat(hangIndex-1)*DownSlideRecommendWH+DownSlideRecommendLeftGap*CGFloat(hangIndex-1), CGFloat(DownSlideRecommendWH), CGFloat(DownSlideRecommendWH)))
                imageView.kf.setImage(with: URL(string:contentMdl.poster))
                imageView.tag = i
                imageView.layer.cornerRadius = 14
                imageView.clipsToBounds = true
                imageView.isUserInteractionEnabled = true
                imageView.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(pushWebAction)))
                contentView.addSubview(imageView)
                lieIndex = 0
                hangIndex += 1
            }
        }
    }
    
    @objc func pushWebAction(ges: UITapGestureRecognizer){
        let imageView = ges.view as! UIImageView
        let i = imageView.tag
        let contentMdl = (self.slideModel?.contents[i])!
        CNLiveDownSlideTool.pushWebVCWithContentsMdl(model: contentMdl.model , contentId: contentMdl.contentId , pid: contentMdl.pid , shareUrl: contentMdl.shareUrl , thirdUrl: contentMdl.thirdUrl , title: contentMdl.title , s_to: contentMdl.s_to ,trailerId: contentMdl.trailerId , controller: nil, slideModel: nil)
    }
    
}