Swift - CollectionView使用自定义动画添加单个单元格

时间:2017-12-06 10:23:34

标签: ios swift

我有一个带有CollectionView的UIViewController,还有一个按钮,它只是在CollectionView的末尾添加一个新单元格。 我使用它来添加它:

func addNewFolderCell {
    self.folder!.childFolders!.append(folder)
    let count = self.folder!.childFolders!.count
    let index = count > 0 ? count - 1 : count
    let indexPath = IndexPath(item: index, section: 0)
    collectionView?.performBatchUpdates({
        collectionView!.insertItems(at: [indexPath])
    })
}

这样可行,但我想自定义默认动画,这样我就可以在添加单元格时执行缩放和淡入淡出。即:

UIView.animate(withDuration: 1.4, delay: 0.15 * Double(indexPath.row),  animations: {
    cell.alpha = 1.0
    cell.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
})

我找到了这个教程,但是当你在UIViewController而不是UICollectionViewController中时,我无法弄清楚如何让它工作:

https://littlebitesofcocoa.com/306-customizing-collection-view-cell-insertion-animations

2 个答案:

答案 0 :(得分:1)

您将动画放入collectionView:willDisplayCell:forItemAtIndexPath:

extension ViewController: UICollectionViewDelegate {
  func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
    cell.alpha = 0
    cell.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
    UIView.animate(withDuration: 0.25) {
      cell.alpha = 1
      cell.transform = .identity
    }
  }
}

编辑更新:

假设您正在使用UICollectionViewFlowLayout,则需要对其进行子类化并覆盖initialLayoutAttributesForAppearingSupplementaryElement(ofKind:at:),该UICollectionViewLayout是在{{3}}(UICollectionViewFlowLayout的抽象父级)上定义的。如果从此方法返回属性,它们将用作单元格的初始属性,动画系统会将它们插入到最终值。然后,将自定义UICollectionViewFlowLayout的实例设置为集合视图.collectionViewLayout

编辑添加代码:

class MyFlowLayout: UICollectionViewFlowLayout {
  override func initialLayoutAttributesForAppearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
    let attributes = UICollectionViewLayoutAttributes()
    attributes.alpha = 0
    attributes.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
    return attributes
  }
}

class ViewController: UIViewController {
  var collectionView: UICollectionView!
  override func viewDidLoad() {
    collectionView.collectionViewLayout = MyFlowLayout()
  }
}

答案 1 :(得分:0)

试试这个答案并告诉我它是否适合你。


不要忘记致电UICollectionViewDelegateFlowLayout

var insertingIndexPaths = [IndexPath]()



 func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
    let attributes = "YourCollectionview".layoutAttributesForItem(at: indexPath)


    if insertingIndexPaths.contains(indexPath) {
        attributes?.alpha = 1.0
        attributes?.transform = CGAffineTransform(
            scaleX: 0.1,
            y: 0.1
        )
    }
    return attributes
}

//这是您点击按钮以插入新项目

@IBAction func btn_click_sign_in(_ sender: Any) {
        //here you have insert one item into your array
        // for Exa. My array count is 3 here.
        // So I am adding new object array.append(element)


        let indexPath = IndexPath(
            item: **yourArray count** - 1,
            section: 0
        )
        "YourCollectionview"?.performBatchUpdates({
            self.coll_pagview?.insertItems(at: [indexPath])
        }, completion: nil)
    }