有個(gè)需求,要求自定義滾動(dòng)指示器,花了半小時(shí),還是記錄下。作為工具,直接使用。

pic.gif
- 無論是 tableView, CollectionView, 還是ScrollView ,只需要拿出他們的
contentOffset
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let offset: CGPoint = self.mCollection.contentOffset
let index = offset.x / self.mCollection.frame.width
scrollBar.updateFrame(index: index)
}
-
scrollBar 是我自己寫的一個(gè)xib的控件 ,很簡(jiǎn)單如圖
A81854A7EFB5AD7C60A1F2E495ECC2D7.png - scrollBar里面實(shí)現(xiàn)代碼如下
func updateFrame(index: CGFloat) {
var frame = indicatorView.frame
frame.origin.x = index * (bgView.width - indicatorView.width)
indicatorView.frame = frame
}
如有需要,可向我要代碼。
