swift UICollectionView 的使用:实现相册功

2016-09-05  本文已影响0人  Sparkle_S

这里的相册功能是基于 UIcollection 实现的,相比于基于 UIScrollview 来实现,这种实现方式会节约更多资源,也会避免出现由于内存剧增导致的闪退问题.效果如图所示:

相册功能
首先,你要有一个自定制的横向滚动按钮视图控件.传送门: UITableView 实现横向滚动按钮视图

其次,你需要定制一个盛放 collectionView 的 collectionViewCell.

class CollCollectionViewCell: UICollectionViewCell,UICollectionViewDelegate,UICollectionViewDataSource {
  var clickBlock:((selNum:Int)->())?  //回调
  var imageUrlArr:[String] = []{
    didSet{
      collectionView.reloadData()
    }
  }
  var collectionView:UICollectionView!
  
  override init(frame: CGRect) {
    super.init(frame: frame)
    self.setUp()
  }
  override func awakeFromNib() {
    super.awakeFromNib()
    self.setUp()
  }
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
  func setUp() {
    collectionView = self.createCollectionView(0, itemSize:CGSizeMake((SCREEN_WIDTH-40)/3.0, (SCREEN_WIDTH-40)/3.0), sectionInset: UIEdgeInsetsMake(10, 10, 10, 10), direction: .Vertical)
    collectionView.delegate = self
    collectionView.dataSource = self
    self.addSubview(collectionView)
    //注册 cell
    collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "CollectionCell")
  }
  // MARK: - UICollectionViewDelegate
  func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return imageUrlArr.count
  }
  func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionCell", forIndexPath: indexPath)
    cell.backgroundColor = UIColor.init(white: 0.8, alpha: 1)
    let cellSide = cell.frame.size.width
    //压缩并裁剪图片
    let newImage = UIImage.compressAndCutImage( UIImage.init(named: "tips"), size: CGSizeMake(cellSide, cellSide))
    if let newImage = newImage {
      let imageV = UIImageView.init(image: newImage)
      cell.addSubview(imageV)
    }
    return cell
  }
  func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    if let clickBlock = clickBlock {
      clickBlock(selNum: indexPath.row)
    }
  }
}

同时,你需要一个实现图片浏览功能的控件.其实也就是 cell的点击事件,传送门: 图片浏览视图

从而,你便可以在以上三条的基础上基于 UICollection View 实现相册功能,详见代码:

class PhotoGalleryView: UIView ,UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate{
  
  var allDataArray:[[String]] = []
  var clickCellBlock:((selNum:Int,urlArr:[String])->())?
  
  var topScrollView:ScrollBtnsView!
  var bottomScrollView:UICollectionView!
  var curPage:Int = 0
  
  // MARK: - system method
  override init(frame: CGRect) {
    super.init(frame: frame)
    self.setUp()
  }
  override func awakeFromNib() {
    super.awakeFromNib()
    self.setUp()
  }
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
  // MARK: private method
  func setUp() {
    //添加相册顶部滚动图
    topScrollView = ScrollBtnsView.init(frame: CGRectMake(0, 0, SCREEN_WIDTH, 50))
    self.addSubview(topScrollView)
    bottomScrollView = self.createCollectionView(50, itemSize:CGSizeMake(SCREEN_WIDTH, self.frame.height-50), sectionInset: UIEdgeInsetsZero , direction: .Horizontal)
    bottomScrollView.delegate = self
    bottomScrollView.dataSource = self
    bottomScrollView.pagingEnabled = true
    self.addSubview(bottomScrollView)
    //注册 cell
    bottomScrollView.registerClass(CollCollectionViewCell.self, forCellWithReuseIdentifier: "CollCollectionViewCell")
  }
  // MARK: public method
  func conPhotoGalleryView(titleArr:[String],imageUrlsArr:[[String]]) {
    topScrollView.configBtnScrollView(titleArr) { (tag) in
      self.bottomScrollView.contentOffset = CGPointMake(CGFloat(tag)*SCREEN_WIDTH, 0)
    }
    allDataArray = imageUrlsArr
  }
  // MARK: - UICollectionViewDelegate
  func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return allDataArray.count
  }
  func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell:CollCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("CollCollectionViewCell", forIndexPath: indexPath) as! CollCollectionViewCell
    let images = allDataArray[indexPath.row]
    cell.imageUrlArr = images
    cell.clickBlock = { (selNum:Int) in
      if let  clickCellBlock = self.clickCellBlock{
        clickCellBlock(selNum:selNum,urlArr: images)
      }
    }
    return cell
  }
  // MARK: - UIScrollViewDelegate
  func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
    let offSetFlag = Int(scrollView.contentOffset.x/SCREEN_WIDTH)
    topScrollView.upDataOldAndNewBtn(offSetFlag)
  }
}

当你完成了相册这个控件之后,使用起来便是极其简单的.

    //创建图片浏览视图
    let images = BigImageView.init(frame: CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT))
    UIApplication.sharedApplication().keyWindow?.addSubview(images)
    images.downloadSucBlock = {
      print("保存图片成功")
    }
    images.downloadFailBlock = {
      print("保存图片失败")
    }
    
    //创建相册
    let photoGalleryView = PhotoGalleryView.init(frame: CGRectMake(0, 64, SCREEN_WIDTH, self.view.frame.size.height))
    self.view.addSubview(photoGalleryView)
    photoGalleryView.clickCellBlock = {(selNum:Int,urlArr:[String]) in
      images.showBigImageView(urlArr, selCount: selNum)
    }
    photoGalleryView.conPhotoGalleryView(["相册1相册1相册1","2","相册三","相册4相册4"], imageUrlsArr: [["","","","","","","",""],["","","","",""],["","","","","","","","","","","","","","",""],["",""]])

期待你的评论建议O(∩_∩)O~

上一篇下一篇

猜你喜欢

热点阅读