当CABasicAnimation只能为一件事物制作动画时,它会动画一切

时间:2010-06-22 01:36:19

标签: iphone animation core-animation calayer caanimation

我制作了一些小的个人照片,每个都有一个单独的CALayer,它们应该以不同的速率异步淡入淡出。每个都有一个背景子层和一个填充子层。计时器在后台运行,以在特定时刻单独为每个计时器设置动画。对于程序的当前行为,而不是为每个单独的动画制作动画,它会围绕它动画整个屏幕。你可以帮助我做到这一点,它一次只能动画一张图像吗?

编辑:我应该更清楚。时机不是问题。在我切换到CoreAnimation之前,这些图像在适当的时间正确动画。但这里的主要问题是,当我告诉一个图像进行动画制作时,整个屏幕(包括所有图像之外的屏幕背景部分)都会被动画化。

以下代码段用于在UIView代码中创建分层结构。自我指的是主要的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"];
}

1 个答案:

答案 0 :(得分:0)

首先,您的代码可以非常精简,这样可以更容易阅读:

- (CALayer *) createImageLayer:(CGPoint)orig Center:(CGPoint)pos 
{    
    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;

    childLayer1.backgroundColor = [[UIColor whiteColor] CGColor];
    childLayer2.backgroundColor = [[UIColor whiteColor] CGColor];

    childLayer1.contents=(id)[[UIImage imageNamed:@"back.png"] CGImage];
    childLayer2.contents=(id)[[UIImage imageNamed:@"fill.png"] CGImage];

    [[self layer] addSublayer:childLayer1];
    [[self layer] addSublayer:childLayer2];

    return parentLayer;
}

接下来,我建议您不要创建计时器,而只需使用 -performSelector:withObject:afterDelay 。然后,您可以立即添加所有动画,只需错开afterDelay参数。像这样:

int i = 0;
for (i = 0; i < 10; ++i)
{
    [self performSelector:@selector(fadeAnimation:) 
               withObject:images[i] 
               afterDelay:(CGFloat)i];
}

其中images是ImageClass对象的C数组。当然,您不必使用C数组。您可以使用NSArray,但仍需要一个forDelay参数的计数器。这会使您的动画每隔一秒钟错开。我不确定你想要什么,所以我只是把它作为一个例子。

还有其他方法可以满足您对Core Animation的要求。您可以查看动画分组和 beginTime 属性,但我认为上面提到的-peformSelector方法是一个阻力较小的路径。

祝你好运

相关问题