UIImageView animationImages无法更改?

时间:2011-09-01 03:26:46

标签: iphone objective-c animation uiimageview

我用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 个答案:

答案 0 :(得分:1)

由于animationImages是一个属性,你不需要将它设置为nil,并且在你将它设置为nil之后释放它什么都不做(它将release发送到nil)。事先释放它会导致崩溃。只需将其设置为新值,或者设置为nil。你不应该释放另一个对象的属性。

答案 1 :(得分:0)

我不知道为什么你的代码不起作用。

这是我的样本。它工作正常。

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