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

iOScollectionView廣告無(wú)限滾動(dòng)實(shí)例(Swift實(shí)現(xiàn))

 更新時(shí)間:2016年11月18日 08:50:43   作者:木子沉雨  
本篇文章主要介紹了iOScollectionView廣告無(wú)限滾動(dòng)實(shí)例,可以實(shí)現(xiàn)廣告無(wú)限滾動(dòng),有興趣的可以了解一下。

今天公司里的實(shí)習(xí)生跑過(guò)來(lái)問(wèn)我一般App上廣告的無(wú)限滾動(dòng)是怎么實(shí)現(xiàn)的,剛好很久沒(méi)寫(xiě)博客了,就決定寫(xiě)下了,盡量幫助那些處于剛學(xué)iOS的程序猿.

做一個(gè)小demo,大概實(shí)現(xiàn)效果如下圖所示:

基本實(shí)現(xiàn)思路:

1. 在你需要放置無(wú)限滾動(dòng)展示數(shù)據(jù)的地方把他的數(shù)據(jù),在原本的基礎(chǔ)上把你要展示的數(shù)據(jù)擴(kuò)大三倍.(當(dāng)然擴(kuò)大兩倍也是可以的,三倍的話,比較好演示)

  // MARK: - 設(shè)置數(shù)據(jù)源
  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//    print(self.arrayM.count)
    return self.arrayM.count * 3
  }

2.當(dāng)在定時(shí)器的作用下,或者在拖動(dòng)情況存下滾動(dòng)到第八個(gè)時(shí)候,設(shè)置此時(shí)的collectionView.contentOffset.x等于滾動(dòng)到第三個(gè)cell的contentOffset.x

if collectionView.contentOffset.x == CGFloat(3 * self.arrayM.count - 1) * self.collectionView.bounds.width {
      self.collectionView.contentOffset.x = CGFloat(self.arrayM.count - 1) * self.collectionView.bounds.width
    }

3.當(dāng)拖動(dòng)到第0個(gè)cell時(shí),設(shè)置此時(shí)的collectionView.contentOffset.x等于第六個(gè)cell的contentOffset.x

if collectionView.contentOffset.x == 0 {
      self.collectionView.contentOffset.x = CGFloat(2 * self.arrayM.count - 1) * self.collectionView.bounds.width
      
    }

代碼如下:

我在代碼中用到5張照片,所以應(yīng)該一共有15個(gè)cell

import UIKit

class ViewController: UIViewController ,UICollectionViewDataSource, UICollectionViewDelegate {

