将图像视图添加到图层

时间:2010-08-18 10:39:56

标签: iphone arrays uiimageview cgimage

我确信这很简单,但我试图将一组图像添加到图层中。以下是我到目前为止的情况:

// Create the fish layer
 fishLayer = [CALayer layer];
 //fish  = [UIImageView imageNamed:@"Fish.png"];

 fish.animationImages = [NSArray arrayWithObjects:
        [UIImage imageNamed:@"swim01.png"],
        [UIImage imageNamed:@"swim02.png"],
        [UIImage imageNamed:@"swim03.png"],
        [UIImage imageNamed:@"swim04.png"],
        [UIImage imageNamed:@"swim05.png"],
        [UIImage imageNamed:@"swim06.png"],
        [UIImage imageNamed:@"swim05.png"],
        [UIImage imageNamed:@"swim04.png"],
        [UIImage imageNamed:@"swim03.png"],
        [UIImage imageNamed:@"swim02.png"], nil];

 fish.animationDuration = 1.50;
 fish.animationRepeatCount = -1;
 [fish startAnimating];


 //[self.view addSubview:fish];
 //This should add the animated array to layer.
 fishLayer.contents = fish;

 fishLayer.bounds = CGRectMake(0, 0, 56, 56);
 fishLayer.position = CGPointMake(self.view.bounds.size.height / 2,
         self.view.bounds.size.width / 2);

[self.view.layer addSublayer:fishLayer];

没有错误,但图像数组不会出现在屏幕上。我想也许这条线就是探测器。

fishLayer.contents = fish;

我已将imageview添加到我的头文件中并将其添加到XIB

如果可以,请提供帮助,

干杯,

亚当

1 个答案:

答案 0 :(得分:0)

阅读你的代码,看起来你没有初始化鱼。由于您没有收到任何错误,我认为它是一个实例变量,这意味着它最初设置为nil。因此,当你设置fish.animationImages时,你基本上什么都不做(鱼是零)。与此代码段中的其他鱼类使用相同。

看起来您最初使用的是视图,但随后对此进行了评论。你为什么要尝试使用图层?你应该能够做到这一点:

fish = [UIImageView imageNamed:@"Fish.png"];

fish.animationImages = [NSArray arrayWithObjects:
        [UIImage imageNamed:@"swim01.png"],
        [UIImage imageNamed:@"swim02.png"],
        [UIImage imageNamed:@"swim03.png"],
        [UIImage imageNamed:@"swim04.png"],
        [UIImage imageNamed:@"swim05.png"],
        [UIImage imageNamed:@"swim06.png"],
        [UIImage imageNamed:@"swim05.png"],
        [UIImage imageNamed:@"swim04.png"],
        [UIImage imageNamed:@"swim03.png"],
        [UIImage imageNamed:@"swim02.png"], nil];

fish.animationDuration = 1.50;
fish.animationRepeatCount = -1;
[fish startAnimating];

[self.view addSubview:fish];

fish.bounds = CGRectMake(0, 0, 56, 56);
fish.position = CGPointMake(self.view.bounds.size.height / 2,
         self.view.bounds.size.width / 2);