【问题标题】:UIImageView animationImages Cannot Be Changed?UIImageView 动画图像无法更改?
【发布时间】:2011-09-01 03:26:46
【问题描述】:

我正在使用 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];

【问题讨论】:

  • 我不确定你为什么在将 imageView.animationImages 设置为 nil 后调用 [imageView.animationImages release],但我认为这不是你的问题。
  • 你确定 imgNameArr 是一个 UIImage 数组吗?代码几乎与您的代码相同,对我来说效果很好。
  • 是的,数组不相同。
  • 更多关于错误的信息。我沿着路径移动了 4 个 UIImageView 对象。当他们到达动画结束时(使用 CAAnimation)他们应该改变他们的图像。那些从右向左移动的 UIImageView 在图像发生变化时消失。奇怪的是,那些从左到右移动的,并没有消失,似乎在工作。

标签: iphone objective-c animation uiimageview


【解决方案1】:

由于 animationImages 是一个属性,因此您不需要将其设置为 nil,并且在将其设置为 nil 后释放它不会执行任何操作(它将release 发送到 nil)。提前释放它会导致崩溃。只需将其设置为新值或为零。你不应该释放另一个对象的属性。

【讨论】:

  • 释放它或设置为 nil 是我尝试解决的问题。
【解决方案2】:

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

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

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

【讨论】:

  • 请检查我刚刚添加到顶部问题的代码示例。向左移动对象使对象在动画结束时从屏幕上消失。从左到右移动非常完美。
猜你喜欢
  • 1970-01-01
  • 2018-12-05
  • 1970-01-01
  • 1970-01-01
  • 2021-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多