// // FS_HomeVC.swift // Funny_sounds // // Created by 忆海16 on 2024/8/14. // import UIKit import SnapKit class FS_HomeVC: RootVC { lazy var collectionView: UICollectionView = { let layout = UICollectionViewFlowLayout() let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) collectionView.backgroundColor = .clear collectionView.showsVerticalScrollIndicator = false return collectionView }() lazy var bgimage = UIImageView() lazy var hometitleImage = UIImageView() var typearr = [SoundCategory]() override func viewDidLoad() { super.viewDidLoad() setHomeUI() setcollectionView() setprankJson() } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.navigationController?.navigationBar.isHidden = true } // MARK: -设置首页布局 //#warning("设置首页布局") func setHomeUI(){ bgimage.image = UIImage(named: "homebg") bgimage.contentMode = .scaleAspectFill self.view.addSubview(bgimage) hometitleImage.image = UIImage(named: "hometitle") hometitleImage.contentMode = .scaleAspectFill self.view.addSubview(hometitleImage) collectionView.backgroundColor = .clear collectionView.showsVerticalScrollIndicator = false self.view.addSubview(collectionView) bgimage.snp.makeConstraints { make in make.width.equalTo(self.view) make.height.equalTo(self.view) } hometitleImage.snp.makeConstraints { make in make.top.equalTo(self.view.safeAreaLayoutGuide).offset(15) make.height.equalTo(30) make.width.equalTo(self.view) } collectionView.snp.makeConstraints { make in make.top.equalTo(hometitleImage.snp.bottom).offset(25) make.width.equalToSuperview() make.bottom.equalTo(self.view.safeAreaLayoutGuide) } } // MARK: -设置collectionView func setcollectionView(){ collectionView.delegate = self collectionView.dataSource = self let layout = CustomCollectionViewFlowLayout() collectionView.collectionViewLayout = layout layout.scrollDirection = .vertical collectionView.register(FS_HomeCollectionViewCell.self, forCellWithReuseIdentifier: "FS_HomeCollectionViewCell") } // MARK: - 解析json func setprankJson(){ if let path = Bundle.main.path(forResource: "prank", ofType: "json") { do { let jsonData = try Data(contentsOf: URL(fileURLWithPath: path)) if let categories = parseJSONData(jsonData: jsonData) { // 现在可以访问categories数组 self.typearr = categories } } catch { print("读取文件时出错: \(error)") } } } } // MARK: -设置collectionView代理 extension FS_HomeVC:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{ func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return typearr.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{ let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FS_HomeCollectionViewCell", for: indexPath)as!FS_HomeCollectionViewCell cell.model = typearr[indexPath.row] return cell } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){ let vc = FS_ClassificationVC() vc.typestring = self.typearr[indexPath.row].categoryName // vc.categoryarr = self.typearr var arr: [AudioFile] = [] for i in self.typearr{ switch i.categoryId{ case self.typearr[indexPath.row].categoryId: arr = i.list default: break } } vc.arr = arr navigationController?.pushViewController(vc, animated: true) } }