ios - UICollectionView 动画单元格高度(快速)

标签 ios swift animation cell collectionview

我正在尝试创建一个可以手动调整大小的 Collection View 。

collectionView 中的单元格具有屏幕的宽度和 Collection View 的高度。

除了当用户更改 Collection View 高度时单元格不会更新约束之外,我已经完成了整个工作。它们仅在出现新单元格时更新

我已经尝试了不同的方法来直接使这个高度动画化,但似乎没有解决方案可以立即改变单元格高度和 Collection View 高度。

是否有变通方法可以使单元格大小具有动画效果?

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


}

允许滑动手势调整 Collection View 大小的按钮:

    @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()
            })
        }

最佳答案

首先,您更改行高的方式不正确。最好将要展开的项目(在本例中我猜是 UIImageView)的高度限制设置为 outlet,并在您想要展开时更改它。它使 Autolayout 能够正确地控制布局。要为其设置动画,您唯一应该做的就是:

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

关于ios - UICollectionView 动画单元格高度(快速),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29453301/

相关文章:

animation - 在 Highmap 中呈现光环动画

ios - 如何从相机图像中获取深度数据

iphone - 在 requestFinished 方法中调用方法时崩溃?

ios - 我可以从后台线程调用或运行 Core Data Main Context (viewContext) 吗?

swift - 如何从 NSURLSession.sharedSession().dataTaskWithRequest 获取数据

css - 有什么方法可以在 CSS 中可视化变换原点?

iphone - iOS进程如何获得它们的名字?

swift - 我得到无法使用字符串类型的参数调用计数

ios - 导入 Swift 图表

jquery - CSS3相当于jQuery slideUp和slideDown?