需求如下:
(1)无限循环轮播
(2)定时轮播
(3)添加PageControl
import UIKit
class HomePageBannerView: UIView {
//MARK: - 属性 & 事件
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var pageControl: UIPageControl!
let ItemCountTimes = Int(100)//item数的倍数
let bannerViewHeight = kScreenWidth*(116.0/375.0)
lazy var layout: UICollectionViewFlowLayout = {
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0
layout.sectionInset = UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0)
layout.scrollDirection = .horizontal
layout.itemSize = CGSize.init(width: kScreenWidth, height: bannerViewHeight)
return layout
}()
var dataSource: [AdvListModel]? {
didSet {
guard dataSource != nil else {
return
}
collectionView.reloadData()
collectionView.scrollToItem(at: IndexPath.init(row: dataSource!.count*(ItemCountTimes/2), section: 0), at: UICollectionViewScrollPosition.left, animated: false)
pageControl.numberOfPages = dataSource?.count ?? 0
pageControl.currentPage = 0
self.addTimer1()
}
}
var timer:Timer?
//MARK: - 生命周期
override func awakeFromNib() {
super.awakeFromNib()
self.prepareUI()
}
deinit {
self.removeTimer()
}
//MARK: - UI设置
func prepareUI() {
collectionView.isPagingEnabled = true
collectionView.showsHorizontalScrollIndicator = false
collectionView.showsVerticalScrollIndicator = false
collectionView.delegate = self
collectionView.dataSource = self
let nib = UINib(nibName: "HomePageBannerCell", bundle: nil)
collectionView.register(nib, forCellWithReuseIdentifier: "HomePageBannerCell")
collectionView.setCollectionViewLayout(layout, animated: true)
pageControl.hidesForSinglePage = true
}
}
// MARK: - UICollectionViewDelegate,UICollectionViewDataSource
extension HomePageBannerView: UICollectionViewDelegate,UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return (dataSource?.count ?? 0)*ItemCountTimes
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomePageBannerCell", for: indexPath) as! HomePageBannerCell
let model = dataSource![indexPath.item%dataSource!.count]
cell.model = model
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
}
}
// MARK: - UIScrollViewDelegate
extension HomePageBannerView: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
//页书为0 不需要设置page
guard (self.dataSource?.count ?? 0) > 0 else {
return
}
let page = Int((scrollView.contentOffset.x/scrollView.frame.size.width+0.5))%self.dataSource!.count
self.pageControl.currentPage = page
}
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
self.removeTimer()
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
self.addTimer1()
}
}
// MARK: - 定时器相关
extension HomePageBannerView {
//添加timer
func addTimer1() {
self.timer = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
}
//销毁timer
func removeTimer() {
self.timer?.invalidate()
self.timer = nil
}
//selector方法
@objc func timerAction() {
guard (self.dataSource?.count ?? 0) > 0 else {
return
}
//获取当前正在展示的位置
let currentIndexPath = self.collectionView.indexPathsForVisibleItems.last
//回到中间相应的显示位置
let currentIndexPathRest = IndexPath.init(item: (dataSource!.count*(ItemCountTimes/2))+(currentIndexPath!.item%dataSource!.count), section: 0)
self.collectionView.scrollToItem(at: currentIndexPathRest, at: .left, animated: false)
//通过动画滚动到下一个位置
let nextItem = currentIndexPathRest.item+1
let nextIndexPath = IndexPath.init(item: nextItem, section: 0)
self.collectionView.scrollToItem(at: nextIndexPath, at: .left, animated: true)//回到中间相应的显示位置
}
}