【问题标题】:Animate uicollectionview cells on selection在选择时为 uicollectionview 单元格设置动画
【发布时间】:2012-10-14 18:06:03
【问题描述】:

我已经创建了一个自定义布局,并像这样在layoutAttributesForItemAtIndexPath 中设置了我的单元格位置属性

attributes.center = CGPointMake((size.width/2 - 100) + 100, (size.height/2 - 150) +100);
 

我想在单元格被选中时对其进行动画处理。复制我们使用 initialLayoutAttributesForAppearingItemAtIndexPathfinalLayoutAttributesForDisappearingItemAtIndexPath 获得的那种动画。

我想在选择和取消选择单元格时执行此操作。

例如:

单元格 A 位于 0,0 位置。单元格 B 位于50,100 位置。如果我选择单元格 B,我想将其设置为 0,0。同时将单元格 A 设置为 50,100。基本上是切换位置,但动画。

【问题讨论】:

    标签: ios ios6 uicollectionview uicollectionviewcell


    【解决方案1】:

    也许是一种不同的方法。只需覆盖 collectionViewCell 中的 isSelected。

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

    在这篇UICollectionView: Animate cell size change on selection 的帖子中,您可以看到一个关于如何为大小更改设置动画的示例。

    【讨论】:

      【解决方案2】:

      我在 UICollectionViewDelegate 中使用 didSelectItemAtIndexPath 为属性设置动画:

      - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
      {
          [_collectionView.collectionViewLayout invalidateLayout];
          UICollectionViewLayoutAttributes *newAttributes = [_collectionView layoutAttributesForItemAtIndexPath:indexPath];
      
          //use new attributes for animation
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-22
        • 1970-01-01
        • 2021-07-04
        • 1970-01-01
        • 1970-01-01
        • 2013-07-17
        • 1970-01-01
        • 2012-11-26
        相关资源
        最近更新 更多