prank/Funny_sounds/Home/C/FS_HomeVC.swift
2024-09-03 09:38:34 +08:00

152 lines
4.4 KiB
Swift

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