我有包含项目的集合视图,每个项目的尺寸(65 * 65)都有水平流动,并且有按钮。

因此,当我单击按钮时,所有项目都必须堆叠在第一个项目上,然后我需要垂直进行集合布局(如表格视图)。

如何制作(带有动画)?

func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        if self.isClose {
            return CGSizeMake(50, 50)
        }else{
            if self.isOpen {
                return CGSizeMake(320, 50)
            }else {
                return CGSizeMake(50, 50)
            }
        }

    }


Screenshot

最佳答案

在Swift 3和4中

func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
    UIView.animate(withDuration: 0.5) {
        if let cell = collectionView.cellForItem(at: indexPath) as? SSProductCell {
            cell.imageView.transform = .init(scaleX: 0.95, y: 0.95)
            cell.contentView.backgroundColor = UIColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 1)
        }
    }
}

func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath) {
    UIView.animate(withDuration: 0.5) {
        if let cell = collectionView.cellForItem(at: indexPath) as? SSProductCell {
            cell.imageView.transform = .identity
            cell.contentView.backgroundColor = .clear
        }
    }
}

关于ios - Collectionview动画,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37522960/

10-09 08:12
查看更多