Wallpaper_Home/wallpaper_project/Rank/C/WA_RankVC.swift
忆海16 5ff1070967 mtg
2024-07-23 11:44:01 +08:00

93 lines
3.4 KiB
Swift

//
// WA_RankVC.swift
// wallpaper_project
import UIKit
import MJExtension
class WA_RankVC: WA_RootVC {
@IBOutlet weak var collectionView: UICollectionView!
var wallpapers = [WA_3DModel]()
override func viewDidLoad() {
super.viewDidLoad()
let layout = CustomLayout()
collectionView.collectionViewLayout = layout
collectionView.backgroundColor = .white
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(UINib(nibName: "WA_RankCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "WA_RankCollectionViewCell")
setJsondata()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.isHidden = true
}
func setJsondata(){
// JSON
if let path = Bundle.main.path(forResource: "rank", ofType: "json") {
do {
let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
// 使 MJExtension JSON
let jsonArray = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [[String: Any]]
if let jsonArray = jsonArray {
// 使 MJExtension JSON
let wallpapersarr = (WA_3DModel.mj_objectArray(withKeyValuesArray: jsonArray) as? [WA_3DModel])!
self.wallpapers = wallpapersarr
}
} catch {
print("Error reading JSON file:", error.localizedDescription)
}
} else {
print("JSON file not found.")
}
}
}
extension WA_RankVC:UICollectionViewDataSource, UICollectionViewDelegate{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return wallpapers.count //
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WA_RankCollectionViewCell", for: indexPath) as! WA_RankCollectionViewCell
cell.model = self.wallpapers[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vc = WA_LiveVideoVC()
vc.model = self.wallpapers[indexPath.row]
navigationController?.pushViewController(vc, animated: true)
}
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
guard let collectionView = collectionView else { return }
let itemHeight = collectionView.bounds.height * 2.5 / 3
let currentOffset = scrollView.contentOffset.y
let targetOffset = targetContentOffset.pointee.y
let currentItemIndex = round(currentOffset / itemHeight)
let targetItemIndex = targetOffset > currentOffset ? currentItemIndex + 1 : currentItemIndex - 1
let newOffsetY = targetItemIndex * itemHeight
targetContentOffset.pointee.y = newOffsetY
}
}