UICollectionviewCellanimation

我想animation的collectionview单元格,animation应该是这样的:

第一个单元出现,然后延迟一段时间后, 第二个单元将出现一些animation ,这将继续像所有的单元格一样。

如何做到这一点?

一个解决scheme是增量创build数据源,以便在设定的时间延迟之后添加一个单元格。 一旦添加,插入

拨打电话设置一个计时器,以任何你喜欢的延迟添加到你的数据源

[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将被重载animation。

我希望这有帮助

这是在Objective-C中,但如果你正在迅速写这个原则,也适用相同的原则

另一个解决scheme是使用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是一个将单元格标记为已加载的数组(在下次出现时不会生成animation)