我想动画的集合视图单元格,动画应该是这样的:
第一个单元出现,在一些延迟之后,第二个单元将与一些动画一起出现,对于所有的单元格都是这样。
如何做到这一点?
发布于 2016-07-18 11:31:56
解决方案是增量地创建数据源,以便在设置时间延迟后添加一个单元格。一旦添加,插入
拨打一个电话,设置一个计时器,以添加到您的dataSource中,无论您喜欢什么延迟。
[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(addToDataSource:) userInfo:nil repeats:YES];
然后,在这个方法中,每隔0.05秒(在本例中)
-(void)addToDataSource:(NSTimer*)timer{
[self.yourMutableDataSourceArray addObject:OBJECT]
NSInteger arrayCount = self.yourMutableDataSourceArray.count;
[self.collectionView performBatchUpdates:^{
[self.collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:arrayCount-1 inSection:0]]];
} completion:^(BOOL finished) {
}];
//Once you have reached the desired count cells you wish, remember to invalidate the timer
}
performBatchUpdate
将意味着collectionView将重新加载动画。
我希望这能帮到你
这是在objective中,但是如果你用快速的方式写这篇文章,同样的原则也适用。
发布于 2016-07-18 17:26:26
这里的另一个解决方案是使用willDisplayCell:forIndexPath
委托。
这是我怎么做的一个例子。
func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
if (!loadedIdx.contains(indexPath.row)) {
let cellContent = cell
let rotationAngleDegrees : Double = -30
let rotationAngleRadians = rotationAngleDegrees * (M_PI/180)
let offsetPositioning = CGPoint(x: collectionView.bounds.size.width, y: -20)
var transform = CATransform3DIdentity
transform = CATransform3DRotate(transform, CGFloat(rotationAngleRadians), -50, 0, 1)
transform = CATransform3DTranslate(transform, offsetPositioning.x, offsetPositioning.y, -50)
cellContent.layer.transform = transform
cellContent.layer.opacity = 0.2
let delay = 0.06 * Double(indexPath.row)
UIView.animateWithDuration(0.8, delay:delay , usingSpringWithDamping: 0.8, initialSpringVelocity: 0.5, options: .CurveEaseIn, animations: { () -> Void in
cellContent.layer.transform = CATransform3DIdentity
cellContent.layer.opacity = 1
}) { (Bool) -> Void in
}
loadedIdx.append(indexPath.row)
}
}
loadedIdx是一个数组,用于标记单元格为加载(下一次出现时不显示动画)。
https://stackoverflow.com/questions/38435196
复制相似问题