Layout Specs

Layout specs

Layout flow

Layout计算是 通过以下启动触发器来递归完成。一个layout布局完成的启动触发器就是 当父节点改变时。这触发器也在第一次node树被创建时发生。

ASDisplayNode第一个重要分支是顶层的[ASDisplayNode calculateLayoutThatFits],这个方法将会选择布局引擎是Texture布局还是Yoga

- (ASLayout *)calculateLayoutThatFits:(ASSizeRange)constrainedSize
{
  __ASDisplayNodeCheckForLayoutMethodOverrides;

  switch (self.layoutEngineType) {
    case ASLayoutEngineTypeLayoutSpec:
      return [self calculateLayoutLayoutSpec:constrainedSize];
#if YOGA
    case ASLayoutEngineTypeYoga:
      return [self calculateLayoutYoga:constrainedSize];
#endif
      // If YOGA is not defined but for some reason the layout type engine is Yoga
      // we explicitly fallthrough here
    default:
      break;
  }

  // If this case is reached a layout type engine was defined for a node that is currently
  // not supported.
  ASDisplayNodeAssert(NO, @"No layout type determined");
  return nil;
}

基于UIKit系统,Texture在不同阶段遵循以下过程

  1. ASDisplayNode和ASLayout初始化。这是完全独立与UIKit存在。这是当一个布局计算创建时
  2. UIView/CALayer初始化。遵循UIKit管理惯例,即在布局大小之前创建要显示的UIKit items。然而,它们是在UIKit层次,允许下列的布局触发器
  3. UIView/CALayer布局。这是UIKit递归操作之后的结果。这和步骤1中的布局计算不同,这是仅仅为了使用已经准备好的布局,并且将其应用到UIView/CALayer中 进行布局调整
  4. 如有必要,在其中渲染CALayer的光栅化。UIKit可以将UIKit层次结构绘制到屏幕上