UICollectionviewCell动画

时间:2016-07-18 11:09:58

标签: ios uicollectionview uicollectionviewcell uiviewanimation caanimation

我想动画集合视图单元格,动画应该是这样的:

首先单元格出现,然后在一些延迟第二单元格出现一些动画之后,对于所有单元格,这将继续显示

如何实现这一目标?

2 个答案:

答案 0 :(得分:1)

解决方案是逐步创建数据源,以便在设置的定时延迟后添加单元格。添加后,插入

拨打电话,设置一个计时器,添加到您的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-C中,但如果您在swift

中写这个原则,则适用相同的原则

答案 1 :(得分:1)

此处的另一个解决方案是使用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是一个数组,用于将单元格标记为已加载(下次出现时不显示动画)