CNLiveShortVideoListTableViewCell.swift 16.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
//
//  CNLiveShortVideoListTableViewCell.swift
//  metaCode
//
//  Created by open on 2024/1/5.
//

import Foundation
import ZFPlayer
import Kingfisher
import APButton

protocol CNLiveShortVideoListTableViewCellDelegate : AnyObject {
    func likeActionMethod(viewcell: CNLiveShortVideoListTableViewCell, button: UIButton)
    func commentActionMethod(viewcell: CNLiveShortVideoListTableViewCell, button: UIButton)
    func shareActionMethod(viewcell: CNLiveShortVideoListTableViewCell, button: UIButton)
    func iconActionMethod(viewcell: CNLiveShortVideoListTableViewCell, button: UIButton)
    func faviActionMethod(viewcell: CNLiveShortVideoListTableViewCell, button: UIButton)
    func listActionMethod(viewcell: CNLiveShortVideoListTableViewCell, button: UIButton)
    func payActionMethod(viewcell: CNLiveShortVideoListTableViewCell, button: UIButton)
}

class CNLiveShortVideoListTableViewCell: UITableViewCell {
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.white
        label.font = semibold_adapt_font(pointSize: 19)
        label.textAlignment = .left
        label.numberOfLines = 1
        label.lineBreakMode = .byTruncatingTail
        return label
    }()
    
    lazy var descLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.white
        label.font = regular_adapt_font(pointSize: 15)
        label.textAlignment = .left
        label.numberOfLines = 3
        label.lineBreakMode = .byTruncatingTail
        return label
    }()
    
    lazy var tipsLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.white
        label.font = semibold_adapt_font(pointSize: 15)
        label.textAlignment = .center
        label.numberOfLines = 0
        label.isHidden = true
        return label
    }()
    
    lazy var listBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.setTitleColor(.white, for: .normal)
        button.contentHorizontalAlignment = .left
        button.contentEdgeInsets = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 0)
        button.titleLabel?.textAlignment = .left
        button.titleLabel?.lineBreakMode = .byTruncatingTail
        button.titleLabel?.font = semibold_adapt_font(pointSize: 15)
        button.layer.cornerRadius = 10
        button.layer.masksToBounds = true
        button.backgroundColor = HexColor("#414141")
        button.addTarget(self, action: #selector(listMethod), for: .touchUpInside)
        return button
    }()
    
    lazy var iconBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "mine_sub_about_app_logo"), for: .normal)
        button.layer.cornerRadius = 25
        button.layer.masksToBounds = true
        button.layer.borderColor = UIColor.white.cgColor
        button.layer.borderWidth = 1
        button.backgroundColor = .clear
        button.addTarget(self, action: #selector(iconMothod), for: .touchUpInside)
        return button
    }()
    
    lazy var faviBtn: APButton = {
        let button = APButton()
        button.setImage(UIImage(named: "short_video_guan_zhu"), for: .normal)
        button.layer.cornerRadius = 10
        button.layer.masksToBounds = true
        button.backgroundColor = .clear
        button.activityIndicator.color = .white
        button.addTarget(self, action: #selector(faviMothod), for: .touchUpInside)
        return button
    }()
    
    lazy var topBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "short_video_to_top"), for: .normal)
        button.backgroundColor = .clear
        button.isUserInteractionEnabled = false
        return button
    }()

    lazy var likeBtn: ImageButton = {
        let button = ImageButton(type: .custom)
        button.setImage(UIImage(named: "short_video_zan_nomal"), for: .normal)
        button.setImage(UIImage(named: "short_video_zan_select"), for: .selected)
        button.setTitle("22.8w", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.titleLabel?.font = light_adapt_font(pointSize: 15)
        button.addTarget(self, action: #selector(likeMothod), for: .touchUpInside)
        return button
    }()

    lazy var commentBtn: ImageButton = {
        let button = ImageButton(type: .custom)
        button.setImage(UIImage(named: "short_video_comment"), for: .normal)
        button.setTitle("232.8w", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.titleLabel?.font = light_adapt_font(pointSize: 15)
        button.addTarget(self, action: #selector(commentMothod), for: .touchUpInside)
        return button
    }()

    lazy var shareBtn: ImageButton = {
        let button = ImageButton(type: .custom)
        button.setImage(UIImage(named: "short_video_share"), for: .normal)
        button.setTitle("0.8w", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.titleLabel?.font = light_adapt_font(pointSize: 15)
        button.addTarget(self, action: #selector(shareMothod), for: .touchUpInside)
        return button
    }()
    
    lazy var payView: UIView = {
        let payView = setupWithPayView()
        payView.isHidden = true
        return payView
    }()
    
    //控制器承载图
    lazy var coverImageView: UIImageView = {
        let coverImageView = UIImageView()
        coverImageView.isUserInteractionEnabled = true
        coverImageView.tag = 100
        coverImageView.contentMode = .scaleAspectFit
        coverImageView.image = UIImage(named: "short_video_default")
        return coverImageView
    }()
    
    var homeModel: CNLiveAlbumListModel!
    weak var delegate: CNLiveShortVideoListTableViewCellDelegate!

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        contentView.backgroundColor = .clear
        backgroundColor = .clear
        selectionStyle = .none
        
        contentView.addSubview(coverImageView)
        contentView.addSubview(shareBtn)
        contentView.addSubview(commentBtn)
        contentView.addSubview(likeBtn)
        contentView.addSubview(iconBtn)
        contentView.addSubview(faviBtn)
        contentView.addSubview(descLabel)
        contentView.addSubview(titleLabel)
        contentView.addSubview(listBtn)
        listBtn.addSubview(topBtn)
        contentView.addSubview(tipsLabel)
        contentView.addSubview(payView)
    }
    
    func setupWithData(model: CNLiveAlbumListModel) {
        homeModel = model
        coverImageView.kf.indicatorType = .activity
        coverImageView.kf.setImage(with: URL(string: model.poster), placeholder: UIImage(named: "short_video_default"))
        iconBtn.kf.setImage(with: URL(string: model.icon!.face), for: .normal)
        likeBtn.setTitle(model.tool?.likesNum, for: .normal)
        commentBtn.setTitle(model.tool?.commentsNum, for: .normal)
        shareBtn.setTitle(model.tool?.shareNum, for: .normal)
        likeBtn.isSelected = model.tool!.liked
        titleLabel.text = model.shortTitle
        descLabel.text = model.title
        listBtn.setTitle(model.albumMsgModel?.albumName, for: .normal)
        
        let imageV: UIImageView = payView.viewWithTag(1000) as! UIImageView
        imageV.kf.indicatorType = .activity
        imageV.kf.setImage(with: URL(string: model.poster))
        let titleL: UILabel = payView.viewWithTag(1001) as! UILabel
        titleL.text = model.title
        let timeL: UILabel = payView.viewWithTag(1002) as! UILabel
        timeL.text = model.duration
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        coverImageView.frame = contentView.bounds
        shareBtn.snp.makeConstraints { [weak self] make in
            guard let self = self else { return }
            make.bottom.equalTo(self.snp.bottom).offset(-(70 + get_system_tabbar_ui_offsetHeight()))
            make.right.equalTo(self.snp.right).offset(-20)
            make.width.equalTo(55)
            make.height.equalTo(50)
        }
        commentBtn.snp.makeConstraints { [weak self] make in
            guard let self = self else { return }
            make.bottom.equalTo(shareBtn.snp.top).offset(-20)
            make.right.equalTo(self.snp.right).offset(-20)
            make.width.equalTo(55)
            make.height.equalTo(50)
        }
        likeBtn.snp.makeConstraints { [weak self] make in
            guard let self = self else { return }
            make.bottom.equalTo(commentBtn.snp.top).offset(-20)
            make.right.equalTo(self.snp.right).offset(-20)
            make.width.equalTo(55)
            make.height.equalTo(50)
        }
        iconBtn.snp.makeConstraints { [weak self] make in
            guard let self = self else { return }
            make.bottom.equalTo(likeBtn.snp.top).offset(-20)
            make.right.equalTo(self.snp.right).offset(-20)
            make.width.height.equalTo(50)
        }
        faviBtn.snp.makeConstraints { [weak self] make in
            guard let self = self else { return }
            make.centerX.equalTo(self.iconBtn)
            make.bottom.equalTo(self.iconBtn).offset(10)
            make.height.width.equalTo(20)
        }
        descLabel.snp.makeConstraints {[weak self] make in
            guard let self = self else { return }
            make.bottom.equalTo(self.snp.bottom).offset(-(70 + get_system_tabbar_ui_offsetHeight()))
            make.left.equalTo(self.snp.left).offset(20)
            make.right.equalTo(shareBtn.snp.left).offset(-10)
        }
        titleLabel.snp.makeConstraints { [weak self] make in
            guard let self = self else { return }
            make.bottom.equalTo(descLabel.snp.top).offset(-10)
            make.left.equalTo(self.snp.left).offset(20)
            make.right.equalTo(shareBtn.snp.left).offset(-10)
        }
        listBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(descLabel.snp.bottom).offset(20)
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.bottom.equalToSuperview().offset(-get_system_tabbar_ui_offsetHeight())
        }
        topBtn.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-15)
        }
        tipsLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-30)
        }
        payView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.bottom.equalTo(listBtn.snp.top).offset(-10)
            make.height.equalTo(165)
        }
    }
    
    open func hiddenView(isHidden: Bool) {
        if (isHidden) {
            self.commentBtn.isHidden = true
            self.faviBtn.isHidden = true
            self.shareBtn.isHidden = true
            self.likeBtn.isHidden = true
            self.iconBtn.isHidden = true
            self.titleLabel.isHidden = true
            self.descLabel.isHidden = true
            self.tipsLabel.isHidden = false
            self.payView.isHidden = false
        } else {
            self.commentBtn.isHidden = false
            self.faviBtn.isHidden = false
            self.shareBtn.isHidden = false
            self.likeBtn.isHidden = false
            self.iconBtn.isHidden = false
            self.titleLabel.isHidden = false
            self.descLabel.isHidden = false
            self.tipsLabel.isHidden = true
            self.payView.isHidden = true
        }
    }
    
    @objc func likeMothod(button: UIButton) {
        self.delegate.likeActionMethod(viewcell: self, button: button)
    }
    
    @objc func commentMothod(button: UIButton) {
        self.delegate.commentActionMethod(viewcell: self, button: button)
    }
    
    @objc func shareMothod(button: UIButton) {
        self.delegate.shareActionMethod(viewcell: self, button: button)
    }
    
    @objc func iconMothod(button: UIButton) {
        self.delegate.iconActionMethod(viewcell: self, button: button)
    }
    
    @objc func faviMothod(button: UIButton) {
        self.delegate.faviActionMethod(viewcell: self, button: button)
    }
    
    @objc func listMethod(button: UIButton) {
        self.delegate.listActionMethod(viewcell: self, button: button)
    }
    
    @objc func payMethod(button: UIButton) {
        self.delegate.payActionMethod(viewcell: self, button: button)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
        print("cell界面销毁了")
    }
    
}

