WallPaperHome722/WallpaperHD_Live/Ranking/C/WP_RankingVC.swift
2024-07-23 11:43:02 +08:00

92 lines
3.0 KiB
Swift

//
// WP_RankingVC.swift
// WallpaperHD_Live
//
// Created by 16 on 2024/7/22.
//
import UIKit
import FSPagerView
import MJExtension
import SDWebImage
class WP_RankingVC: WP_RootVC {
@IBOutlet weak var pagerView: FSPagerView!
var wallpapers = [WP_4KModel]()
override func viewDidLoad() {
super.viewDidLoad()
setJsondata()
setFSpageV()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.isHidden = true
}
func setJsondata(){
// JSON
if let path = Bundle.main.path(forResource: "4k", 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
wallpapers = (WP_4KModel.mj_objectArray(withKeyValuesArray: jsonArray) as? [WP_4KModel])!
}
} catch {
print("Error reading JSON file:", error.localizedDescription)
}
} else {
print("JSON file not found.")
}
}
func setFSpageV(){
self.pagerView.delegate = self
self.pagerView.dataSource = self
pagerView.register(UINib(nibName: "WP_RankingCell", bundle: nil), forCellWithReuseIdentifier: "WP_RankingCell")
pagerView.transformer = FSPagerViewTransformer(type: .overlap)
pagerView.automaticSlidingInterval = 0
pagerView.isInfinite = true
pagerView.decelerationDistance = 1
pagerView.itemSize = CGSize(width: UIScreen.main.bounds.size.width - 80, height: self.pagerView.frame.size.height)
// pagerView.itemSize = pagerView.frame.size
pagerView.interitemSpacing = 0
}
}
extension WP_RankingVC:FSPagerViewDelegate,FSPagerViewDataSource{
public func numberOfItems(in pagerView: FSPagerView) -> Int {
return wallpapers.count
}
public func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "WP_RankingCell", at: index) as! WP_RankingCell
let model = self.wallpapers[index]
if let imageURL = URL(string: model.thumbnail ?? "") {
cell.wallpaperImageV.sd_setImage(with: imageURL, placeholderImage: UIImage(named: "Rectangle"))
}
return cell
}
func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
let vc = WP_WallPaPerDetailsVC()
vc.type = 1
vc.model = self.wallpapers[index]
self.navigationController?.pushViewController(vc, animated: true)
}
}