Music_Player3/MusicPlayer/MP/Views/Home(音乐资源列表-播放器)/Home_RowListsTableViewCell.swift
Mr.zhou 96147c5e37 项目:Musicoo
版本:A面 1.0
构建:1.1
更新内容:对项目A面功能的实现,经测试确定各项功能无问题。
更新时间:2024年4月12日 11:20
上传状态:已上传App Connect
2024-04-12 11:19:58 +08:00

191 lines
8.3 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// 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
}
}
//collecitonViewCellSize
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)
}
}