UICollectionViewCell - contents do not animate alongside cell's contentView

前端 未结 3 577
予麋鹿
予麋鹿 2021-01-31 20:32

Problem looks like this: http://i.imgur.com/5iaAiGQ.mp4 (red is a color of cell.contentView)

Here is the code: https://github.com/nezhyborets/UICollectionViewContentsAni

3条回答
  •  长情又很酷
    2021-01-31 21:14

    You can apply a transform to a cell, although it has some drawbacks, such as handling orientation changes.

    For extra impact, I have added a color change and a spring effect in the mix, neither of which could be achieved using the reloading route:

        func collectionView(_ collectionView: UICollectionView,
                            didSelectItemAt indexPath: IndexPath) {
            UIView.animate(
                withDuration: 0.4,
                delay: 0,
                usingSpringWithDamping: 0.4,
                initialSpringVelocity: 0,
                options: UIViewAnimationOptions.beginFromCurrentState,
                animations: {
                    if( self.selectedIndexPath.row != NSNotFound) {
                        if let c0 =
                            collectionView.cellForItem(at: self.selectedIndexPath)
                        {
                            c0.contentView.layer.transform = CATransform3DIdentity
                            c0.contentView.backgroundColor = UIColor.lightGray
                        }
                    }
                    self.selectedIndexPath = indexPath
                    if let c1 = collectionView.cellForItem(at: indexPath)
                    {
                        c1.contentView.layer.transform =
                            CATransform3DMakeScale(1.25, 1.25, 1)
                        c1.contentView.backgroundColor = UIColor.red
                    }
    
                },
                completion: nil)
        }
    

提交回复
热议问题