【发布时间】: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