将图像视图添加到图层

发布于 2024-09-15 09:20:20 字数 1259 浏览 3 评论 0原文

我确信这很简单,但我正在尝试将图像数组添加到图层中。到目前为止,这是我所得到的:

// 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 中,

如果可以的话请帮忙,

干杯,

Adam

I'm sure this is simple but I am trying to add an array of images to a layer. Here is what I have so far:

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

There is no error but the array of images don't appear on the screen. I think maybe this line is the probem..

fishLayer.contents = fish;

I have added the imageview to my header files and added it in the XIB

Please help if you can,

Cheers,

Adam

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏末的微笑 2024-09-22 09:20:20

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

看起来您最初使用的是视图,但后来将其全部注释掉了。你为什么要尝试使用图层?你应该能够这样做:

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

Reading your code, it looks like you're not initializing fish. Since you're not getting any errors, I assume it is an instance variable, which means it gets set to nil initially. So when you set fish.animationImages, you essentially do nothing (fish is nil). Same with every other use of fish in this code snippet.

It looks like you were using views initially, but then commented all that out. Why are you trying to use a layer? You should be able to just do this:

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文