I have a UIViewController on a UINavigationController with a special NSObject that performs some animations on it. The method that performs the animation looks like this:
- (void) animateView:(UIView*) aView toPosition:(CGPoint) aPositionEnd duration:(CGFloat)duration delegate:(id)delegate {
[self.layer addSublayer:aView.layer];
CGPoint aViewPosition = aView.center;
aView.center = OUT_OF_BOUNDS_POSITION; // Make sure this image position is somewhere out of the view
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, aViewPosition.x, aViewPosition.y);
CGPathAddQuadCurveToPoint(path, NULL, aPositionEnd.x, aViewPosition.y, aPositionEnd.x, aPositionEnd.y);
// Uncomment this to draw the path the thumbnail will fallow
// [_mainView setPath:path];
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.path = path;
//pathAnimation.duration = duration;
pathAnimation.removedOnCompletion = NO;
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
CATransform3D t = CATransform3DMakeScale(0.1, 0.1, 1.0);
scaleAnimation.toValue = [NSValue valueWithCATransform3D:t];
scaleAnimation.removedOnCompletion = NO;
CABasicAnimation *alphaAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
alphaAnimation.toValue = [NSNumber numberWithFloat:0.0f];
alphaAnimation.removedOnCompletion = NO;
//CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@""];
//CATransform3D t = CATransform3DMakeRotation(degreesToRadian(60.0f) , 1, 0, 0);
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
animationGroup.animations = [NSArray arrayWithObjects:pathAnimation, scaleAnimation, alphaAnimation, nil];
animationGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animationGroup.duration = duration;
animationGroup.delegate = delegate;
animationGroup.removedOnCompletion = YES;
[aView.layer addAnimation:animationGroup forKey:nil];
CFRelease(path);
}
‘aView’ is a UIImageView, ‘self’ is the UIViewController, and ‘delegate’ is the NSObject. If the animation completes and I pop the UIViewController, everything is fine: animationDidStop:finished: is called in the NSObject and the UIViewController releases the NSObject in its dealloc. If, however, I pop the UIViewController while the animation is happening, animationDidStop:finished: is called as before, but the NSObject isn’t deallocated afterwards, even though animationGroup.removedOnCompletion is set to YES! This definitely happens because CAAnimationGroup isn’t releasing the delegate, as it should be. (CAAnimation delegates are retained.) I have no idea why this would be. Any ideas/suggestions?
Perhaps this is in some way related to the behavior I asked about here. Are UINavigationControllers just buggy?
I switched over to addSubview: instead of addSublayer: and everything worked okay.