麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 系統 > iOS > 正文

iOS Swift利用UICollectionView實現無限輪播功能(原理)詳解

2019-10-21 18:39:37
字體:
來源:轉載
供稿:網友

前言

作為一個資深(自認為)iOS程序猿,會經常用到輪播圖,上一次使用UIScrollView實現無限輪播的效果,這一次在Swift語言中,我使用UICollectionView再為大家講解一次無限輪播的實現原理。

先上圖:

iOS,Swift,UICollectionView,無限輪播
UICollectionView-無限輪播.gif

首先需要實現了就是UICollectionView的分頁,這個很簡單:

collectionView.isPagingEnabled = true

接下來就是原理,在UICollectionView的兩端需要先添加兩張圖片,首段需要添加最后一張圖片,而尾端需要添加第一張圖片,然后在中間的位置上一次添加各個圖片。這個其實是很容易實現的:

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCollectionViewCell", for: indexPath) as! ImageCollectionViewCell  /// 給圖片賦值(在首尾分別添加兩張圖片) if (indexPath.row == 0) {  cell.imageName = imageNameList.last } else if (indexPath.row == self.imageNameList.count + 1) {  cell.imageName = imageNameList.first } else {  cell.imageName = imageNameList[indexPath.row - 1] }  return cell }

這樣在滑動的時候,通過偏移量就可以實現無限輪播的效果了。當滑動停止時判斷偏移量,當偏移量為0時(視圖上顯示的是最后一張圖片),這時候就直接調動調整偏移量的方法,把UICollectionView偏移到最后一張圖片的位置?;瑒拥轿捕藭r是同理。

 func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { /// 當UIScrollView滑動到第一位停止時,將UIScrollView的偏移位置改變 if (scrollView.contentOffset.x == 0) {  scrollView.contentOffset = CGPoint(x: CGFloat(self.imageNameList.count) * kScreenWidth,y: 0)  self.pageControl.currentPage = self.imageNameList.count  /// 當UIScrollView滑動到最后一位停止時,將UIScrollView的偏移位置改變 } else if (scrollView.contentOffset.x == CGFloat(self.imageNameList.count + 1) * kScreenWidth) {  scrollView.contentOffset = CGPoint(x: kScreenWidth,y: 0)  self.pageControl.currentPage = 0 } else {  self.pageControl.currentPage = Int(scrollView.contentOffset.x / kScreenWidth) - 1 } }

其實原理很簡單,個人認為使用UICollectionView實現無限輪播比起UIScrollView更加實用并且便于維護,接下來我將代碼全部列一下:

import UIKitlet kScreenWidth = UIScreen.main.bounds.widthclass ViewController: UIViewController {  lazy var collectionView: UICollectionView = { let flowLayout = UICollectionViewFlowLayout() flowLayout.minimumLineSpacing = 0 flowLayout.minimumInteritemSpacing = 0 flowLayout.scrollDirection = .horizontal flowLayout.itemSize = CGSize(width: kScreenWidth, height: 200)  let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 200), collectionViewLayout: flowLayout)  collectionView.isPagingEnabled = true collectionView.showsHorizontalScrollIndicator = false collectionView.backgroundColor = UIColor.white collectionView.delegate = self collectionView.dataSource = self self.view.addSubview(collectionView)  return collectionView }()  lazy var pageControl: UIPageControl = { let pageControl = UIPageControl(frame: CGRect(x: 0, y: 150, width: kScreenWidth, height: 50))  pageControl.numberOfPages = self.imageNameList.count pageControl.currentPage = 0  pageControl.tintColor = UIColor.black pageControl.pageIndicatorTintColor = UIColor.gray;  return pageControl; }()  lazy var imageNameList: [String] = { let imageList = ["image0", "image1", "image2", "image3"]  return imageList }() override func viewDidLoad() { super.viewDidLoad()  setupController() }  func setupController() { /// 設置數據 collectionView.register(ImageCollectionViewCell.self, forCellWithReuseIdentifier: "ImageCollectionViewCell")  collectionView.reloadData() collectionView.scrollToItem(at: IndexPath(row: 1, section: 0), at: .left, animated: false)  self.view.addSubview(pageControl) }}extension ViewController: UICollectionViewDataSource {  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { /// 這步只是防止崩潰 if (imageNameList.count == 0) {  return 0 } return imageNameList.count + 2 }  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCollectionViewCell", for: indexPath) as! ImageCollectionViewCell  /// 給圖片賦值(在首尾分別添加兩張圖片) if (indexPath.row == 0) {  cell.imageName = imageNameList.last } else if (indexPath.row == self.imageNameList.count + 1) {  cell.imageName = imageNameList.first } else {  cell.imageName = imageNameList[indexPath.row - 1] }  return cell } }extension ViewController: UICollectionViewDelegate {  func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { /// 當UIScrollView滑動到第一位停止時,將UIScrollView的偏移位置改變 if (scrollView.contentOffset.x == 0) {  scrollView.contentOffset = CGPoint(x: CGFloat(self.imageNameList.count) * kScreenWidth,y: 0)  self.pageControl.currentPage = self.imageNameList.count  /// 當UIScrollView滑動到最后一位停止時,將UIScrollView的偏移位置改變 } else if (scrollView.contentOffset.x == CGFloat(self.imageNameList.count + 1) * kScreenWidth) {  scrollView.contentOffset = CGPoint(x: kScreenWidth,y: 0)  self.pageControl.currentPage = 0 } else {  self.pageControl.currentPage = Int(scrollView.contentOffset.x / kScreenWidth) - 1 } } }/// collectionView圖片的cellclass ImageCollectionViewCell: UICollectionViewCell {  /// 顯示的圖片 let imageView = UIImageView() var imageName: String? = "" { didSet {  if let name = imageName {  imageView.image = UIImage(named: name)  } } }  override init(frame: CGRect) { super.init(frame: frame)  setupCell(); }  /// 初始化視圖 func setupCell() { imageView.frame = self.bounds contentView.addSubview(imageView) }  required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }

 

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網的支持。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 91麻豆蜜桃一区二区三区 | 久久久中文 | 国产亚洲精品久久久久5区 综合激情网 | 婷婷久久网 | 亚洲骚综合 | 国内精品视频饥渴少妇在线播放 | 成人不卡免费视频 | 国产女做a爱免费视频 | 韩国一大片a毛片 | www.成人免费 | 欧美重口另类videos人妖 | 亚洲免费高清 | 亚洲第一页综合 | 一级国产航空美女毛片内谢 | 国产亚洲精品综合一区91 | 毛片a级毛片免费播放100 | 一区二区三区视频播放 | 欧美成年私人网站 | 亚洲成人欧美在线 | 久久亚洲成人 | www.91操| 在线观看国产一区二区三区 | 精品国内视频 | 久久艹精品 | 久久久久久久一区二区 | 一级毛片在线观看视频 | 国内成人自拍视频 | www.91操| 国产精品视频一区二区三区四 | 黄色网址免费在线播放 | 国产精品久久久久久久久久尿 | 日本免费a∨ | 91精品观看91久久久久久国产 | 中文字幕一二区 | 五月天影院,久久综合, | 99热久草| 免费黄色大片在线观看 | 成人国产精品色哟哟 | 激情亚洲一区二区 | 91久久99热青草国产 | 精品国产一区二区三区四区阿崩 |