该问题与this question structure有关。我将pageControl放在集合视图下面的tableView xib上。
在tableView单元格中,我这样写:
class AccountTypeTableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate, UIPageViewControllerDelegate {
weak var cellDelegate:CustomCollectionCellDelegate?
weak var myParent:AccountTypeViewController?
@IBOutlet weak var collectionAccountTypeView: UIView!
@IBOutlet weak var lblAccountTypeTitle: UILabel!
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var pageControl: UIPageControl!
@IBAction func pageControllerAction(_ sender: UIPageControl) {
print("currentPage:\(sender.currentPage)")
self.collectionView.scrollToItem(at: IndexPath(row: sender.currentPage, section: 0), at: .centeredHorizontally, animated: true)
}
func scrollViewDidScroll(_ scrollView: UIScrollView)
{
let pageWidth = scrollView.frame.width
let currentPages = Int((scrollView.contentOffset.x + pageWidth / 2) / pageWidth)
print("scrollViewDidScroll-\(pageWidth)-\(currentPages)")
self.pageControl.currentPage = currentPages
print("current page\(pageControl.currentPage)-\(currentPages)")
}}
当我点击pageControl点时,它可以自动滚动到目标集合视图-> @IBAction func pageControllerAction(_ sender:UIPageControl)
但是当我滚动/滑动集合视图时,pageControl活动点没有更改-> self.pageControl.currentPage = currentPages(没有用)
当我记录(打印)print(“ scrollViewDidScroll-(pageWidth)-(currentPages)”)时,我正确地获得了currentPages。
如何在scrollViewDidEndDecelerating之后使pageControl活动点更改,因为我尝试了scrollViewDidEndDecelerating,也无法正常工作。
override func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let x = scrollView.contentOffset.x
let w = scrollView.bounds.size.width
AccountTypeTableViewCell.customCell.scrollViewDidScroll(scrollView)
}
问题出在我登录
print("current page\(pageControl.currentPage)-\(currentPages)")
currentPages的结果是正确的,但pageControl.currentPage始终为0。我不知道为什么pageControl.currentPage甚至没有添加currentPages也不会更新
self.pageControl.updateCurrentPageDisplay()