【问题标题】:UIViewController popped -> CAAnimation.delegate not released?UIViewController 弹出 -> CAAnimation.delegate 未发布?
【发布时间】: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


【解决方案1】:

我感觉这更有可能是您对以下两种方法的期望

animationDidStop:finished:

removedOnCompletion:

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

在这种情况下,您需要使用手动删除动画

removeAllAnimations

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

【讨论】:

  • 嗯,这是个好主意。我一定会试试的。如果是真的,这似乎是一个奇怪的设计决定,不过......
  • 很遗憾,这不起作用。动画在到达回调时没有 animationKeys。我发现委托在完成后永远不会被释放:不是。这可能是 CAAnimationGroup 的错误。同时,我可能只需要在回调中手动调用 [self release]。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-03
    • 2018-05-22
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多