UICollectionviewCell Animations

人走茶凉 提交于 2020-01-03 06:21:06

问题


I want to animate the collectionview cells,the animations should be like this:

First Cell Appear and then after some delay second cell will appear with some animations,this will go on like this for all the cells.

How to achieve this?


回答1:


A solution would be to create the datasource incrementally so you add a cell after a set timed delay. Once added, insert

Make a call to set up a timer to add to your dataSource with whatever delay you like

[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(addToDataSource:) userInfo:nil repeats:YES];

Then within this method that will hit every 0.05 seconds (in this example)

-(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

}

The performBatchUpdate will mean the collectionView will be reloaded with animation.

I hope this helps

This is in objective-C, though same principles apply if you're writing this in swift




回答2:


The other solution here is using willDisplayCell:forIndexPath delegate

Here is a sample how i do it.

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)
    }

}

The loadedIdx is an array to mark cell as loaded ( not animate in the next time appear)



来源:https://stackoverflow.com/questions/38435196/uicollectionviewcell-animations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!