2

I am animating images with UIImageView:

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

At some point later I am trying to change images to some new ones:

    [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];

This does NOT work. My app either crashes or images disappear. I've seen a few people have the same problem and they create various workaround. Is something wrong with my code? Can something be done?

In addition I discovered that with this code below the object disappears at end of animation.

    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];
Vad
  • 3,658
  • 8
  • 46
  • 81
  • I'm not sure why you are calling [imageView.animationImages release] after setting imageView.animationImages to nil, but I don't think that is your problem. – EricS Sep 01 '11 at 03:32
  • Are you sure that imgNameArr is an array of UIImages? Code nearly identical to what you have works fine for me. – EricS Sep 01 '11 at 03:51
  • yes, the array is not identical. – Vad Sep 01 '11 at 04:02
  • More on error. There are 4 UIImageView objects I am moving along the path. When they reach end if animation (using CAAnimation) they should change their images. Those UIImageViews that move from right to left, disappear when the change of images occurs. Strangely enough, those that move from left to right, do not disappear and seem to work. – Vad Sep 01 '11 at 04:13

2 Answers2

1

Since animationImages is a property, you don't need to set it to nil, and releasing it AFTER you set it to nil does nothing (it sends release to nil). Releasing it beforehand will cause a crash. Just set it to the new value, or to nil. You shouldn't be releasing another object's properties.

AndrewS
  • 8,196
  • 5
  • 39
  • 53
0

I do not know why your code does not work.

It is my sample. It works fine.

    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];
ChangUZ
  • 5,380
  • 10
  • 46
  • 64
  • Please check my code sample I just added to the question on top. Moving object left makes the object disappear from screen at end of animation. Moving from left to right works just perfect. – Vad Sep 01 '11 at 11:30