ICycleView
ICycleView是一個基于UICollectionView實作的輕量級無限輪播圖
Content
- [Features]()
- [Requirements]()
- [CocoaPods]()
- [Usage]()
- [預設滾動視圖]()
- [自定義圖檔寬度和訓示器的位置和顔色]()
- [自定義Cell-純代碼和Xib建立都支援]()
- [Implementation]()
- [實作原理]()
- [主要代碼]()
- [UICollectionView代理方法]()
- [循環輪播實作]()
- [Contact]()
- [Github]()
Features
- 支援單張圖檔
- 支援滾動圖檔寬度設定
- 支援本地圖檔顯示,網路圖顯示,本地圖檔和網路圖混合顯示
- 支援自定義圖檔展示Cell(純代碼和Xib建立都支援)
- 支援UIPageControl具體位置設定
- 支援UIPageControl顯示顔色設定
- 支援圖檔點選回調
- 支援圖檔滾動回調
Requirements
- iOS 8.0+
- Swift 4.0+
CocoaPods
pod 'ICycleView', '~> 1.0.0'
在終端
pod search 'ICycleView'
時若出現
Unable to find a pod with name, author, summary, or description matching 'ICycleView'
錯誤
請在終端運作
1:
pod setup
2:
$rm ~/Library/Caches/CocoaPods/search_index.json
Usage
預設滾動視圖
// 惰性初始化滾動視圖
lazy var defaultCycleView: ICycleView = {
let cycleView = ICycleView(frame: CGRect(x: 0, y: 50, width: UIScreen.main.bounds.width, height: 130*scaleForPlus))
view.addSubview(cycleView)
return cycleView
}()
// 圖檔指派
defaultCycleView.pictures = pictures
自定義圖檔寬度和訓示器的位置和顔色
// 惰性初始化滾動視圖
lazy var customPagetrolPositionnCycleView: ICycleView = {
let cycleView = ICycleView(frame: CGRect(x: 0, y: 190, width: UIScreen.main.bounds.width, height: 130*scaleForPlus))
cycleView.imgViewWidth = 374*scaleForPlus
cycleView.pageIndicatorTintColor = .green
view.addSubview(cycleView)
return cycleView
}()
// 圖檔指派
customPagetrolPositionnCycleView.pictures = pictures
// pageControlStyle屬性必須在設定 pictures 後指派,因為訓示器是根據 numberOfPages 計算Size的
customPagetrolPositionnCycleView.pageControlStyle = .bottom(bottom: -20)
customPagetrolPositionnCycleView.pageControlStyle = .right(trailing: 30*scaleForPlus)
自定義Cell-純代碼和Xib建立都支援
// 惰性初始化滾動視圖
lazy var customPictureCellCycleView: ICycleView = {
let cycleView = ICycleView(frame: CGRect(x: 0, y: 345, width: UIScreen.main.bounds.width, height: 130*scaleForPlus))
cycleView.register([UINib.init(nibName: "CustomCycleViewCell", bundle: nil)], identifiers: ["CustomCell"])
cycleView.delegate = self
view.addSubview(cycleView)
return cycleView
}()
// 圖檔指派
customPictureCellCycleView.pictures = pictures
// 代理方法
/**
- 協定方法都是可選方法,根據需要實作即可
*/
// MARK: ICycleViewDelegate
extension ViewController: ICycleViewDelegate {
// 圖檔點選
func iCycleView(cycleView: ICycleView, didSelectItemAt index: Int) {
print("你點選了第 \(index) 張圖檔")
}
// 圖檔自動滾動
func iCycleView(cycleView: ICycleView, autoScrollingItemAt index: Int) {
print("目前滾動的圖檔是第 \(index) 張")
}
// 自定義Cell
func iCycleView(cycleView: ICycleView, collectionView: UICollectionView, cellForItemAt indexPath: IndexPath, picture: String) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCycleViewCell
cell.imgView.kf.setImage(with: URL(string: picture))
cell.titleLab.text = "自定義Cell\n第 \(indexPath.item) 張圖檔"
return cell
}
}
Implementation
實作原理
- collectionView的cell顯示兩倍數量的圖檔,展示圖檔分為兩組,預設顯示第二組的第一張
- 左滑collectionView到第二組最後一張,即最後一個cell時,設定scrollView的contentOffset顯示第一組的最後一張,繼續左滑,實作了無限左滑
- 右滑collectionView到第一組第一張,即第一cell時,設定scrollView的contentOffset顯示第二組的第一張,繼續右滑,實作了無限右滑
- 由2,3實作無限循環
主要代碼
UICollectionView代理方法
// MARK: - UICollectionViewDataSource, UICollectionViewDelegate
extension ICycleView: UICollectionViewDataSource, UICollectionViewDelegate {
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return pictures.count * 2
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if isCustomCell {
// 自定義Cell
return delegate?.iCycleView?(cycleView: self, collectionView: collectionView, cellForItemAt: IndexPath(item: indexPath.item % pictures.count, section: 0), picture: pictures[indexPath.item % pictures.count]) ?? UICollectionViewCell()
} else {
// 預設Cell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ICycleViewConst.cellIdentifier, for: indexPath) as! ICycleViewCell
cell.configureCell(picture: pictures[indexPath.item % pictures.count], placeholderImage: placeholderImage, imgViewWidth: imgViewWidth)
return cell
}
}
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
delegate?.iCycleView?(cycleView: self, didSelectItemAt: indexPath.item % pictures.count)
}
}
循環輪播實作
// MARK: - 循環輪播實作
extension ICycleView {
// 定時器方法,更新Cell位置
@objc private func updateCollectionViewAutoScrolling() {
if let indexPath = collectionView.indexPathsForVisibleItems.last {
let nextPath = IndexPath(item: indexPath.item + 1, section: indexPath.section)
collectionView.scrollToItem(at: nextPath, at: .centeredHorizontally, animated: true)
}
}
// 開始拖拽時,停止定時器
public func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
timer.fireDate = Date.distantFuture
}
// 結束拖拽時,恢複定時器
public func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
timer.fireDate = Date(timeIntervalSinceNow: autoScrollDelay)
}
/**
- 監聽手動減速完成(停止滾動)
- 1.collectionView的cell顯示兩倍數量的圖檔,展示圖檔分為兩組,預設顯示第二組的第一張
- 2.左滑collectionView到第二組最後一張,即最後一個cell時,設定scrollView的contentOffset顯示第一組的最後一張,繼續左滑,實作了無限左滑
- 3.右滑collectionView到第一組第一張,即第一cell時,設定scrollView的contentOffset顯示第二組的第一張,繼續右滑,實作了無限右滑
- 4.由2,3實作無限循環
*/
public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let offsetX = scrollView.contentOffset.x
let page = Int(offsetX / bounds.size.width)
let itemsCount = collectionView.numberOfItems(inSection: 0)
if page == 0 {
// 第一頁
collectionView.contentOffset = CGPoint(x: offsetX + CGFloat(pictures.count) * bounds.size.width, y: 0)
} else if page == itemsCount - 1 {
// 最後一頁
collectionView.contentOffset = CGPoint(x: offsetX - CGFloat(pictures.count) * bounds.size.width, y: 0)
}
}
// - 滾動動畫結束的時候調用
public func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
scrollViewDidEndDecelerating(collectionView)
}
/**
- 正在滾動
- 設定分頁,算出滾動位置,更新訓示器
*/
public func scrollViewDidScroll(_ scrollView: UIScrollView) {
let offsetX = scrollView.contentOffset.x
var page = Int(offsetX / bounds.size.width+0.5)
page = page % pictures.count
if pageControl.currentPage != page {
pageControl.currentPage = page
delegate?.iCycleView?(cycleView: self, autoScrollingItemAt: page)
}
}
}
Contact
QQ: 2256472253
Email: [email protected]