【问题标题】:CALayer hides its sublayerCALayer 隐藏它的子层
【发布时间】:2012-03-13 18:35:10
【问题描述】:

我正在尝试添加一个 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];,但只显示绿色方块。有什么想法吗?

【问题讨论】:

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


    【解决方案1】:

    根据documentation

    位置

    position 属性是一个 CGPoint,它指定了 层相对于其超层,并表示为超层的 坐标系。

    所以你需要改变

    ship1.position = CGPointMake(600,500);
    

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

    【讨论】:

    • 正确!感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-05
    相关资源
    最近更新 更多