我有collectionView,其中每个单元格都是一个页面(单元格在纵向模式下占据整个屏幕,在横向模式下占据2个单元格)。我正在尝试添加当前页码/总页数(即 1/30)到目前为止我得到的 1/0 --> 1 是当前页码,0 是总页数。
有两个问题:
- 只有当我开始滚动时才会显示总页数
- 当我滚动当前页码不会改变。
我使用的代码:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if pageControl.currentPage < pages.count {
pageControl.currentPage += 1
numberOfPagesLabel.text = "\(pageControl.currentPage + 1)/\(pages.count)"
} else if pageControl.currentPage != 0 {
pageControl.currentPage -= 1
numberOfPagesLabel.text = "\(pageControl.currentPage - 1)/\(pages.count)"
}
}
class DetailsViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate {
private var collectionView: UICollectionView!
let pageControl = UIPageControl (frame: CGRect(x: 20, y: 120, width: 44, height: 44))
let numberOfPagesLabel = UILabel (frame: CGRect(x: 20, y: 0, width: 44, height: 44))
private var pages = [Pages]() {
didSet {
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
numberOfPagesLabel.layer.cornerRadius = 15.0
numberOfPagesLabel.clipsToBounds = true
numberOfPagesLabel.center.x = self.view.center.x
numberOfPagesLabel.center.y = self.view.center.y+350
self.view.addSubview(collectionView)
self.view.addSubview(pageControl)
self.view.addSubview(numberOfPagesLabel)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
collectionViewLayout()
collectionView.delegate = self
collectionView.dataSource = self
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if pageControl.currentPage < pages.count {
pageControl.currentPage += 1
numberOfPagesLabel.text = "\(pageControl.currentPage + 1)/\(pages.count)"
} else if pageControl.currentPage != 0 {
pageControl.currentPage -= 1
numberOfPagesLabel.text = "\(pageControl.currentPage - 1)/\(pages.count)"
}
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return pages.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "colCell7", for: indexPath) as! colCell7
let book = pages[indexPath.row]
cell.setupCell(for: book)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if UIWindow.isLandscape {
return CGSize(width: (collectionView.frame.size.width)/2, height: (collectionView.frame.size.height))
}
else { return CGSize(width: (collectionView.frame.size.width), height: (collectionView.frame.size.height))
}
}
func collectionViewLayout() {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: 0, height: 0), collectionViewLayout: layout)
collectionView.backgroundColor = .white
collectionView.showsHorizontalScrollIndicator = false
collectionView.showsVerticalScrollIndicator = false
collectionView.isPagingEnabled = true
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.register(colCell7.self, forCellWithReuseIdentifier: "colCell7")
collectionView.isScrollEnabled = true
collectionView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
layout.itemSize = CGSize(width: view.frame.width, height: view.frame.height)
layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0
layout.itemSize = CGSize(width: view.frame.width/2, height:view.frame.height )
}
}