UICollectionView动画单元格高度(swift)

时间:2015-04-05 01:48:12

标签: ios swift animation cell collectionview

我正在尝试创建一个可以手动调整大小的集合视图。

collectionView中的单元格具有屏幕宽度和集合视图的高度。

我有整个工作,除了单元格在用户更改集合视图高度时不更新约束。它们仅在出现新单元格时更新

我已经尝试了不同的方法直接使这个高度动画,但没有解决方案似乎立即改变单元格高度与集合视图高度。

是否有解决方法使单元格大小具有动画效果?

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
              
    let width = self.CollectionViewController2.frame.width as CGFloat
    let height = self.CollectionViewController2.frame.height as CGFloat     
        
    return CGSizeMake(self.CollectionViewController2.frame.width, self.CollectionViewController2.frame.height)                           
}

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

            let height = self.CollectionViewController2.frame.height

            cell.frame = CGRectMake(cellB.frame.origin.x, 0, cellB.frame.size.width, height)
            cell.updateConstraintsIfNeeded()
            cell.layoutIfNeeded()
            cell.imgs.image = UIImage(named: somePictures[indexPath.row])

            return cell


}

允许滑动手势调整集合视图大小的按钮:

    @IBAction func SwipeDownStatus(sender: AnyObject) {

        if CollectionViewHeight.constant == 390 {
            self.CollectionViewHeight.constant = 130
            self.CommentLikeBarHeight.constant = -50
            CollectionViewController2.updateConstraints()
            UIView.animateWithDuration(0.25, animations: { () -> Void in
                self.view.layoutIfNeeded()
            })
        }

1 个答案:

答案 0 :(得分:0)

首先,更改行高的方式不正确。最好输出将要展开的项目的高度约束(在这种情况下,我猜是UIImageView)并在您想要展开时更改它。它使Autolayout能够正确控制布局。要为它制作动画,你唯一应该做的就是:

heightConstant.constant = 50 // your desired height
UIView.animate(withDuration: 0.25, animations: {
        self.view.layoutIfNeeded()
    }, completion: nil)