CNUpSlideDetailScrollView.swift 7.86 KB
//
//  CNUpSlideDetailScrollView.swift
//  metaCode
//
//  Created by zx on 2023/10/16.
//

import UIKit

typealias enterBlock = (_ contentsMdl:CNLiveDownSlideContentsModel)->Void
    
class CNUpSlideDetailScrollView: UIScrollView {
    //返回首页
    var callBack : enterBlock?
    
    let bgBackView = UIView()
    let imageView = UIImageView()

    //slideModel
    var _slideModel : CNLiveDownSlideModel?
    var slideModel : CNLiveDownSlideModel? {
        get{
            _slideModel
        }
        set{
            _slideModel = newValue!
            self.setupNewUI(model: _slideModel!)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
        
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        bgBackView.frame = CGRect(x: DownSlideRecommendLeftGap, y: 0,width:KSreenWidth , height:KSreenHeight)
        imageView.frame = CGRect(x: 0,y: 0,width:KSreenWidth , height: KSreenWidth*DownSlideSingleCourseImgH/DownSlideSingleCourseW)
        imageView.contentMode = .scaleAspectFill
                        
        bgBackView.addSubview(imageView)
        addSubview(bgBackView)
        
        contentSize = CGSize(width: KSreenWidth, height: bgBackView.height)
    }
    
    func setupNewUI(model:CNLiveDownSlideModel){
        if model.type == SlideModelTypeAllCourse12 {
            //全部课程列表
            let topTitleLbl = UILabel(frame: CGRect(x: DownSlideRecommendLeftGap, y: KStatusBarHeight+DownSlideRecommendLeftGap, width: DownSlideSingleCourseW-DownSlideRecommendLeftGap*2, height: 30))
            topTitleLbl.text = model.blockName ?? "全部课程"
            topTitleLbl.font = BoldFont_21
            topTitleLbl.textColor = HexColor("#333333")
            bgBackView.addSubview(topTitleLbl)
            
            //总块数
            let contentsNum = model.contents.count
            //model行数
            let row = model.rowNum
            //总显示块数
            var totalNum = (contentsNum >= row) ? row : contentsNum
            if row == 0{//row为0 , contents全部显示
                totalNum = contentsNum
            }
            for i in 0..<totalNum{//i是第几行
                let contentMdl = model.contents[i]
                
                //小icon
                let imageView = UIImageView(frame: CGRectMake(DownSlideRecommendLeftGap, topTitleLbl.bottom+DownSlideRecommendLeftGap+CGFloat(i)*(DownSlideAllCourseRowTopYGap+DownSlideAllCourseRowImageWH+DownSlideAllCourseRowBottomYGap), CGFloat(DownSlideAllCourseRowImageWH), CGFloat(DownSlideAllCourseRowImageWH)))
                imageView.kf.setImage(with: URL(string:contentMdl.poster ?? ""))
                bgBackView.addSubview(imageView)
                
                let titleLbl = UILabel(frame: CGRect(x: imageView.right+DownSlideRecommendLeftGap, y: imageView.top, width: DownSlideSingleCourseW-90-imageView.right, height: 21))
                titleLbl.text = contentMdl.title ?? ""
                titleLbl.font = Font_15
                titleLbl.textColor = HexColor("#333333")
                bgBackView.addSubview(titleLbl)
                
                let subTitleLbl = UILabel(frame: CGRect(x: titleLbl.left, y:titleLbl.bottom+3 , width: titleLbl.width, height: 17))
                subTitleLbl.text = contentMdl.subTitle ?? ""
                subTitleLbl.font = Font_12
                subTitleLbl.textColor = HexColor("#999999")
                bgBackView.addSubview(subTitleLbl)
                
                let enterBtn = UIButton(type: .custom)
                enterBtn.frame = CGRect(x: subTitleLbl.right+10, y: 0, width: 65, height: 30)
                enterBtn.centerY = imageView.centerY
                enterBtn.setBackgroundImage(UIImage(named: "homepage_down_enter"), for: .normal)
                enterBtn.setTitle("进入", for: .normal)
                enterBtn.titleLabel?.font = BoldFont_13
                enterBtn.setTitleColor(HexColor("#FFFFFF"), for: .normal)
                enterBtn.addTarget(self, action: #selector(enterBtnAction(_:)), for: .touchUpInside)
                enterBtn.tag = i
                bgBackView.addSubview(enterBtn)
            }
        }else if model.type == SlideModelTypeOneCourse75 {
            //单图课程
            
            let topTitleLbl = UILabel(frame: CGRect(x: 0, y: KStatusBarHeight+DownSlideRecommendLeftGap, width:bgBackView.width - 50 , height: 30))
            topTitleLbl.text = model.contents[0].title ?? ""
            topTitleLbl.font = BoldFont_21
            topTitleLbl.textColor = HexColor("#FFFFFF")
            imageView.addSubview(topTitleLbl)
            
            let topSubTitleLbl = UILabel(frame: CGRect(x: topTitleLbl.left, y:topTitleLbl.bottom , width: topTitleLbl.width, height: 30))
            topSubTitleLbl.text = model.contents[0].subTitle ?? ""
            topSubTitleLbl.font = Font_12
            topSubTitleLbl.textColor = HexColor("#FFFFFF")
            imageView.addSubview(topSubTitleLbl)
            
            //总块数
            let contentsNum = model.contents.count
            //model行数
            let row = model.rowNum
            //总显示块数
            var totalNum = (contentsNum >= row) ? row : contentsNum
            if row == 0{//row为0 , contents全部显示
                totalNum = contentsNum
            }
            for i in 0..<totalNum{//i是第几行
                let contentMdl = model.contents[i]
                
                //小icon
                let imgView = UIImageView(frame: CGRectMake(0, imageView.height+DownSlideRecommendLeftGap+CGFloat(i)*(DownSlideAllCourseRowTopYGap+DownSlideAllCourseRowImageWH+DownSlideAllCourseRowBottomYGap), CGFloat(DownSlideAllCourseRowImageWH), CGFloat(DownSlideAllCourseRowImageWH)))
                imgView.kf.setImage(with: URL(string:contentMdl.poster ?? ""))
                bgBackView.addSubview(imgView)
                
                let titleLbl = UILabel(frame: CGRect(x: imgView.right+DownSlideRecommendLeftGap, y: imgView.top, width: DownSlideSingleCourseW-90-imgView.right, height: 21))
                titleLbl.text = contentMdl.title ?? ""
                titleLbl.font = Font_15
                titleLbl.textColor = HexColor("#333333")
                bgBackView.addSubview(titleLbl)
                
                let subTitleLbl = UILabel(frame: CGRect(x: titleLbl.left, y:titleLbl.bottom+3 , width: titleLbl.width, height: 17))
                subTitleLbl.text = contentMdl.subTitle ?? ""
                subTitleLbl.font = Font_12
                subTitleLbl.textColor = HexColor("#999999")
                bgBackView.addSubview(subTitleLbl)
                
                let enterBtn = UIButton(type: .custom)
                enterBtn.frame = CGRect(x: subTitleLbl.right+10, y: 0, width: 65, height: 30)
                enterBtn.centerY = imgView.centerY-5
                enterBtn.setBackgroundImage(UIImage(named: "homepage_down_enter"), for: .normal)
                enterBtn.setTitle("进入", for: .normal)
                enterBtn.titleLabel?.font = BoldFont_13
                enterBtn.setTitleColor(HexColor("#FFFFFF"), for: .normal)
                enterBtn.addTarget(self, action: #selector(enterBtnAction(_:)), for: .touchUpInside)
                enterBtn.tag = i
                bgBackView.addSubview(enterBtn)
            }
        }
    }
  
    //闭包:函数参数
//    func callBackBlock(_ block: @escaping enterBlock) {
//        block(_:)
//    }
//    func useBlock(){
//        if callBack != nil {
//            callBack!("闭包传值2")
//        }
//    }
    //MARK: - 事件
    @objc func enterBtnAction(_ sender: UIButton){
        let tag = sender.tag
        let contentMdl = (self.slideModel?.contents[tag])!
        if callBack != nil {
            let contentMdl1 = contentMdl
            callBack!(contentMdl1)
        }
    }
}