【问题标题】:How to exchange place of UIcollectionView cells with animation ?如何用动画交换 UIcollectionView 单元格的位置?
【发布时间】:2015-11-04 17:02:22
【问题描述】:

我有一个 UIcollection 视图,其中有 2 个不同大小的单元格。最后一个单元格总是较大且暴露在外,其他所有单元格都较小且半隐藏。

现在,当用户点击半隐藏单元格时,我想选择这些单元格并将它们的位置替换为前面(暴露)的单元格并带有动画。

到目前为止,我正在重新洗牌数据源中的数据并重新加载它,这可以达到目的,但没有动画,因为找不到任何有用的东西。

任何帮助将不胜感激。 注意:我使用的是 Swift。

谢谢,

【问题讨论】:

    标签: uicollectionviewcell swift2 xcode7-beta4


    【解决方案1】:

    试试这个:

    import UIKit
    
    private let reuseIdentifierBig = "bigger"
    private let reuseIdentifierSmall = "small"
    
    class CollectionViewController: UICollectionViewController {
    
    let array = [0,1,2,3,4]
    var selectedIndex:NSIndexPath?
    var preSelectedIndex:NSIndexPath?
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }
    
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return array.count
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        if selectedIndex != indexPath {
            let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifierSmall, forIndexPath: indexPath)
            return cell
        } else {
            let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifierBig, forIndexPath: indexPath)
            return cell
        }
    }
    
    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        preSelectedIndex = selectedIndex
        selectedIndex = indexPath
        collectionView.reloadItemsAtIndexPaths([indexPath])
        if preSelectedIndex != nil {
            collectionView.reloadItemsAtIndexPaths([preSelectedIndex!])
        }
    }
    

    }

    【讨论】:

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