extension CNLiveShortVideoListTableViewCell {
    
    private func setupWithPayView() -> UIView {
        let view = UIView.init()
        view.backgroundColor = HexColor("#414141")
        view.layer.cornerRadius = 10
        view.layer.masksToBounds = true
        
        let imageView = UIImageView.init()
        imageView.contentMode = .scaleAspectFill
        imageView.layer.cornerRadius = 5
        imageView.layer.masksToBounds = true
        imageView.clipsToBounds = true
        imageView.tag = 1000
        view.addSubview(imageView)
        imageView.snp.makeConstraints { make in
            make.left.top.equalToSuperview().offset(20)
            make.width.equalTo(50)
            make.height.equalTo(70)
        }
        
        let titleLabel = UILabel.init()
        titleLabel.textColor = .white
        titleLabel.lineBreakMode = .byTruncatingTail
        titleLabel.numberOfLines = 2
        titleLabel.tag = 1001
        titleLabel.font = medium_adapt_font(pointSize: 12)
        view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(imageView.snp.right).offset(10)
            make.top.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
        }
        
        let timeLabel = UILabel.init()
        timeLabel.textColor = .white
        timeLabel.tag = 1002
        timeLabel.font = light_adapt_font(pointSize: 13)
        view.addSubview(timeLabel)
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(imageView.snp.right).offset(10)
            make.bottom.equalTo(imageView.snp.bottom)
            make.right.equalToSuperview().offset(-20)
        }
        
        let lineLabel = UILabel.init()
        lineLabel.backgroundColor = HexColor("#686868")
        view.addSubview(lineLabel)
        lineLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.height.equalTo(1)
            make.top.equalTo(imageView.snp.bottom).offset(10)
            make.right.equalToSuperview().offset(-20)
        }
        
        let payButton = APButton()
        payButton.setTitle("840积分解锁全42集", for: .normal)
        payButton.setTitleColor(.white, for: .normal)
        payButton.layer.cornerRadius = 20
        payButton.activityIndicator.color = .white
        payButton.layer.masksToBounds = true
        payButton.viewWithTag(1003)
        payButton.backgroundColor = HexColor("#67C5FF")
        payButton.titleLabel?.font = medium_adapt_font(pointSize: 15)
        payButton.addTarget(self, action: #selector(payMethod), for: .touchUpInside)
        view.addSubview(payButton)
        payButton.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.width.equalTo(200)
            make.height.equalTo(40)
            make.top.equalTo(lineLabel.snp.bottom).offset(10)
        }
        payButton.setGradient(colors: [UIColor(red: 103/255.0, green: 197/255.0, blue: 255/255.0, alpha: 0.3),UIColor(red: 96/255.0, green: 154/255.0, blue: 255/255.0, alpha: 0.3)], startPoint: CGPoint(x: 0, y: 0.5), endPoint: CGPoint(x: 1, y: 0.5))
        return view
    }
}

class ImageButton: UIButton {
    override func layoutSubviews() {
        super.layoutSubviews()

        guard let imageView = self.imageView, let titleLabel = self.titleLabel else {
            return
        }
        let imageRect = CGRect(x: (self.bounds.width - imageView.bounds.width) / 2, y: 0, width: imageView.bounds.width, height: imageView.bounds.height)
        imageView.frame = imageRect
        let titleRect = CGRect(x: 0, y: imageRect.maxY + 3.0, width: self.bounds.width, height: titleLabel.bounds.height)
        titleLabel.frame = titleRect
        titleLabel.textAlignment = .center
    }
    override func setTitle(_ title: String?, for state: UIControl.State) {
        super.setTitle(title, for: state)
        setNeedsLayout()
    }
    override func setImage(_ image: UIImage?, for state: UIControl.State) {
        super.setImage(image, for: state)
        setNeedsLayout()
    }
}