Animate uicollectionview cells on selection

后端 未结 2 696
无人及你
无人及你 2021-01-14 17:49

I have created a customlayout and set my cells position attribute in layoutAttributesForItemAtIndexPath like this

attributes.center = CGPointMake         


        
相关标签:
2条回答
  • 2021-01-14 18:13

    Maybe a different approach. Just override isSelected in the collectionViewCell.

     override var isHighlighted: Bool {
        didSet {
            if isHighlighted {
                UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseOut, animations: {
                    // animate highlight
                }, completion: nil)
            } else {
                UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseOut, animations: {
                    // animate unHighligh
                }, completion: nil)
            }
        }
    }
    

    In this post UICollectionView: Animate cell size change on selection you can see an example on how to animate a size change.

    0 讨论(0)
  • 2021-01-14 18:30

    I'm animating the attributes by using didSelectItemAtIndexPath in my UICollectionViewDelegate:

    - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
    {
        [_collectionView.collectionViewLayout invalidateLayout];
        UICollectionViewLayoutAttributes *newAttributes = [_collectionView layoutAttributesForItemAtIndexPath:indexPath];
    
        //use new attributes for animation
    }
    
    0 讨论(0)
提交回复
热议问题