// // Home_RowListsTableViewCell.swift // MusicPlayer // // Created by Mr.Zhou on 2024/3/27. // import UIKit //HomeTableViewcell展示类型 enum HomeRowCellLayout: Int { case Zero = 0 case First = 1 case Second = 2 case Third = 3 //collectionView的高度 var height:CGFloat{ switch self { case .Zero: return 140*width case .First: return 163*width case .Second: return 163*width case .Third: return 215*width } } //collecitonViewCell的Size var size:CGSize{ switch self { case .Zero: return .init(width: 166*width, height: 140*width) case .First: return .init(width: 108*width, height: 118*width) case .Second: return .init(width: 108*width, height: 118*width) case .Third: return .init(width: 340*width, height: 54*width) } } //collectionViewCell的外边距 var edgeInsets:UIEdgeInsets{ switch self { case .Zero: return .init(top: 0, left: 16*width, bottom: 0, right: 16*width) case .First: return .init(top: 0, left: 16*width, bottom: 45*width, right: 16*width) case .Second: return .init(top: 0, left: 16*width, bottom: 45*width, right: 16*width) case .Third: return .init(top: 0, left: 16*width, bottom: 17*width, right: 16*width) } } //collectionViewCell的组距 var columnDistance:CGFloat{ return 17*width } //collectionViewCell的列距 var rowDistance:CGFloat{ return 8*width } } class Home_RowListsTableViewCell: UITableViewCell { @IBOutlet weak var collectionView: UICollectionView!{ didSet{ collectionView.contentInsetAdjustmentBehavior = .never collectionView.dataSource = self collectionView.delegate = self collectionView.register(UINib(nibName: Home_FirstListCollectionViewCellID, bundle: nil), forCellWithReuseIdentifier: Home_FirstListCollectionViewCellID) collectionView.register(UINib(nibName: Home_SecondListCollectionViewCellID, bundle: nil), forCellWithReuseIdentifier: Home_SecondListCollectionViewCellID) collectionView.register(UINib(nibName: Home_FourthListCollectionViewCellID, bundle: nil), forCellWithReuseIdentifier: Home_FourthListCollectionViewCellID) } } @IBOutlet weak var heightConstraint: NSLayoutConstraint! var layout:HomeRowCellLayout = .Zero { didSet{ heightConstraint.constant = layout.height collectionView.reloadData() } } fileprivate let Home_FirstListCollectionViewCellID = "Home_FirstListCollectionViewCell" fileprivate let Home_SecondListCollectionViewCellID = "Home_SecondListCollectionViewCell" fileprivate let Home_FourthListCollectionViewCellID = "Home_FourthListCollectionViewCell" override func awakeFromNib() { super.awakeFromNib() selectionStyle = .none //添加通知 NotificationCenter.notificationKey.add(observer: self, selector: #selector(switchPlayerStateAction(_ :)), notificationName: .play_music) NotificationCenter.notificationKey.add(observer: self, selector: #selector(switchPlayerStateAction(_ :)), notificationName: .stop_music) NotificationCenter.notificationKey.add(observer: self, selector: #selector(switchPlayerStateAction(_ :)), notificationName: .resume_music) NotificationCenter.notificationKey.add(observer: self, selector: #selector(switchPlayerStateAction(_ :)), notificationName: .pause_music) } deinit{ //移除所有通知 NotificationCenter.default.removeObserver(self) } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } //当音乐播放器状态改变时 @objc private func switchPlayerStateAction(_ sender:Notification) { //回归主线程 DispatchQueue.main.async { [weak self] in //刷新页面内容 self?.collectionView.reloadData() } } } //MARK: - collectionView处理 extension Home_RowListsTableViewCell:UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { func numberOfSections(in collectionView: UICollectionView) -> Int { return layout != .Third ? 1:3 } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { switch layout { case .Zero: return LoadDataMusic.shared.homeZeroMusics.count case .First: return LoadDataMusic.shared.homeFirstMusics.count case .Second: return LoadDataMusic.shared.homeSecondMusics.count default: return LoadDataMusic.shared.homeThirdMusics[section].count } } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { switch layout { case .Zero: let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Home_FirstListCollectionViewCellID, for: indexPath) as! Home_FirstListCollectionViewCell cell.musicView = LoadDataMusic.shared.homeZeroMusics[indexPath.row] return cell case .First: let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Home_SecondListCollectionViewCellID, for: indexPath) as! Home_SecondListCollectionViewCell cell.musicView = LoadDataMusic.shared.homeFirstMusics[indexPath.row] return cell case .Second: let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Home_SecondListCollectionViewCellID, for: indexPath) as! Home_SecondListCollectionViewCell cell.musicView = LoadDataMusic.shared.homeSecondMusics[indexPath.row] return cell default: let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Home_FourthListCollectionViewCellID, for: indexPath) as! Home_FourthListCollectionViewCell cell.musicView = LoadDataMusic.shared.homeThirdMusics[indexPath.section][indexPath.row] return cell } } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return layout.size } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { return layout.edgeInsets } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { return layout.rowDistance } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { return layout.columnDistance } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { var music:MusicModel! switch layout { case .Zero: //获取一首音乐 music = LoadDataMusic.shared.homeZeroMusics[indexPath.row].music case .First: music = LoadDataMusic.shared.homeFirstMusics[indexPath.row].music case .Second: music = LoadDataMusic.shared.homeSecondMusics[indexPath.row].music default: music = LoadDataMusic.shared.homeThirdMusics[indexPath.section][indexPath.row].music } //检索当前音乐状态 guard MPMediaCenterManager.shared.getMusic()?.identifier != music.identifier else { //同一首,检索是否播放中 switch MPMediaCenterManager.shared.getPlayerState() { case .Null://没有播放 MPMediaCenterManager.shared.playerStart(music, actionType: .Normal) case .Playing://播放中 break case .Pause://继续播放 MPMediaCenterManager.shared.playerResume() } return } //新的一首 MPMediaCenterManager.shared.playerStart(music, actionType: .Normal) } }