在从UICollectionView删除单元格之前,在项目之后删除动画

时间:2015-01-08 16:38:45

标签: ios objective-c uicollectionview core-animation nsfetchedresultscontroller

我已经构建了一个通过NSFetchResultsController进行更新的集合视图控制器。

当控制器设置为编辑时,我会在集合视图中摆动项目单元格以突出显示用户处于不同的模式,因此在点击项目时应该期望删除行为。

问题是当我开始编辑然后删除项目时,删除的单元格之后的所有其他项目停止动画。

Screen recording to show issue

如果您观看上面的GIF,您会看到我开始编辑并且所有项目都在摇晃。当我删除第3项时,下面的所有项目(4,5,6等)停止摇摆,而其上方的所有单元格(0,1)仍然摇晃。我的猜测是问题是Apple的删除动画的实现删除了所有动画,但我希望不会。

可以在SQKFetchedCollectionViewController功能分支中找到所有源here。克隆repo后,您必须从Project文件夹运行pod install。对工作区进行操作,您可以在示例目标中找到SQKCommitsCollectionViewController,然后在Pods目标中找到将数据作为开发窗格的代码。

1 个答案:

答案 0 :(得分:0)

感谢@ ian-macdonald,我调查了使用CAAnimation而不是[UIView animateWith选项。

旧方法

- (void)startWiggling
{
    CGAffineTransform leftWobble = CGAffineTransformMakeRotation(degreesToRadians(animationRotateDegree * +1));
    CGAffineTransform rightWobble = CGAffineTransformMakeRotation(degreesToRadians(animationRotateDegree * -1));
    CGAffineTransform moveTransform = CGAffineTransformTranslate(rightWobble, -animationTranslateX, -animationTranslateY);
    CGAffineTransform conCatTransform = CGAffineTransformConcat(rightWobble, moveTransform);

    self.transform = leftWobble;

    CGFloat delay = (((float) rand() / RAND_MAX) * 0.09) + 0.04;

    [UIView animateWithDuration:0.1
                          delay:delay
                        options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
                     animations:^{
                         self.transform = conCatTransform;
                     }
                     completion:nil];
}

新的填充方法,适用于集合视图的删除动画。

- (void)startWiggleing
{
    CAKeyframeAnimation *position = [CAKeyframeAnimation animation];
    position.keyPath = @"position";
    position.values = @[
                        [NSValue valueWithCGPoint:CGPointZero],
                        [NSValue valueWithCGPoint:CGPointMake(-1, 0)],
                        [NSValue valueWithCGPoint:CGPointMake(1, 0)],
                        [NSValue valueWithCGPoint:CGPointMake(-1, 1)],
                        [NSValue valueWithCGPoint:CGPointMake(1, -1)],
                        [NSValue valueWithCGPoint:CGPointZero]
                        ];
    position.timingFunctions = @[
                                 [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                 [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]
                                 ];
    position.additive = YES;

    CAKeyframeAnimation *rotation = [CAKeyframeAnimation animation];
    rotation.keyPath = @"transform.rotation";
    rotation.values = @[
                        @0,
                        @0.03,
                        @0,
                        [NSNumber numberWithFloat:-0.02]
                        ];
    rotation.timingFunctions = @[
                                 [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                 [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]
                                 ];

    CAAnimationGroup *group = [[CAAnimationGroup alloc] init];
    group.animations = @[position, rotation];
    group.duration = 0.4;
    group.repeatCount = HUGE_VALF;

    [self.layer addAnimation:group forKey:@"wiggle"];
}
相关问题