在UICollectionViewCell中处理滚动视图大小

时间:2015-12-02 13:12:55

标签: ios swift uiscrollview uicollectionview

我正在制作水平UICollectionView,而在UICollectionViewCell内我有滚动视图,在滚动视图中我有一个imageView。

我遇到的问题是,当我放大imageView时,scrollView会占用所有单元格大小,因此它不适合图像大小的高度和宽度。通过向上和向下滚动图像从scrollview中消失,我不知道我的代码中出了什么问题。

我的ColectionViewCell代码:

class CollectionViewCell: UICollectionViewCell {
    @IBOutlet var scrollView: UIScrollView!
    @IBOutlet var ImageV: UIImageView!
}

CollectionView代码:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
  let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CollectionViewCell

  cell.scrollView.contentMode = UIViewContentMode.ScaleAspectFit
  cell.scrollView.delegate = self
  cell.ImageV.image = UIImage(named: array[indexPath.row])
  cell.ImageV.contentMode = UIViewContentMode.ScaleAspectFit

  cell.scrollView.minimumZoomScale = 1
  cell.scrollView.maximumZoomScale = 4;
  cell.scrollView.contentSize = cell.ImageV.frame.size

  return cell
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

  return CGSize(width: self.collectionView.frame.size.width , height: self.collectionView.frame.size.height - 100)

}

func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {

  let indexPath = NSIndexPath(forItem: Int(currentIndex), inSection: 0)

  if let cell1 = self.collectionView.cellForItemAtIndexPath(indexPath) {  
    let cell = cell1 as! CollectionViewCell
    let boundsSize = cell.scrollView.bounds.size
    var contentsFrame = cell.ImageV.frame

    if contentsFrame.size.width < boundsSize.width{
      contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2
    }else{
      contentsFrame.origin.x = 0
    }

    if contentsFrame.size.height < boundsSize.height {
      contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2
    }else{
      contentsFrame.origin.y = 0
    }
    return cell.ImageV    
  }      
   return UIView()     
 }


func scrollViewDidEndDecelerating(scrollView: UIScrollView) {

  currentIndex = self.collectionView.contentOffset.x / self.collectionView.frame.size.width;
  oldcell = currentIndex - 1

  let indexPath = NSIndexPath(forItem: Int(oldcell), inSection: 0)
  if let cell1 = self.collectionView.cellForItemAtIndexPath(indexPath) {
    let cell = cell1 as! CollectionViewCell
    cell.scrollView.zoomScale = 0 
  }
}

图片预览: https://i.imgur.com/Gr2p09A.gifv

我的项目在此处找到: https://drive.google.com/file/d/0B32ROW7V8Fj4RVZfVGliXzJseGM/view?usp=sharing

2 个答案:

答案 0 :(得分:4)

首先,让我们从持有UIViewController的{​​{1}}开始:

定义一个变量来保存集合视图布局:

UICollectionView

您必须覆盖var flowLayout:UICollectionViewFlowLayout = UICollectionViewFlowLayout() ,这将处理collectionView大小。

viewWillLayoutSubviews

此外,您还需要覆盖override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() flowLayout.itemSize = CGSize(width: view.frame.width, height:view.frame.height) } 来处理每个新单元格的大小,以将其设置为默认大小:

viewDidLayoutSubviews
override func viewDidLayoutSubviews() { if let currentCell = imageCollectionView.cellForItemAtIndexPath(desiredIndexPath) as? GalleryCell { currentCell.configureForNewImage() } }

将collectionView设置为与流布局水平:

ViewDidLoad

// Set up flow layout flowLayout.scrollDirection = UICollectionViewScrollDirection.Horizontal flowLayout.minimumInteritemSpacing = 0 flowLayout.minimumLineSpacing = 0 // Set up collection view imageCollectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height), collectionViewLayout: flowLayout) imageCollectionView.translatesAutoresizingMaskIntoConstraints = false imageCollectionView.registerClass(GalleryCell.self, forCellWithReuseIdentifier: "GalleryCell") imageCollectionView.dataSource = self imageCollectionView.delegate = self imageCollectionView.pagingEnabled = true view.addSubview(imageCollectionView) imageCollectionView.contentSize = CGSize(width: 1000.0, height: 1.0) 方法中UICollectionView只加载图片而不设置任何内容:

cellForItemAtIndexPath

现在让我们转到 cell.image = UIImage(named: array[indexPath.row]) 类并在缩放时处理scrollView:

GalleryCell

我已经通过示例对其进行了测试,它应该通过scrollview来解决您的问题,并且@Spencevail主要解释了它为什么会被引发!

答案 1 :(得分:3)

您需要在滚动视图上设置contentInset。发生的事情contentSize的{​​{1}}最初设置为与屏幕尺寸相匹配。当您放大滚动视图时,contentSize会按比例扩展,因此当您放大时,在缩放时,照片上方和下方的黑色区域会在放大时展开。换句话说,您正在扩展区域的上方和下方区域你的形象可以去。如果您调整UIScrollView,它将带来该死区,并且不允许滚动视图将图像移出窗口。

contentInset

这与我帮助管理的开源Cocoapod SwiftPhotoGallery几乎相同。看看SwiftPhotoGalleryCell的代码,你应该很清楚如何做到这一点。 (如果你愿意,也可以随意使用cocoapod!)