【问题标题】:Adding sublayer to view.layer changes the frame position?将子层添加到 view.layer 会改变框架位置?
【发布时间】:2013-01-06 05:50:40
【问题描述】:

我将CALayer 作为子层添加到UIViewlayer 属性,如下所示:

_graphicLayer = [[GraphicLayer alloc] init];
        self.bounds = _graphicLayer.bounds;
        _graphicLayer.position = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
        [self.layer addSublayer:_graphicLayer];

如您所见,我更改了 _graphicLayer 的位置以说明居中的anchorPoint。添加此子层后,我注意到它正在将视图 frame 更改为 (-self.bounds.width / 2, -self.bounds.height / 2)。为什么会这样?如果我改变 _graphicLayer 的位置,我认为这只是相对于它的父视图。为什么它会影响frame 属性的视图? (而且我不想调整视图层属性或_graphicLayer 的anchorPoint。

【问题讨论】:

    标签: iphone ios cocoa-touch core-animation calayer


    【解决方案1】:

    你目前正在做

    self.bounds = _graphicLayer.bounds;
    

    这会改变视图的(自己的)框架,因为它会改变宽度和高度。也许你打算这样做:

    _graphicLayer.bounds = self.bounds;
    

    请参阅此处了解边界如何影响框架:Link

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多