CABasic не избавляется от изображения после завершения анимации

У меня есть кнопка в файле NIB, которая воспроизводит звук и вызывает этот метод ниже.

-(void) animateHeart
{
    heartLayer = [[CALayer alloc] init];
    [heartLayer setBounds:CGRectMake(0.0, 0.0, 85.0, 85.0)];
    [heartLayer setPosition:CGPointMake(150.0, 100.0)]; 

    UIImage *heartImage = [UIImage imageNamed:@"heart.png"];
    CGFloat nativeWidth = CGImageGetWidth(heartImage.CGImage) / 3;
    CGFloat nativeHeight = CGImageGetHeight(heartImage.CGImage) / 3;

    CGRect  startFrame = CGRectMake(165.0, 145.0, nativeWidth, nativeHeight);
    heartLayer.contents = (id)heartImage.CGImage;
    heartLayer.frame = startFrame;
    [self.view.layer addSublayer:heartLayer];

    CABasicAnimation *theAnimation;

    theAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"];
    theAnimation.duration=2.5;
    theAnimation.repeatCount=2;
    theAnimation.speed = 1.85;
    theAnimation.autoreverses=YES;
    theAnimation.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut];
    theAnimation.fromValue=[NSNumber numberWithFloat:0.0];
    theAnimation.toValue=[NSNumber numberWithFloat:1.0];
    [theAnimation setValue:heartLayer forKey:@"parentLayer"];

    [heartLayer addAnimation:theAnimation forKey:@"animateOpacity"];

    theAnimation.fillMode = kCAFillModeRemoved;
    theAnimation.removedOnCompletion = YES;


}

Этот метод прекрасно анимирует изображение, но после этого изображение остается висеть в представлении, несмотря на то, что для параметра removeOnCompletion BOOL установлено значение true. Я хочу, чтобы изображение исчезло после завершения анимации. Буду признателен за любую помощь, которую я могу здесь получить.

Заранее спасибо, ТАК.


person Rehat Kathuria    schedule 05.06.2012    source источник


Ответы (2)


Установка removedOnCompletion в YES гарантирует, что анимация будет удалена из слоя изображения по завершении, а не то, что слой изображения будет удален из суперслоя. Вам нужно добавить делегата, отслеживать событие завершения и вызвать removeFromSuperview в вашем представлении. Реализуйте метод - (void)animationDidStart:(CAAnimation *)theAnimation для делегата, затем вызовите [heartLayer removeFromSuperlayer].

person Sergey Kalinichenko    schedule 05.06.2012
comment
Я забыл добавить делегата. Большое спасибо за вашу помощь, добрый сэр! - person Rehat Kathuria; 06.06.2012
comment
@RehatKathuria Добро пожаловать! Если ответ работает для вас, подумайте о том, чтобы принять его, щелкнув контур флажка рядом с ответом. Это скажет другим, что вы больше не ищете лучшего ответа, и принесет вам новый значок на Stack Overflow. - person Sergey Kalinichenko; 06.06.2012

Положил:

theAnimation.fillMode = kCAFillModeRemoved;
theAnimation.removedOnCompletion = YES;

До:

[heartLayer addAnimation:theAnimation forKey:@"animateOpacity"];
person graver    schedule 05.06.2012