簡體   English   中英

Swift-CollectionView添加具有自定義動畫的單個單元格

[英]Swift - CollectionView Add Single Cell With Custom Animation

我有一個內部帶有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])
    })
}

可行,但是我想自定義默認動畫,以便在添加單元格時可以執行縮放和淡入。 IE:

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

您將動畫放在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:在:)它定義UICollectionViewLayout ,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()
  }
}

請嘗試嘗試此答案,並告訴我它是否對您有用。


不要忘記調用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)
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM