将CollectionViewCell大小设置为外部CollectionView边界 - Swift

时间:2015-11-20 08:18:54

标签: ios swift uicollectionview uicollectionviewcell

我一直致力于动画某些collectionViewCells的大小。它在我的collectionViewController中运行良好,但在另一个UIViewController中我与其他对象一起有一个小collectionView,单元格只会增长到collectionView的大小。我希望它能填满整个屏幕。这样做的最佳方式是什么?

这是我目前的代码:

    var largePhotoIndexPath : NSIndexPath? {
    didSet{

        var indexPaths = [NSIndexPath]()
        if largePhotoIndexPath != nil {
            indexPaths.append(largePhotoIndexPath!)
        }
        if oldValue != nil {
            indexPaths.append(oldValue!)
        }
        collectionView?.performBatchUpdates({ () -> Void in
            self.collectionView?.reloadItemsAtIndexPaths(indexPaths)
            return
            }){
                completed in

                if self.largePhotoIndexPath != nil {
                    self.collectionView?.scrollToItemAtIndexPath(self.largePhotoIndexPath!, atScrollPosition: .CenteredVertically, animated: true)
                }
        }
    }
}

func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool {
    if largePhotoIndexPath == indexPath {
        largePhotoIndexPath = nil
    }
    else {
        largePhotoIndexPath = indexPath
    }
    return false
}

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

    let dealImage: UIImage = UIImage(data: self.imageDataArray[indexPath.row])!
    let dealPhoto: UIImageView = UIImageView(image: dealImage)

    if indexPath == largePhotoIndexPath {

        var size = UIScreen.mainScreen().bounds
        dealPhoto.frame == size
        return CGSize(width: 300, height: 525)

    }
    return CGSize(width: 100, height: 175)
}



func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return self.imageDataArray.count

}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! TrailInfoCollectionViewCell

    cell.imageView.image = UIImage(data: self.imageDataArray[indexPath.row])

    return cell
}

我选择单元格之前和之后的一些图像:

Before Selection

After Selection

正如您所看到的,图像已经增长到我想要的大小,但它受到collectionView的约束。 collectionViewCell是否可能超出collectionView的边界,或者我是否必须以其他方式执行此操作?我只想设置一个隐藏的imageView,但我喜欢动画过渡。

感谢您的帮助。如果您需要更多详细信息,请与我们联系。

1 个答案:

答案 0 :(得分:2)

clipsToBounds属性阻止子视图越过它的父级。将其设置为false,它应该工作。