【发布时间】:2011-06-28 18:33:36
【问题描述】:
我在 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 的行为有关。 UINavigationController 是不是有问题?
【问题讨论】:
-
为什么不在animationDidStop:中将delegate设为nil?
-
只要!我收到一个异常,抱怨 CAAnimationGroup 是只读的或类似的。
标签: objective-c ios cocoa-touch