UIViewController弹出 - > CAAnimation.delegate没有发布?

时间:2011-06-28 18:33:36

标签: objective-c ios cocoa-touch

我在UINavigationController上有一个UIViewController,它有一个特殊的NSObject,可以在它上面执行一些动画。执行动画的方法如下所示:

- (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'是UIImageView,'self'是UIViewController,'delegate'是NSObject。如果动画完成并弹出UIViewController,一切都很好:animationDidStop:finished:在NSObject中调用,UIViewController在其dealloc中释放NSObject。但是,如果我在动画发生时弹出UIViewController,则会像之前一样调用animationDidStop:finished:但是之后不会释放NSObject,即使animationGroup.removedOnCompletion设置为YES!这肯定会发生,因为CAAnimationGroup不会释放委托,因为它应该是。 (保留CAAnimation代表。)我不知道为什么会这样。有什么想法/建议吗?

也许这在某种程度上与我询问here的行为有关。 UINavigationControllers是否只是马车?

2 个答案:

答案 0 :(得分:0)

我觉得你更有可能成为以下两种方法的期望

animationDidStop:finished:

removedOnCompletion:

例如,当您弹出视图控制器并且动画仍在运行时,是否将bool值传递给委托方法NO?在这种情况下,您不能指望它在完成时被删除,因为您从未给它机会完成。

在这种情况下,您需要使用

手动删除动画
removeAllAnimations

在动画仍在运行时弹出视图控制器。

答案 1 :(得分:0)

我切换到addSubview:而不是addSublayer:一切正常。