在从 UICollectionView 中删除单元格之前,在项目之后删除动画
Animations are removed after an item before the cell is deleted from a UICollectionView
我构建了一个通过 NSFetchResultsController
.
更新的集合视图控制器
当控制器设置为编辑时,我在集合视图中摆动项目单元格以突出显示用户处于不同的模式,因此当他们点击项目时应该会出现删除行为。
问题是当我开始编辑然后删除一个项目时,删除单元格之后的所有其他项目都停止动画。
如果您观看上面的 gif,您会看到我开始编辑并且所有项目都在摆动。当我删除第 3 个项目时,该项目下方的所有项目(4、5、6 等)停止抖动,而其上方的所有单元格(0、1)保持抖动。我的猜测是问题出在 Apple 的删除动画删除所有动画的实现上,但我希望不会。
所有源代码都可以在 here SQKFetchedCollectionViewController 功能分支中找到。克隆存储库后,您将必须 运行 从 Project 文件夹安装 pod。打开工作区,您可以在示例目标中找到 SQKCommitsCollectionViewController
,然后是在 Pods 目标中提供数据作为开发 pod 的代码。
感谢@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"];
}
我构建了一个通过 NSFetchResultsController
.
当控制器设置为编辑时,我在集合视图中摆动项目单元格以突出显示用户处于不同的模式,因此当他们点击项目时应该会出现删除行为。
问题是当我开始编辑然后删除一个项目时,删除单元格之后的所有其他项目都停止动画。
如果您观看上面的 gif,您会看到我开始编辑并且所有项目都在摆动。当我删除第 3 个项目时,该项目下方的所有项目(4、5、6 等)停止抖动,而其上方的所有单元格(0、1)保持抖动。我的猜测是问题出在 Apple 的删除动画删除所有动画的实现上,但我希望不会。
所有源代码都可以在 here SQKFetchedCollectionViewController 功能分支中找到。克隆存储库后,您将必须 运行 从 Project 文件夹安装 pod。打开工作区,您可以在示例目标中找到 SQKCommitsCollectionViewController
,然后是在 Pods 目标中提供数据作为开发 pod 的代码。
感谢@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"];
}