在UICollectionViewCell中处理滚动视图大小

Nat*_*Mio 17 uiscrollview ios uicollectionview swift

我做了一个水平UICollectionView,内部UICollectionViewCell我有scrollview和在scrollview内我有一个imageView.

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

我的ColectionViewCell代码:

class CollectionViewCell: UICollectionViewCell {
    @IBOutlet var scrollView: UIScrollView!
    @IBOutlet var ImageV: UIImageView!
}
Run Code Online (Sandbox Code Playgroud)

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 
  }
}
Run Code Online (Sandbox Code Playgroud)

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

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

Aao*_*oIi 7

那么首先让我们从UIViewController持有您的UICollectionView

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

var flowLayout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
Run Code Online (Sandbox Code Playgroud)

您将不得不重写viewWillLayoutSubviews,这将处理 collectionView 大小。

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()

    flowLayout.itemSize = CGSize(width: view.frame.width, height:view.frame.height)

}
Run Code Online (Sandbox Code Playgroud)

您还需要覆盖viewDidLayoutSubviews以处理每个新单元格的大小以将其设置为默认大小:

    override func viewDidLayoutSubviews() {

        if let currentCell = imageCollectionView.cellForItemAtIndexPath(desiredIndexPath) as? GalleryCell {
            currentCell.configureForNewImage()
        }
}
Run Code Online (Sandbox Code Playgroud)

ViewDidLoad将 collectionView 设置为水平流布局时:

    // 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)
Run Code Online (Sandbox Code Playgroud)

在您的UICollectionView方法中cellForItemAtIndexPath只加载图像而不设置任何内容:

 cell.image = UIImage(named: array[indexPath.row])
Run Code Online (Sandbox Code Playgroud)

现在让我们转到GalleryCell类并在缩放时处理 scrollView:

class GalleryCell: UICollectionViewCell, UIScrollViewDelegate {

    var image:UIImage? {
        didSet {
            configureForNewImage()
        }
    }

     var scrollView: UIScrollView
     let imageView: UIImageView

   override init(frame: CGRect) {

        imageView = UIImageView()
        imageView.translatesAutoresizingMaskIntoConstraints = false
        scrollView = UIScrollView(frame: frame)
        scrollView.translatesAutoresizingMaskIntoConstraints = false

        super.init(frame: frame)
        contentView.addSubview(scrollView)
        contentView.addConstraints(scrollViewConstraints)

        scrollView.addSubview(imageView)

        scrollView.delegate = self
        scrollView.maximumZoomScale = 4
    }

    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    internal func configureForNewImage() {
        imageView.image = image
        imageView.sizeToFit()

        setZoomScale()
        scrollViewDidZoom(scrollView)
    }

    public func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return imageView
    }

    public func scrollViewDidZoom(scrollView: UIScrollView) {

        let imageViewSize = imageView.frame.size
        let scrollViewSize = scrollView.bounds.size

        let verticalPadding = imageViewSize.height < scrollViewSize.height ? (scrollViewSize.height - imageViewSize.height) / 2 : 0
        let horizontalPadding = imageViewSize.width < scrollViewSize.width ? (scrollViewSize.width - imageViewSize.width) / 2 : 0

        if verticalPadding >= 0 {
            // Center the image on screen
            scrollView.contentInset = UIEdgeInsets(top: verticalPadding, left: horizontalPadding, bottom: verticalPadding, right: horizontalPadding)
        } else {
            // Limit the image panning to the screen bounds
            scrollView.contentSize = imageViewSize
        }


}
Run Code Online (Sandbox Code Playgroud)

我已经用示例对其进行了测试,它应该可以通过 scrollview 解决您的问题,@Spencevail 主要解释了导致它的原因!