删除项目后对CollectionViewCell进行动画处理

时间:2020-03-31 13:22:18

标签: ios swift uicollectionview delegates uicollectionviewcell

对于我的CollectionView,我在animation里面有这个willDisplay

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {

        // Add animations here
        let animation = AnimationFactory.makeMoveUpWithFade(rowHeight: cell.frame.height, duration: 0.5, delayFactor: 0.1)
        let animator = Animator(animation: animation)
        animator.animate(cell: cell, at: indexPath, in: collectionView)

}

This是动画的工作方式(我为CollectionView实现了动画),如果您需要更多信息,请访问它。

问题:

在我的项目中,用户可以createdeleteitem

目前,collectionView不在deleting之后进行动画处理,即使我正在呼叫reloadData

extension MainViewController: DismissWishlistDelegate {

func dismissWishlistVC(dataArray: [Wishlist], dropDownArray: [DropDownOption]) {
    self.dataSourceArray = dataArray
    self.dropOptions = dropDownArray
    self.makeWishView.dropDownButton.dropView.tableView.reloadData()

    // reload the collection view
    theCollectionView.reloadData()
    theCollectionView.performBatchUpdates(nil, completion: nil)

}
}

这是我在其他delegate中调用ViewController的地方:

func deleteTapped(){

    let alertcontroller = UIAlertController(title: "Wishlist löschen", message: "Sicher, dass du diese Wishlist löschen möchtest?", preferredStyle: .alert)

    let deleteAction = UIAlertAction(title: "Löschen", style: .default) { (alert) in

        DataHandler.deleteWishlist(self.wishList.index)

        self.dataSourceArray.remove(at: self.currentWishListIDX)
        self.dropOptions.remove(at: self.currentWishListIDX)

        // change heroID so wishlist image doesnt animate
        self.wishlistImage.heroID = "delete"

        self.dismiss(animated: true, completion: nil)

        //  update datasource array in MainVC
        self.dismissWishlistDelegate?.dismissWishlistVC(dataArray: self.dataSourceArray, dropDownArray: self.dropOptions)

    }

    let cancelAction = UIAlertAction(title: "Abbrechen", style: .default) { (alert) in
        print("abbrechen")
    }


    alertcontroller.addAction(cancelAction)
    alertcontroller.addAction(deleteAction)

    self.present(alertcontroller, animated: true)
}

creating时,动画效果很好。这就是我的createDelegateFunction的样子:

func createListTappedDelegate(listImage: UIImage, listImageIndex: Int, listName: String) {
    // append created list to data source array
    var textColor = UIColor.white
    if Constants.Wishlist.darkTextColorIndexes.contains(listImageIndex) {
        textColor = UIColor.darkGray
    }

    let newIndex = self.dataSourceArray.last!.index + 1

    self.dataSourceArray.append(Wishlist(name: listName, image: listImage, wishData: [Wish](), color: Constants.Wishlist.customColors[listImageIndex], textColor: textColor, index: newIndex))

    // append created list to drop down options
    self.dropOptions.append(DropDownOption(name: listName, image: listImage))

    // reload the collection view
    theCollectionView.reloadData()
    theCollectionView.performBatchUpdates(nil, completion: {
        (result) in
        // scroll to make newly added row visible (if needed)
        let i = self.theCollectionView.numberOfItems(inSection: 0) - 1
        let idx = IndexPath(item: i, section: 0)
        self.theCollectionView.scrollToItem(at: idx, at: .bottom, animated: true)

    })
}

2 个答案:

答案 0 :(得分:0)

使用finalLayoutAttributesForDisappearingItem(at:)initialLayoutAttributesForAppearingItem(at:)正确完成IUCollectionView元素插入和删除的动画

摘自finalLayoutAttributesForDisappearingItem(at:)上Apple文档的摘录

对于要删除的任何项目,在prepare(forCollectionViewUpdates :)方法之后和finalizeCollectionViewUpdates()方法之前调用此方法。您的实现应返回描述项目最终位置和状态的布局信息。集合视图使用此信息作为所有动画的终点。 (动画的起点是项目的当前位置。)如果返回nil,则布局对象对动画的起点和终点使用相同的属性。

答案 1 :(得分:0)

我通过删除performBatchUpdates来解决了这个问题……我不知道为什么它现在可以正常工作,performBatchUpdates可以做什么,但是它可以工作,所以如果有人想向我解释一下,请随意: D

最终功能如下:

func dismissWishlistVC(dataArray: [Wishlist], dropDownArray: [DropDownOption], shouldDeleteWithAnimation: Bool, indexToDelete: Int) {

    if shouldDeleteWithAnimation {

        self.shouldAnimateCells = true
        self.dataSourceArray.remove(at: self.currentWishListIDX)
        self.dropOptions.remove(at: self.currentWishListIDX)

        // reload the collection view
        theCollectionView.reloadData()


    } else {

        self.shouldAnimateCells = false
        self.dataSourceArray = dataArray
        self.dropOptions = dropDownArray

        // reload the collection view
        theCollectionView.reloadData()
        theCollectionView.performBatchUpdates(nil, completion: nil)
    }

    self.makeWishView.dropDownButton.dropView.tableView.reloadData()

}