欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Swift實(shí)現(xiàn)無限輪播效果

 更新時間:2017年03月10日 14:22:27   作者:豆丶漿油條  
這篇文章主要為大家詳細(xì)介紹了Swift無限輪播效果實(shí)現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

從今天開始,我學(xué)習(xí)的重點(diǎn)開始轉(zhuǎn)向Swift,并且會分享一些自己學(xué)習(xí)的心得體會,今天給大家?guī)淼牡氖菬o限輪播。廣告頁的無限輪播是非常常見的一個功能,大多數(shù)APP都有,大多數(shù)程序員也都實(shí)現(xiàn)過,今天我們用Swift實(shí)現(xiàn)一下。項(xiàng)目地址

圖片切換我們可以選擇的基本控件有兩個UIScrollView 和 UICollectionView,這次我們選擇UICollectionView;既然是輪播,就會用到Timer。所以,我們這次主要應(yīng)用的知識點(diǎn)為UICollectionView 和 Timer;

import UIKit

class CycleScrollView: UIView, UICollectionViewDelegate,UICollectionViewDataSource {

  var bottomView : UICollectionView?
  var width : CGFloat?
  var height : CGFloat?
  var timer : Timer?
  
  override init(frame: CGRect){
    
    super.init(frame: frame)
    // 1.設(shè)置背景色
    self.backgroundColor = UIColor.clear
    // 2.設(shè)置寬高
    width = self.frame.size.width
    height = self.frame.size.height
    // 3.添加bottomView
    setupBottomView()
    // 4.添加定時器
    setupTimer()
  }
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
  
  
  func setupBottomView() {
    
    // 5.設(shè)置collectionView的布局
    let flowLayout = UICollectionViewFlowLayout();
    flowLayout.itemSize = self.bounds.size
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirection.horizontal;
    bottomView = UICollectionView.init(frame: self.bounds, collectionViewLayout: flowLayout)
    self.addSubview(bottomView!);
    // 6.設(shè)置collectionView的尺寸
    bottomView?.contentSize = CGSize(width:width! * CGFloat(4),height:height!)
    // 7.分頁
    bottomView?.isPagingEnabled = true
    // 8.去掉滾動條
    bottomView?.showsVerticalScrollIndicator = false
    bottomView?.showsHorizontalScrollIndicator = false
    // 9.設(shè)置代理
    bottomView?.delegate = self
    bottomView?.dataSource = self
    // 10.注冊cell
    bottomView?.register(UICollectionViewCell().classForCoder, forCellWithReuseIdentifier: "ID");
    if #available(iOS 10.0, *) {
      // 11.預(yù)加載
      bottomView?.isPrefetchingEnabled = true
    } else {
      // Fallback on earlier versions
    }
  }
  func setupTimer() {
    // 12.實(shí)例化定時器
    timer = Timer.init(timeInterval: 2, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true);
    RunLoop.main.add(timer!, forMode: RunLoopMode.defaultRunLoopMode);

    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
    
      self.timer?.fire();
    }
  }
  func timerAction() {
    
    var contentOffsetX = (self.bottomView?.contentOffset.x)! + self.frame.size.width
    
    if contentOffsetX > self.frame.size.width * 3 {
      // 當(dāng)前視圖顯示的是第三個的時候,設(shè)置bottomView的偏移量為0
      self.bottomView?.contentOffset = CGPoint(x:0,y:0)
      contentOffsetX = self.frame.size.width
    }
     self.bottomView?.setContentOffset(CGPoint(x:contentOffsetX,y:0), animated: true)
  }
  // 重寫removeFromSuperview方法,用于刪除定時器,否則定時器一直存在,浪費(fèi)內(nèi)存
  override func removeFromSuperview() {
    
    timer?.invalidate()
    timer = nil
    super.removeFromSuperview()
  }
  // Mark:UICollectionViewDataSource
  // 設(shè)置Itmes
  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    
    return 4;
  }
  // 設(shè)置cell
  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    let cell : UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "ID", for: indexPath)
    for view : UIView in cell.contentView.subviews {
      
      view.removeFromSuperview()
    }
    let imageView = UIImageView.init(frame: cell.contentView.bounds)
    if indexPath.row < 3 {
      
      imageView.image = UIImage.init(named: String(indexPath.row))

    } else {
      imageView.image = UIImage.init(named: String(0))
    }
    cell.contentView.addSubview(imageView)
    
    return cell;
  }
  // Mark:UICollectionViewDelegate
  // 點(diǎn)擊方法
  func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
    print("您點(diǎn)擊了第 \(indexPath.row == 3 ? 0 : indexPath.row) 個");
  }
  
}

UICollectionView 和 Timer的用法和OC基本相同。Swift和OC的UI部分應(yīng)該是一致的,因?yàn)榈讓佣际荗penGL。我直接說一下區(qū)別:

1.Timer:如果重復(fù),OC是等一個間隔再執(zhí)行的,Swift是立即執(zhí)行的,所以我用了GCD延時開啟定時器。

2.Swift 沒有 CGPointZero。

無限輪播的原理就是在最后面多添加一個和第一個相同的itme。當(dāng)你滑動到最后一個itme時,把UICollectionView的contentOffset置零,繼續(xù)向右活動。如果不添加,會給用戶一種卡頓的感覺。

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

相關(guān)文章

最新評論