Objective c 和CALayer有问题吗
所以我有一个类,它返回一个CALayer,其中添加了一个CALayer数组作为子层,但问题是它似乎没有呈现其中的任何一个。我想我可能只是错过了一些关于CALayers是如何工作的,因为我还不是很熟悉。有人能看看我的代码,看看我做错了什么吗Objective c 和CALayer有问题吗,objective-c,core-animation,Objective C,Core Animation,所以我有一个类,它返回一个CALayer,其中添加了一个CALayer数组作为子层,但问题是它似乎没有呈现其中的任何一个。我想我可能只是错过了一些关于CALayers是如何工作的,因为我还不是很熟悉。有人能看看我的代码,看看我做错了什么吗 - (CALayer*)drawMap { CALayer* theLayer = [CALayer layer]; for (int y = 0; y < [self.height intValue]; y++) { f
- (CALayer*)drawMap {
CALayer* theLayer = [CALayer layer];
for (int y = 0; y < [self.height intValue]; y++) {
for (int x = 0; x < [self.width intValue]; x++) {
CALayer* tileLayer = [CALayer layer];
tileLayer.bounds = CGRectMake((x * [self.tileWidth intValue]), (y * [self.tileHeight intValue]), [self.tileWidth intValue], [self.tileHeight intValue]);
[tileLayer setBackgroundColor:[UIColor colorWithHue:(CGFloat)x saturation:(CGFloat)y brightness:255 alpha:1.0].CGColor];
[theLayer addSublayer:tileLayer];
}
}
[theLayer setBounds:CGRectMake([self.width intValue] * [self.tileWidth intValue], [self.height intValue] * [self.tileHeight intValue], 10, 10)];
return theLayer;
}
如果需要,我可以提供标题,但我认为问题不在那里,我也不想要一篇长篇大论。我认为问题在于您对父层设置的边界:
[theLayer setBounds:
CGRectMake(
[self.width intValue] * [self.tileWidth intValue],
[self.height intValue] * [self.tileHeight intValue],
10, 10)];
这似乎设置了边界,使其刚好从刚刚添加的所有子层开始,并在两个方向上延伸10个点。我认为以下几点更合适:
[theLayer setBounds:
CGRectMake(
0,
0,
[self.width intValue] * [self.tileWidth intValue],
[self.height intValue] * [self.tileHeight intValue])];
以下内容看起来也很可疑:
[self.view.layer insertSublayer:[myMap drawMap] above:self.view.layer];
[myMap drawMap]
将成为self.view.layer
的子层<代码>self.view.layer不是它自己的子视图之一,因此它不知道如何处理上面的指令:
指令。您可能只需要添加子层:
最后,这是:
tileLayer.bounds = CGRectMake((x * [self.tileWidth intValue]), (y * [self.tileHeight intValue]), [self.tileWidth intValue], [self.tileHeight intValue]);
为每个子层提供完全相同的帧
。与UIView
s非常相似,bounds
是信息的源区域,帧是将信息放在超层中的位置。我想您可能需要设置帧
最后,UIColor+colorWithHue:…
将色调和饱和度钳制在[0,1]范围内,因此您可能需要(CGFloat)x/[self.width floatValue]
和饱和度的等效值,以确保所有瓷砖以不同的颜色呈现出来
编辑:因此,将所有这些结合在一起,我稍微修改了您的代码,这样我就不必编写完整的包含类,并将其添加到基于Xcode视图的项目模板中,如下所示:
#define kTileWidth 3
#define kTileHeight 3
#define kWidth 100
#define kHeight 100
- (CALayer*)drawMap {
CALayer* theLayer = [CALayer layer];
for (int y = 0; y < kHeight; y++)
{
for (int x = 0; x < kWidth; x++)
{
CALayer* tileLayer = [CALayer layer];
tileLayer.frame = CGRectMake((x * kTileWidth), (y * kTileHeight), kTileWidth, kTileHeight);
[tileLayer setBackgroundColor:[UIColor colorWithHue:(CGFloat)x/kWidth saturation:(CGFloat)y/kHeight brightness:255 alpha:1.0].CGColor];
[theLayer addSublayer:tileLayer];
}
}
[theLayer setFrame:CGRectMake(0, 0, kWidth * kTileWidth, kHeight * kTileHeight)];
return theLayer;
}
// ...
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.view.layer addSublayer:[self drawMap]];
}
#定义kTileWidth 3
#定义kTileHeight 3
#定义宽度100
#定义kHeight 100
-(CALayer*)绘图地图{
CALayer*图层=[CALayer图层];
对于(int y=0;y
这使得色调/饱和度平面在许多艺术包装中都很常见。明显的问题:瓷砖宽度和瓷砖高度是否有合理的值?谢谢,这就解决了这个问题。我只是在学习所有这些核心动画的东西,所以我认为这是个错误,但看起来我所有的错误都是基本的东西。我不太担心颜色是否正确,我只是想给瓷砖上色以便我能看到它们。我正在尝试使用核心动画制作一个2D平铺游戏引擎。
#define kTileWidth 3
#define kTileHeight 3
#define kWidth 100
#define kHeight 100
- (CALayer*)drawMap {
CALayer* theLayer = [CALayer layer];
for (int y = 0; y < kHeight; y++)
{
for (int x = 0; x < kWidth; x++)
{
CALayer* tileLayer = [CALayer layer];
tileLayer.frame = CGRectMake((x * kTileWidth), (y * kTileHeight), kTileWidth, kTileHeight);
[tileLayer setBackgroundColor:[UIColor colorWithHue:(CGFloat)x/kWidth saturation:(CGFloat)y/kHeight brightness:255 alpha:1.0].CGColor];
[theLayer addSublayer:tileLayer];
}
}
[theLayer setFrame:CGRectMake(0, 0, kWidth * kTileWidth, kHeight * kTileHeight)];
return theLayer;
}
// ...
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.view.layer addSublayer:[self drawMap]];
}