最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2018年09月29日 08:59:02   作者:楓developer  
無線輪播圖的實現(xiàn)方式有很多,下面這篇文章主要給大家介紹了關(guān)于iOS Swift利用UICollectionView實現(xiàn)無限輪播功能(原理)的相關(guān)資料,需要的朋友可以參考下

前言

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

先上圖:


UICollectionView-無限輪播.gif

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

collectionView.isPagingEnabled = true

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

 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
 }

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

 func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
 /// 當(dāng)UIScrollView滑動到第一位停止時,將UIScrollView的偏移位置改變
 if (scrollView.contentOffset.x == 0) {
  scrollView.contentOffset = CGPoint(x: CGFloat(self.imageNameList.count) * kScreenWidth,y: 0)
  self.pageControl.currentPage = self.imageNameList.count
  /// 當(dāng)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
 }
 }

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

import UIKit

let kScreenWidth = UIScreen.main.bounds.width

class 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() {
 /// 設(shè)置數(shù)據(jù)
 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) {
 /// 當(dāng)UIScrollView滑動到第一位停止時,將UIScrollView的偏移位置改變
 if (scrollView.contentOffset.x == 0) {
  scrollView.contentOffset = CGPoint(x: CGFloat(self.imageNameList.count) * kScreenWidth,y: 0)
  self.pageControl.currentPage = self.imageNameList.count
  /// 當(dāng)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圖片的cell
class 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")
 } 
}

ok,喜歡的話可以點一下收藏哈,用UIScrollView實現(xiàn)輪播的原理在:http://www.fzitv.net/article/148185.htm,大家需要的話也可以了解一下。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • iOS中searchBar(搜索框)光標(biāo)初始位置后移

    iOS中searchBar(搜索框)光標(biāo)初始位置后移

    這篇文章主要介紹了iOS中searchBar(搜索框)光標(biāo)初始位置后移的關(guān)鍵代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-08-08
  • iOS布局渲染之UIView方法的調(diào)用時機詳解

    iOS布局渲染之UIView方法的調(diào)用時機詳解

    在你剛開始開發(fā) iOS 應(yīng)用時,最難避免或者是調(diào)試的就是和布局相關(guān)的問題,下面這篇文章主要給大家介紹了關(guān)于iOS布局渲染之UIView方法調(diào)用時機的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2018-07-07
  • Swift和Objective-C 混編注意事項

    Swift和Objective-C 混編注意事項

    這篇文章主要介紹了Swift和Objective-C 混編注意事項的相關(guān)資料,需要的朋友可以參考下
    2016-10-10
  • ios的collection控件的自定義布局實現(xiàn)與設(shè)計

    ios的collection控件的自定義布局實現(xiàn)與設(shè)計

    這篇文章主要介紹了mac、iOS端支持自定義布局的collection控件的實現(xiàn)與設(shè)計,需要的朋友學(xué)習(xí)參考下吧。
    2017-12-12
  • 適配iPhoneXS max和iPhoneX R的方法示例

    適配iPhoneXS max和iPhoneX R的方法示例

    這篇文章主要介紹了適配iPhoneXS max和iPhoneX R的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-10-10
  • Objective-C中字符串的拼接方法小結(jié)

    Objective-C中字符串的拼接方法小結(jié)

    這篇文章主要介紹了Objective-C中字符串的拼接方法小結(jié),除了依靠NSString,文中還介紹了在宏里拼接字符串的方法,需要的朋友可以參考下
    2016-02-02
  • iOS實現(xiàn)PDF文件瀏覽功能

    iOS實現(xiàn)PDF文件瀏覽功能

    這篇文章主要為大家詳細(xì)介紹了iOS實現(xiàn)PDF文件瀏覽功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • IOS獲取緩存文件的大小并清除緩存文件的方法

    IOS獲取緩存文件的大小并清除緩存文件的方法

    今天通過本文給大家介紹的離線緩存的功能實現(xiàn),主要分為緩存文件大小的獲取、清除緩存文件的實現(xiàn)。本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,感興趣的朋友一起看看吧
    2016-10-10
  • iOS中利用UIBezierPath + CAAnimation實現(xiàn)心跳動畫效果

    iOS中利用UIBezierPath + CAAnimation實現(xiàn)心跳動畫效果

    這篇文章主要給大家介紹了關(guān)于iOS中利用UIBezierPath + CAAnimation實現(xiàn)心跳動畫效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的日常開發(fā)具有一定的參考學(xué)習(xí),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-10-10
  • iOS開發(fā)之觸摸事件

    iOS開發(fā)之觸摸事件

    iOS設(shè)備都是可以多點觸摸的,是指手指放在iOS設(shè)備的屏幕上從屏幕上拖動或抬起。系統(tǒng)當(dāng)前視圖響應(yīng)觸摸事件,若無響應(yīng)則向上層傳遞,構(gòu)成響應(yīng)者鏈。觸摸事件的函數(shù)有4個。
    2016-04-04

最新評論

安康市| 武定县| 桂平市| 大英县| 泽州县| 平邑县| 丹寨县| 沙田区| 怀远县| 措美县| 聂拉木县| 桂平市| 扎囊县| 紫阳县| 芦山县| 夹江县| 郁南县| 韩城市| 安宁市| 平江县| 德安县| 十堰市| 峡江县| 醴陵市| 兰西县| 忻州市| 民勤县| 蒙自县| 洞口县| 永清县| 自贡市| 普陀区| 高陵县| 襄城县| 哈巴河县| 云南省| 明光市| 宁波市| 军事| 德兴市| 巨鹿县|