CALayer隐藏其子层

时间:2012-03-13 18:35:11

标签: iphone objective-c cocoa-touch core-animation

我正在尝试添加CALayer作为另一个CALayer的子层。但是只显示父图层。这是我的代码:

//display a green square:
CALayer *shipContainer = [CALayer layer];
shipContainer.bounds = CGRectMake(0,0,200,200);
shipContainer.position = CGPointMake(600,500);
shipContainer.borderColor = [UIColor greenColor].CGColor;
shipContainer.borderWidth = 3;

//display a red dot inside the square:    
CALayer *ship1 = [CALayer layer];
ship1.bounds = CGRectMake(0,0,20,20);
ship1.position = CGPointMake(600,500);
ship1.cornerRadius = 10;
ship1.backgroundColor = [UIColor redColor].CGColor;
[shipContainer addSublayer:ship1];

然后我调用[self.view.layer addSublayer:shipContainer];,但只显示绿色方块。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

根据documentation

<强>位置

  

position属性是一个指定位置的CGPoint   相对于其超层的层,并在超层中表示   坐标系。

所以你需要改变

ship1.position = CGPointMake(600,500);

以便ship1可以进入可见区域。由于超级层的边界为200,200,因此您需要使位置xy小于这些值。

相关问题