2020 年完全正确的方法:
只需UIPageControl在情节提要中添加一个。
将它放在您的收藏视图下方(即顶部可见)。

链接到它...
class YourVC: UIViewController, UICollectionViewDelegate,
UICollectionViewDataSource,
UICollectionViewDelegateFlowLayout {
@IBOutlet var collectionView: UICollectionView!
@IBOutlet var dots: UIPageControl!
只需在集合视图中添加一个水平居中的约束,然后添加一个约束来对齐底部。
这将给出标准的定位/间距。
(当然,您可以将点放在您想要的任何位置,但这是标准。)

提示 1 - 颜色
奇怪的是,点的默认颜色是……清晰!
所以将它们设置为灰色/黑色或任何你想要的:

或者你可以在代码中做到这一点:
override func viewDidLoad() {
super.viewDidLoad()
dots.pageIndicatorTintColor = .systemGray5
dots.currentPageIndicatorTintColor = .yourCorporateColor
}
下一个。在numberOfItemsInSection,添加...
func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
let k = ... yourData.count, or whatever your count is
dots.numberOfPages = k
return k
}
提示 2 - 事实上,不要使用减速呼叫
添加此代码:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
dots.currentPage = Int(
(collectionView.contentOffset.x / collectionView.frame.width)
.rounded(.toNearestOrAwayFromZero)
)
)
}
您只需在“scrollViewDidScroll”中设置页面。
实际上
•不要使用 scrollViewWillBeginDecelerating
•不要使用 scrollViewDidEndDecelerating。
To see why: try it using either of those calls. Now skim quickly through many pages. Notice it does not work properly.
Simply use scrollViewDidScroll for the correct, perfect result, including initialization.
Tip 3 - do NOT use Int division - it completely changes the behavior and is totally wrong.
You will often see this example code:
// wrong, do not do this
dots.currentPage = Int(collectionView.contentOffset.x) /
Int(collectionView.frame.width)
// wrong, do not do this
That often-seen example code is completely wrong.
If you try that, it will result in the dots "jumping" in a non-standard way, as you skim through pages.
Best explanation is to try it and see.
For the usual, correct, Apple-style behavior as you scroll through or skim through the pages, the code is:
dots.currentPage = Int(
(collectionView.contentOffset.x / collectionView.frame.width)
.rounded(.toNearestOrAwayFromZero)
)
Final example...
