Iphone 将imageview添加到图层
我相信这很简单,但我正在尝试向层中添加一组图像。以下是我到目前为止的情况:Iphone 将imageview添加到图层,iphone,arrays,uiimageview,cgimage,Iphone,Arrays,Uiimageview,Cgimage,我相信这很简单,但我正在尝试向层中添加一组图像。以下是我到目前为止的情况: // Create the fish layer fishLayer = [CALayer layer]; //fish = [UIImageView imageNamed:@"Fish.png"]; fish.animationImages = [NSArray arrayWithObjects: [UIImage imageNamed:@"swim01.png"], [UII
// 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正在阅读您的代码,看起来您没有初始化fish。由于没有得到任何错误,我假设它是一个实例变量,这意味着它最初被设置为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);