Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift-CollectionView使用自定义动画添加单个单元格_Ios_Swift - Fatal编程技术网

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

Ios Swift-CollectionView使用自定义动画添加单个单元格,ios,swift,Ios,Swift,我有一个UIViewController,里面有一个CollectionView,还有一个按钮,只需在CollectionView的末尾添加一个新单元格。 我使用以下方法添加它: func addNewFolderCell { self.folder!.childFolders!.append(folder) let count = self.folder!.childFolders!.count let index = count > 0 ? count - 1

我有一个UIViewController,里面有一个CollectionView,还有一个按钮,只需在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时,我不知道如何使其工作:


将动画放入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,则需要将其子类化,并覆盖在UICollectionViewFlowLayout的抽象父级上定义的子类。如果使用此方法返回属性,则这些属性将用作单元的初始属性,动画系统会将它们插值为最终值。然后将自定义UICollectionViewFlowLayout的实例设置为集合视图的
。collectionViewLayout

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
}
编辑添加代码:

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

将动画放置在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,则需要将其子类化,并覆盖在UICollectionViewFlowLayout的抽象父级上定义的子类。如果使用此方法返回属性,则这些属性将用作单元的初始属性,动画系统会将它们插值为最终值。然后将自定义UICollectionViewFlowLayout的实例设置为集合视图的
。collectionViewLayout

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
}
编辑添加代码:

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

试试这个答案,告诉我它是否对你有用


别忘了调用
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)
    }


我尝试过这个,但在使用insertItems时它不起作用。它只适用于重新加载的数据。它运行该功能,但动画没有动画,它只是直接进入结尾。我同意这是解决方案(这就是我链接的文章所做的),但我真的不明白如何在UIViewController而不是UICollectionViewController上真正做到这一点。我只是遇到了一百万个错误,无法解决它。如果我能看到一个例子,我想我能理解它。对我来说不起作用,只是像正常情况一样淡入,不可缩放。如果你看到的是淡出/淡入动画,你不应该看到,在UICollectionView的performBatchUpdates函数中包装布局的设置或更改。我尝试了这个,但当使用插入项时,它不起作用。它只适用于重新加载的数据。它运行该功能,但动画没有动画,它只是直接进入结尾。我同意这是解决方案(这就是我链接的文章所做的),但我真的不明白如何在UIViewController而不是UICollectionViewController上真正做到这一点。我只是遇到了一百万个错误,无法解决它。如果我能看到一个例子,我想我能理解它。对我来说不起作用,只是像正常一样淡入,不可缩放。如果你看到的是淡出/淡入动画,你不应该看到,将布局的设置或更改包装到UICollectionView的performBatchUpdates函数中。使用此方法打开func layoutAttributesForItem(在indexPath:indexPath)->UICollectionViewLayoutAttributes?,我认为这将取代UICollectionviewController对ViewController的默认方法。你还必须导入UICollectionViewDelegateFlowLayout你在collectionView上尝试过动画吗?.performBatchUpdates行?@JitendraModi我想你是对的,我只是不知道怎么做。介意为它创造一个答案吗?我已经导入了UICollectionViewDelegateFlowLayout。@ErickMaynard现在您可以检查我的答案了。我已经检查了我的演示。使用此方法打开func layoutAttributesForItem(在indexPath:indexPath)->UICollectionViewLayoutAttributes?,我认为这将取代UICollectionviewController对ViewController的默认方法。你还必须导入UICollectionViewDelegateFlowLayout你在collectionView上尝试过动画吗?.performBatchUpdates行?@JitendraModi我想你是对的,我只是不知道怎么做。介意为它创造一个答案吗?我已经导入了UICollectionViewDelegateFlowLayout。@ErickMaynard现在您可以检查我的答案了。我已签入演示。我收到错误:“UIViewController”类型的值没有成员“layoutAttributesForItem”是否调用了UICollectionViewDelegateFlowLayout?是:类TierViewController:UIViewController、UICollectionViewDelegate、UICollectionViewDataSource、UICollectionViewDelegateFlowLayout{等等,我会提供一个解决方案。我正在检查我的演示。很抱歉,这个解决方案出现了太多错误,我想这是您在UICollectionViewController上使用的代码,而不是在包含CollectionView的UIViewController上。这是我的主要问题:我不明白如何在这里进行转换,我只是在e之后出现错误当我自己尝试对其进行故障排除时出错。我得到错误:“UIViewController”类型的值没有成员“layoutAttributesForItem”是否调用了UICollectionViewDelegateFlowLayout?是:类TierViewController:UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{等等,我会提供一个解决方案。我正在检查我的演示。很抱歉,这个解决方案出现了太多错误,我想这是您在UICollectionViewController上使用的代码,而不是在包含CollectionView的UIViewController上。这是我的主要问题:我不明白如何在这里进行转换,我只是在e之后出现错误当我试着去旅行的时候