https://github.com/gsdios/SDCycleScrollView
https://github.com/gsdios/SDCycleScrollView
使用collectionView实现无线轮播的大概思路是一直切换当前的cell(就是一个一个往下滑),当你滑到最后一个的时候让cell马上切换到所有cell的中部(totalItemsCount / 2)然后在数据源中你需要展示的那些数据就依次循环展示(index % self.noticeArray.count,index表示Indexpath.item)
代码只展示切换的逻辑,像collection的创建,layout的创建,代理和数据源就不详细的展示了
//noticeArray表示装有所有数据的数组
var noticeArray : [String]
//这里有collectionViewCell的复用不用考虑性能问题
fileprivate lazy var totalItemsCount:Int = {
return self.noticeArray.count * 100
}()
//设置自动滚动计时器
func configureAutoScrollTimer() {
//设置一个定时器,每三秒钟滚动一次
self.srollTimer = Timer.scheduledTimer(timeInterval: 3, target: self,
selector: #selector(letItScroll),
userInfo: nil, repeats: true)
RunLoop.main.add(self.srollTimer!, forMode: .commonModes)
}
func currentIndex() -> Int {
if (noticeCollectionView?.frame.size.width == 0 || noticeCollectionView?.frame.size.height == 0) {
return 0
}
var index = 0
//这里是纵向滑动
index = Int(((self.noticeCollectionView?.contentOffset.y)! + self.rolllayout.itemSize.height * 0.5) / (self.rolllayout.itemSize.height))
//横向滑动
// index = Int(((self.noticeCollectionView?.contentOffset.x)! + self.rolllayout.itemSize.width * 0.5) / (self.rolllayout.itemSize.width))
return max(0, index)
}
func scrollToIndex( targetIndex:inout Int) {
if targetIndex >= totalItemsCount {
targetIndex = totalItemsCount / 2
noticeCollectionView?.scrollToItem(at: IndexPath.init(row: targetIndex, section: 0), at: UICollectionViewScrollPosition.init(rawValue: 0), animated: false)
return
}
noticeCollectionView?.scrollToItem(at: IndexPath.init(row: targetIndex, section: 0), at: UICollectionViewScrollPosition.init(rawValue: 0), animated: true)
}
//计时器时间一到
func letItScroll(){
if self.totalItemsCount == 0 {
return
}
let currentIndex = self.currentIndex()
var targetIndex = currentIndex + 1
self.scrollToIndex(targetIndex: &targetIndex)
}
func pageControlIndexWithCurrentCellIndex( index : Int) -> Int {
return index % self.noticeArray.count
}
extension AZRollNoticeView : UICollectionViewDelegate{
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let index = self.pageControlIndexWithCurrentCellIndex(index: indexPath.item)
self.letSelectBlock!(index)
}
func scrollViewDidScroll(_ scrollView: UIScrollView){
let itemIndex = self.currentIndex()
//当前位置信息
self.titleCount = self.pageControlIndexWithCurrentCellIndex(index: itemIndex)
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
self.configureAutoScrollTimer()
}
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
self.stopTimer()
}
}
extension AZRollNoticeView : UICollectionViewDataSource{
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) ->
UICollectionViewCell {
self.indexPath = indexPath
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as!AZRollNoticeCell
let index = self.pageControlIndexWithCurrentCellIndex(index: indexPath.item)
cell.textLabel?.text = self.noticeArray[index]
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return totalItemsCount
}
}
本文记录自己的理解,有什么不认同的东西直接参考这篇文章
https://github.com/gsdios/SDCycleScrollView
网友评论