gpt4 book ai didi

ios - Swift 2 - performBatchUpdates - 当在 UICollectionView 框架内可见时,一个一个地动画单元格

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:59:17 26 4
gpt4 key购买 nike

我正在尝试为框架中可见的 UICollectionView 的每个单元格制作动画。每次我滚动时,都会出现一个新的单元格和动画。

我在 cellForItemAtIndexPath 中使用 performBatchUpdates 执行此操作,但是,动画会同时应用于所有单元格,而且速度非常快。好像没识别到1秒的动画。

我还试图找到在按下按钮但没有成功时将动画应用于单元格的方法。

我使用的代码是:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let Cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CellClass

self.collectionView?.performBatchUpdates({
Cell.layer.cornerRadius = 200
return
}){
completed in

UIView.animateWithDuration(1, animations: {
Cell.layer.cornerRadius = 0
})
}

Cell.playAnimationBtn.layer.setValue(indexPath.row, forKey: "indexPlayBtn")
}

@IBAction func actionGetAnimation(sender: UIButton)
{
let indexUser = (sender.layer.valueForKey("indexPlayBtn")) as! Int

//Cell selected do animation corners = 200
}

最佳答案

当您将动画移动到 willDisplayCell(_:cell:indexPath:) 时,您可以使它工作。每次要显示新单元格时都会调用该方法。

您不能对图层属性使用 UIView.animateWithDuration。你必须为此使用 CABasicAnimation

如果您想在用户按下按钮时为单元格设置动画,您可以从下面的代码示例中调用 animateCellAtIndexPath。您必须知道单元格的 indexPath 才能这样做。在此示例中,我在用户选择单元格时调用此方法。

func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
animateCell(cell)
}

func animateCell(cell: UICollectionViewCell) {
let animation = CABasicAnimation(keyPath: "cornerRadius")
animation.fromValue = 200
cell.layer.cornerRadius = 0
animation.toValue = 0
animation.duration = 1
cell.layer.addAnimation(animation, forKey: animation.keyPath)
}

func animateCellAtIndexPath(indexPath: NSIndexPath) {
guard let cell = collectionView.cellForItemAtIndexPath(indexPath) else { return }
animateCell(cell)
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
animateCellAtIndexPath(indexPath)
}

关于ios - Swift 2 - performBatchUpdates - 当在 UICollectionView 框架内可见时,一个一个地动画单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33522296/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com