Swift 2-performBatchUpdates-在UICollectionView框架内可见时,对单元格进行逐一动画处理 [英] Swift 2 - performBatchUpdates - Animate cell one by one when visibile inside the UICollectionView frame
问题描述
我正在尝试为框架中的 UICollectionView
可见的每个单元设置动画。
每次滚动新的单元格时都会显示动画。
I am trying to animate each cell of my UICollectionView
visibile in the frame.
Every time I scroll a new cell appears with the animation.
我正在使用 performBatchUpdates
在 cellForItemAtIndexPath
内部,动画将同时应用于所有单元,而且速度非常快。似乎无法识别1秒的动画。
I am doing this using performBatchUpdates
inside cellForItemAtIndexPath
however, the animation is applied to all cells at the same time and its very fast. It seems that the animation of 1 second its not recognised.
我还试图找到一种方法,当不成功按下按钮时将动画应用于单元格
Also I am trying to find the way to apply an animation to a cell when a button is pressed with no success.
我使用的代码是:
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:)
时,您可以使其工作。每次将要显示新单元格时都会调用该方法。
You can make this work when you move the animation to willDisplayCell(_:cell:indexPath:)
. That method is called every time a new cell is about to be displayed.
您不能将 UIView.animateWithDuration
用于图层属性。为此,您必须使用 CABasicAnimation
。
You cannot use UIView.animateWithDuration
for the layer properties. You have to use CABasicAnimation
for that.
如果您想在用户按下按钮时对单元进行动画处理,可以从下面的代码示例中调用 animateCellAtIndexPath
。您必须知道单元格的indexPath才能这样做。在此示例中,当用户选择单元格时,我将调用此方法。
If you want to animate the cell when the user presses a button you can call animateCellAtIndexPath
from the code example below. You have to know the cell's indexPath to do so. In this example I call this method when the user selects the cell.
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)
}
这篇关于Swift 2-performBatchUpdates-在UICollectionView框架内可见时,对单元格进行逐一动画处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!