【问题标题】:UICollectionView Animate cell height (swift)UICollectionView 动画单元格高度(快速)
【发布时间】:2015-04-05 01:48:12
【问题描述】:

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

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

【问题讨论】:

    标签: ios swift animation cell collectionview


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-06
      • 2020-12-27
      • 2015-03-25
      • 1970-01-01
      • 1970-01-01
      • 2020-02-06
      • 1970-01-01
      相关资源
      最近更新 更多