我制作了一堆个别的小图片,每张图片都有一个单独的CALayer,并且它们应该以不同的速率异步地淡入和淡出。每个都有一个背景子图层和一个填充子图层。计时器在后台运行,在特定时刻单独为每个人设置动画。对于该程序的当前行为,而不是为每个单独的动画制作动画,而是围绕它动画化整个屏幕。你能帮我做到这一点吗?它一次只能激活一个图像?CABasicAnimation在应该只动画一件东西的时候动画所有东西
编辑:我应该更清楚。时机不是问题。在我切换到CoreAnimation之前,这些图像在适当的时候正确地动画。但是,这里的主要问题是,当我告诉一个图像进行动画制作时,整个屏幕(包括所有图像之外的屏幕的背景部分)都会生成动画。
以下代码段用于在UIView代码中创建分层结构。 Self指的是主要的UIView。返回值用于为代表屏幕上这些小图像之一的类设置CALayer成员变量。
- (CALayer *) createImageLayer:(CGPoint)orig Center:(CGPoint)pos {
CALayer *parentLayer = [self layer];
CALayer *childLayer1 = [CALayer layer];
childLayer1.bounds = CGRectMake(0, 0, 40.0f, 40.0f);
childLayer1.position = pos;
CALayer *childLayer2 = [CALayer layer];
childLayer2.bounds = CGRectMake(0, 0, 40.0f, 40.0f);
childLayer2.position = pos;
float components[4] = {1.0, 1.0, 1.0, 1.0};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGColorRef whiteColor = CGColorCreate(colorSpace, components);
childLayer1.backgroundColor = whiteColor;
childLayer2.backgroundColor = whiteColor;
UIImage *childImage = [UIImage imageNamed:@"back.png"];
UIImage *childImage2 = [UIImage imageNamed:@"fill.png"];
CGImageRef imageRef = [childImage CGImage];
CGImageRef imageRef2 = [childImage2 CGImage];
childLayer1.contents=(id)imageRef;
childLayer2.contents=(id)imageRef2;
[parentLayer addSublayer:childLayer1];
[parentLayer addSublayer:childLayer2];
CGColorSpaceRelease(colorSpace);
CGColorRelease(whiteColor);
return parentLayer;
}
告诉它动画的代码。图像对象被告知将动画添加到其图层成员。
- (void) fadeAnimation:(ImageClass *)image {
CABasicAnimation *theAnimation;
theAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"];
theAnimation.duration=1.0; // fixed duration for now
theAnimation.repeatCount=1;
theAnimation.autoreverses=YES;
theAnimation.fromValue=[NSNumber numberWithFloat:1.0];
theAnimation.toValue=[NSNumber numberWithFloat:0.0];
[image.myLayer addAnimation:theAnimation forKey:@"animateOpacity"];
}
谢谢你的帮助。我得到的主要问题是,当我想要告诉每个单独的图像执行淡入淡出动画时,例如[self fadeAnimation:myImage],它会将动画显示在整个屏幕上,而不会显示在图像上。时机对我来说并不是真正的问题,但我可以尝试一下你的建议,因为这对我的目的而言似乎比计时器更适合。 – William 2010-06-22 20:43:14