  @IBOutlet weak var collectionView: UICollectionView!
  var timer : Timer?
  var arrayM : [BOModel] = [] {
    didSet {
      self.collectionView.reloadData()
    }
  }
  static let CellID = "cell"
  override func viewDidLoad() {
    super.viewDidLoad()
    
    self.collectionView.dataSource = self
    self.collectionView.delegate = self

    // 加載數(shù)據(jù)
    loadData()
    self.collectionView.register(UINib.init(nibName: "BOCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: ViewController.CellID)
    
    //設(shè)置collextionView
    setupCollectionView()
    
    // 開(kāi)啟定時(shí)器
    starTimer()
    
  }

  /// 從polist中加載數(shù)據(jù)
  func loadData() {
    let stemp: NSArray = NSArray(contentsOfFile: Bundle.main.path(forResource: "shops.plist", ofType: nil)!)!
    
    for dict in stemp {
      let model = BOModel.init(dict: dict as! [String : Any])
      
      self.arrayM.append(model)
    }
  }
  /// 設(shè)置cellection的布局方式
  ///
  /// - Returns: 一個(gè)布局類(lèi)型
  func setupCollectionFlowlayout() -> (UICollectionViewFlowLayout) {
    let flowLayout = UICollectionViewFlowLayout()
    flowLayout.itemSize = self.collectionView.bounds.size
    flowLayout.minimumLineSpacing = 0
    flowLayout.minimumInteritemSpacing = 0
    flowLayout.scrollDirection = .horizontal
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)
    return flowLayout
  }
  
  /// 設(shè)置collectionVIew
  func setupCollectionView() -> () {
    
    self.collectionView.collectionViewLayout = self.setupCollectionFlowlayout()
    self.collectionView.showsVerticalScrollIndicator = false
    self.collectionView.showsHorizontalScrollIndicator = false
    self.collectionView.isPagingEnabled = true
    
  }
  
  // MARK: - 設(shè)置數(shù)據(jù)源
  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//    print(self.arrayM.count)
    return self.arrayM.count * 3
  }
  
  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: ViewController.CellID, for: indexPath) as! BOCollectionViewCell
    
    cell.model = self.arrayM[indexPath.row % self.arrayM.count]
    return cell
  }
  
  // MARK: - 實(shí)現(xiàn)代理方法
  func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    //contentOffset.x == 0 時(shí),重新設(shè)置contentOffset.x的值
    if collectionView.contentOffset.x == 0 {
      self.collectionView.contentOffset.x = CGFloat(2 * self.arrayM.count - 1) * self.collectionView.bounds.width
      
    }
    //當(dāng)?shù)竭_(dá)最后一個(gè)cell時(shí),重新設(shè)置contentOffset.x的值
    if collectionView.contentOffset.x == CGFloat(3 * self.arrayM.count - 1) * self.collectionView.bounds.width {
      self.collectionView.contentOffset.x = CGFloat(self.arrayM.count - 1) * self.collectionView.bounds.width
    }
    
  }
  
  
  /// 開(kāi)啟定時(shí)器
  func starTimer () {
    let timer = Timer.init(timeInterval: 1, target: self, selector: #selector(ViewController.nextPage), userInfo: nil, repeats: true)
    // 這一句代碼涉及到runloop 和 主線程的知識(shí),則在界面上不能執(zhí)行其他的UI操作
    RunLoop.main.add(timer, forMode: RunLoopMode.commonModes)
    
    self.timer = timer
    
  }
  
  /// 在1秒后,自動(dòng)跳轉(zhuǎn)到下一頁(yè)
  func nextPage() {
    // 如果到達(dá)最后一個(gè),則變成第四個(gè)
    if collectionView.contentOffset.x == CGFloat(3 * self.arrayM.count - 1) * self.collectionView.bounds.width {
      self.collectionView.contentOffset.x = CGFloat(self.arrayM.count - 1) * self.collectionView.bounds.width
    }else {
      // 每過(guò)一秒,contentOffset.x增加一個(gè)cell的寬度
      self.collectionView.contentOffset.x += self.collectionView.bounds.size.width
    }

}
  
  
  /// 當(dāng)collectionView開(kāi)始拖動(dòng)的時(shí)候,取消定時(shí)器
  func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    self.timer?.invalidate()
    self.timer = nil
  }
  
  /// 當(dāng)用戶(hù)停止拖動(dòng)的時(shí)候,開(kāi)啟定時(shí)器
  func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    starTimer()
  }
}

plist文件如下圖所示:

用到的字典轉(zhuǎn)模型因?yàn)楸容^簡(jiǎn)單的轉(zhuǎn)換,就自己寫(xiě)了個(gè):

import UIKit

class BOCollectionViewCell: UICollectionViewCell {

  @IBOutlet weak var imageView: UIImageView!
  var model : BOModel? {
    didSet {
      guard let image = UIImage.init(named: (model?.name)!) else {
        return
      }
      
      
      self.imageView.image = image
    }
  }
  override func awakeFromNib() {
    super.awakeFromNib()
 
  }

}

自定義collectionViewCell類(lèi)中的內(nèi)容:

import UIKit

class BOCollectionViewCell: UICollectionViewCell {

  @IBOutlet weak var imageView: UIImageView!
  var model : BOModel? {
    didSet {
      guard let image = UIImage.init(named: (model?.name)!) else {
        return
      }
      
      
      self.imageView.image = image
    }
  }
  override func awakeFromNib() {
    super.awakeFromNib()
 
  }

}

附: 其實(shí)這種方法比較實(shí)現(xiàn)無(wú)限滾動(dòng),利用了一點(diǎn)小技巧,用電腦測(cè)試的時(shí)候可能有一點(diǎn)缺陷.

