有没有办法改变UICollectionViews的动画速度?

aka*_*aru 6 uitableview ios6 uicollectionview

我说的是insertItemsAtIndexPaths或者像moveItemAtIndexPath:toIndexPath:.我已经尝试将其包装在动画块中,但这不起作用.似乎动画停留在.25或.3秒,但我想放慢速度.除了自己实施这个不值得麻烦的运动之外,有没有人知道公共API是否可以实现这一点?

Hei*_*erg 6

绝对!但是你无法使用UIView动画块正确地完成它.你需要深入了解Core Animation.

集合视图有助于将动画添加到CALayer支持每个动画的动画中UICollectionViewCell.但是,如果您实现自己的布局(或现有的子类),您可以在finalizeCollectionViewUpdates方法中对这些动画进行所有必要的修改.

UICollectionViewLayout课程文档:

集合视图将此方法称为在将任何更改设置为动画之前的最后一步.

以下是如何使用您自己的替换默认位置动画的示例:

- (void)finalizeCollectionViewUpdates
{
    for (UICollectionViewCell *cell in [self.collectionView visibleCells])
    {
        CAAnimation *positionAnimation = [cell.layer animationForKey:@"position"];
        if (positionAnimation)
        {
            [cell.layer removeAnimationForKey:@"position"];

            CALayer *presentationLayer = cell.layer.presentationLayer;
            CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
            [animation setFromValue:[NSValue valueWithCGPoint:presentationLayer.position]];
            [animation setToValue:[NSValue valueWithCGPoint:cell.layer.position]];
            [animation setDuration:0.4];

            [cell.layer addAnimation:animation forKey:@"position"];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

查看与动画相关的方法CALayer,了解如何深入了解当前在图层上运行的动画.

使用这种方法,您甚至可以创造性地使用CAKeyframeAnimation或者您可以想到的任何其他内容添加过冲/反弹效果.您还可以选择性地禁用某些动画(如大小)并保留其他动画(如位置).