2011-06-28 29 views
0

我有一個UIViewController UINavigationController與一個特殊的NSObject,它執行一些動畫。執行該動畫的方法是這樣的:UIViewController彈出 - > CAAnimation.delegate沒有發佈?

- (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,「自我」是的UIViewController,和「代理」是NSObject的。如果動畫完成並彈出UIViewController,則一切正常:animationDidStop:finished:在NSObject中調用,並且UIViewController在其dealloc中釋放NSObject。但是,如果在動畫發生時彈出UIViewController,則animationDidStop:finished:將像以前一樣被調用,但即使animationGroup.removedOnCompletion設置爲YES,NSObject也不會解除分配!這肯定發生,因爲CAAnimationGroup沒有發佈委託,因爲它應該是。 (CAAnimation代表保留。)我不知道爲什麼會這樣。任何想法/建議?

也許這在某種程度上與我詢問的有關here的行爲有關。 UINavigationControllers只是越野車?

+0

爲什麼不在animationDidStop中設置委託爲零:? – Eiko

+0

如果只有!我收到一個抱怨CAAnimationGroup爲只讀或類似的異常。 – Archagon

回答

0

我切換到addSubview:而不是addSublayer:並且一切正常。

0

我感覺它更可能是你在做什麼以下兩種方法的期望

animationDidStop:finished: 

removedOnCompletion: 

例如,當你彈出視圖控制器和動畫仍在運行,是傳遞給委託方法NO的布爾值嗎?在這種情況下,您不可能期望在完成時將其刪除,因爲您從未給它完成的機會。

在這種情況下,你需要彈出視圖控制器時,而動畫仍然在運行使用

removeAllAnimations 

手動刪除您的動畫。

+0

嗯,這是一個好主意。我一定會嘗試。似乎是一個奇怪的設計決定,如果屬實,雖然... – Archagon

+0

可悲的是,這並沒有奏效。到達回調時動畫沒有動畫鍵。我發現代表從未完成時發佈:是NO。這可能是CAAnimationGroup的一個錯誤。同時,我可能只需在回調中手動調用[self release]。 – Archagon