CNUpSlideViewController.swift 3.51 KB
//
//  CNUpSlideViewController.swift
//  metaCode
//
//  Created by zx on 2023/10/13.
//

import Foundation
import UIKit

class CNUpSlideViewController: CNLiveBaseViewController,UITableViewDelegate,UITableViewDataSource{//UITableViewController {
    
    let kCNLiveDownSlideCellIdentifier = "kCNLiveDownSlideCellIdentifier"
    
    var selectedCell: CNDownSlideCell?
    
    var statusBarShouldBeHidden = false
    //we need to set `View controller-based status bar appearance = YES` in info.plist.
    //so we can be able to hide statusBar.
    override var prefersStatusBarHidden: Bool {
        return statusBarShouldBeHidden
    }
    
    override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
        return .slide
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("slidetouchesBegan")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }

    lazy var tableView : UITableView = {
        let tableView = UITableView(frame: .zero, style: .grouped)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.separatorStyle = .none
        tableView.register(CNDownSlideCell.self, forCellReuseIdentifier: kCNLiveDownSlideCellIdentifier)
        tableView.rowHeight = GlobalConstants.toDayCardRowH
        return tableView
    }()
    
    private func updateStatusBarAndTabbarFrame(visible: Bool) {
        statusBarShouldBeHidden = !visible
        UIView.animate(withDuration: 0.25) {
            self.setNeedsStatusBarAppearanceUpdate()
        }
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveDownSlideCellIdentifier) as! CNDownSlideCell
        cell.selectionStyle = .none
        cell.bgImageView.backgroundColor = .red
        cell.bgImageView.kf.setImage(with: URL(string: "https://wjj.ys2.cnliveimg.com/769/img_new/1697162534497.jpg"))

        return cell
    }
    
    
    func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
        guard let row = tableView.cellForRow(at: indexPath) as? CNDownSlideCell else { return }
        UIView.animate(withDuration: 0.1) {
            row.bgBackView.transform = CGAffineTransform(scaleX: 0.95, y: 0.95)
        }
    }
    
    func tableView(_ tableView: UITableView, didUnhighlightRowAt indexPath: IndexPath) {
        guard let row = tableView.cellForRow(at: indexPath) as? CNDownSlideCell else { return }
        UIView.animate(withDuration: 0.3) {
            row.bgBackView.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
        }
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("点击了cell")
        guard let cell = tableView.cellForRow(at: indexPath) as? CNDownSlideCell else { return }
        selectedCell = cell

        let detailVC = CNUpSlideDetailViewController(cell: cell)
        
        detailVC.dismissClosure = { [weak self] in
            guard let StrongSelf = self else { return }
            StrongSelf.updateStatusBarAndTabbarFrame(visible: true)
        }
        
        updateStatusBarAndTabbarFrame(visible: false)
        
        present(detailVC, animated: true, completion: nil)

    }
}