UIImageView animationImages не может быть изменено? - PullRequest
2 голосов
/ 01 сентября 2011

Я анимирую изображения с помощью UIImageView:

    imageView.animationImages = [NSArray arrayWithArray:imgNameArr];    
    imageView.animationDuration = 2;
    imageView.animationRepeatCount = 0;
    [imageView startAnimating];

В какой-то момент позже я пытаюсь изменить изображения на новые:

    [imageView stopAnimating];
    imageView.animationImages = nil;
    [imageView.animationImages release]; 
    imageView.animationImages = [NSArray arrayWithObjects:  
            [UIImage imageNamed:@"1.png"],
    [UIImage imageNamed:@"2.png"],
    nil];
    [imageView setAnimationDuration:1];
    [imageView startAnimating];

Это НЕ работает. Мое приложение либо вылетает, либо изображения исчезают. Я видел, что у некоторых людей есть такая же проблема, и они создают различные обходные пути. Что-то не так с моим кодом? Можно ли что-то сделать?

Кроме того, я обнаружил, что с этим кодом объект исчезает в конце анимации.

    int x = rect.origin.x;
int y = rect.origin.y;
int w = rect.size.width;    
    float _frequency = 0;
    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.duration = speed;
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;

CGMutablePathRef pointPath = CGPathCreateMutable();
CGPathMoveToPoint(pointPath, NULL, rect.origin.x, rect.origin.y);

    do {        
        CGFloat yVal = sin (_frequency * M_PI/180);
        CGPathAddLineToPoint(pointPath, NULL, x, y + yVal * 15);
        _frequency += freq;
        x--;        
    } while (x > w);//does not go outside

pathAnimation.path = pointPath;
CGPathRelease(pointPath);

[imageView.layer addAnimation:pathAnimation forKey:@"pathAnimation"];
[imageView release];

Ответы [ 2 ]

1 голос
/ 01 сентября 2011

Так как animationImages является свойством, вам не нужно устанавливать его в ноль, а после его отпускания ПОСЛЕ того, как вы установите его в ноль, ничего не происходит (он отправляет release в ноль).Выпуск этого заранее вызовет сбой.Просто установите его на новое значение или на ноль.Вы не должны освобождать свойства другого объекта.

0 голосов
/ 01 сентября 2011

Я не знаю, почему ваш код не работает.

Это мой образец. Работает нормально.

    animationView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];

    NSMutableArray *animationImages = [[NSMutableArray alloc] init];
    for (int i=0;  i < [contents count]; i++) {
        [animationImages addObject: [UIImage imageNamed:[contents objectAtIndex:i]]];
    }

    animationView.animationImages = animationImages;
    animationView.animationDuration = playTime;
    animationView.animationRepeatCount = 0;

    [self.view addSubview:animationView];
    [animationView startAnimating];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...