CABasicAnimation中的内存泄漏

时间:2013-06-17 13:13:41

标签: ios memory-management

以下代码指向内存分配泄漏(使用分析器中的分配工具) - 有人可以指出为什么 - 我使用CABAsicAnimation来轮播UIImageView

-(void)SetGaugeToValueInAngles: (int)newValue
{

    static int oldValue = 0;

    int delta = newValue - oldValue;

    CABasicAnimation *rotate = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    [rotate setDelegate:self];
    rotate.fromValue = [NSNumber numberWithFloat:oldValue/57.2958];
    rotate.removedOnCompletion = NO;
    rotate.fillMode = kCAFillModeForwards;

    rotate.toValue = [NSNumber numberWithFloat:  newValue/57.2958];

    rotate.duration = 10.0; // seconds

    [self.UIImageViewPressureMark.layer addAnimation:rotate forKey:nil]; // "key" is optional

    NSLog(@"Old Value %d New Value %d", oldValue, newValue);

    oldValue = newValue;
}

2 个答案:

答案 0 :(得分:1)

如果您将removeOnCompletion设置为NO,图层将保留动画,直到您将其删除。

您应该将removedOnCompletion设置为YES,或检查图层是否已有动画,并在添加下一个newValue时将其删除

答案 1 :(得分:0)

释放此类的实例时,应调用removeAnimationForKey。 做如下 1.改变

[self.UIImageViewPressureMark.layer addAnimation:rotate forKey:nil];

[self.UIImageViewPressureMark.layer addAnimation:rotate forKey:@"myAnimation"]; 

在您的方法“SetGaugeToValueInAngles”

2.当您重新启动此类的实例时,请调用以下方法

- (void)invalateAnimation
{
    [self.UIImageViewPressureMark.layer removeAnimationForKey:@"myAnimation"];
    self.UIImageViewPressureMark.layer = nil;
}