I have built a collection view controller that updates via a NSFetchResultsController
.
When the controller is set to editing I wobble the item cells in the collection view to highlight that the user is in a different mode so should expect the delete behaviour when they tap an item.
The issue is when I start editing and then delete an item, all other items after the deleted cell stop animating.
If you watch the gif above you will see I start editing and all items wobble. When I delete the 3rd Item all items below that (4, 5, 6 etc) stop jiggling whilst all cell above it(0, 1) remain jiggling. My guess is that the issue is Apple's implementation of the delete animation that removes all animations but I hope not.
All source can be found here in the SQKFetchedCollectionViewController feature branch. You will have to run pod install from the Project folder once you clone the repo. Opining the workspace you can find the SQKCommitsCollectionViewController
in the example target and then the code that provides the data in the Pods target as a development pod.
Thanks to @ian-macdonald I investigated into using CAAnimation
rather than the [UIView animateWith
option.
Old method
- (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];
}
New padded method that works with the deletion animation of a collection view.
- (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"];
}