原文鏈接:http://www.cnblogs.com/muzichenyu/p/6071757.html

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • IOS 線程死鎖詳細(xì)介紹

    IOS 線程死鎖詳細(xì)介紹

    這篇文章主要介紹了IOS 線程死鎖詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • iOS開(kāi)發(fā)中指紋識(shí)別簡(jiǎn)單介紹

    iOS開(kāi)發(fā)中指紋識(shí)別簡(jiǎn)單介紹

    指紋識(shí)別是在iOS8.0以后才推出的,所以我們?nèi)绻氚阎讣y集成到我們的APP當(dāng)中,我們首先就要在代碼中判斷iOS版本。接下來(lái)通過(guò)本文給大家分享iOS開(kāi)發(fā)中指紋識(shí)別簡(jiǎn)單介紹,需要的朋友參考下吧
    2017-11-11
  • 詳解iOS自定義UITabBar與布局

    詳解iOS自定義UITabBar與布局

    本篇文章給大家詳細(xì)分析了iOS自定義UITabBar與布局的實(shí)際操作過(guò)程以及相關(guān)代碼分享,一起學(xué)習(xí)下。
    2018-02-02
  • MacOS系統(tǒng)下Unity啟動(dòng)黑屏的解決方法

    MacOS系統(tǒng)下Unity啟動(dòng)黑屏的解決方法

    最近發(fā)現(xiàn)了一個(gè)問(wèn)題,unity一打開(kāi)就黑屏,通過(guò)查找相關(guān)的資料終于解決了,所以下面這篇文章主要給大家介紹了關(guān)于在MacOS系統(tǒng)下Unity啟動(dòng)黑屏的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。
    2018-01-01
  • 源碼解析ios開(kāi)發(fā)SDWebImage方法

    源碼解析ios開(kāi)發(fā)SDWebImage方法

    這篇文章主要為大家介紹了源碼解析ios開(kāi)發(fā)SDWebImage方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • Flutter?ScrollController滾動(dòng)監(jiān)聽(tīng)及控制示例詳解

    Flutter?ScrollController滾動(dòng)監(jiān)聽(tīng)及控制示例詳解

    這篇文章主要為大家介紹了Flutter?ScrollController滾動(dòng)監(jiān)聽(tīng)及控制示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • iOS tableView上拉刷新顯示下載進(jìn)度的問(wèn)題及解決辦法

    iOS tableView上拉刷新顯示下載進(jìn)度的問(wèn)題及解決辦法

    這篇文章主要介紹了 iOS tableView上拉刷新顯示下載進(jìn)度的問(wèn)題及解決辦法,需要的朋友可以參考下
    2017-03-03
  • iOS無(wú)障礙適配西瓜視頻Voice?Over實(shí)踐示例

    iOS無(wú)障礙適配西瓜視頻Voice?Over實(shí)踐示例

    本文從研發(fā)的視角出發(fā),講述了如何使用?Voice?Over、如何適配?Voice?Over?以及適配過(guò)程中如果遇到問(wèn)題應(yīng)該如何解決。希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • iOS讓軟鍵盤(pán)消失的簡(jiǎn)單方法

    iOS讓軟鍵盤(pán)消失的簡(jiǎn)單方法

    一些文本輸入控件等待輸入時(shí)會(huì)彈出軟鍵盤(pán),我們可以設(shè)置這些控件的Did End On Exit之類(lèi)的回調(diào)方法以在用戶(hù)點(diǎn)擊軟鍵盤(pán)上的done或return之列的按鍵時(shí)收起鍵盤(pán)
    2016-02-02
  • ios微信瀏覽器返回不刷新問(wèn)題完美解決方法

    ios微信瀏覽器返回不刷新問(wèn)題完美解決方法

    這篇文章主要介紹了ios微信瀏覽器返回不刷新問(wèn)題完美解決方法,需要的朋友可以參考下
    2017-09-09

最新評(píng)論

宿迁市| 静宁县| 宁海县| 越西县| 吉林省| 青神县| 新竹市| 定边县| 曲沃县| 西城区| 桦川县| 永吉县| 莎车县| 长治市| 辽宁省| 桃园市| 年辖:市辖区| 民县| 方城县| 长泰县| 呈贡县| 淮滨县| 桃园市| 怀宁县| 新津县| 长汀县| 江油市| 桓仁| 哈尔滨市| 松溪县| 南郑县| 全椒县| 洛川县| 平乡县| 增城市| 仲巴县| 克山县| 浦北县| 乌什县| 鄂尔多斯市| 河东区|