我做了一个水平UICollectionView
,内部UICollectionViewCell
我有scrollview和在scrollview内我有一个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