将图像视图添加到图层
我确信这很简单,但我正在尝试将图像数组添加到图层中。到目前为止,这是我所得到的:
// 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
阅读你的代码,看起来你没有初始化鱼。由于您没有收到任何错误,我假设它是一个实例变量,这意味着它最初设置为 nil。所以当你设置fish.animationImages时,你基本上什么都不做(fish为零)。与此代码片段中 Fish 的所有其他用法相同。
看起来您最初使用的是视图,但后来将其全部注释掉了。你为什么要尝试使用图层?你应该能够这样做:
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: