【发布时间】:2016-03-06 17:57:23
【问题描述】:
我正在做一个水平的UICollectionView,在UICollectionViewCell 里面我有滚动视图,在滚动视图里面我有一个imageView。
我遇到的问题是,当我放大 imageView 时,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
【问题讨论】:
-
嘿,下载演示并告诉我它好不好,然后我会更新我的答案dropbox.com/s/i815mu0z4d0d76v/horizntal%203.zip?dl=0
-
一定要检查并告诉我:)
-
@RushangPrajapati ,应启用 ScrollView 滚动属性,在您的情况下禁用它,因此我可以在不滚动的情况下放大以查看图像的边界或其余部分。任何可能的解决方案?
-
表示您要在不禁用 Scoll View Property Disabled 的情况下放大吗?
-
是并处理滚动视图大小以仅获取图像边界而不是整个单元格,例如打开照片应用并放大图像并在其中左右滚动。
标签: ios swift uiscrollview uicollectionview