代码之家  ›  专栏  ›  技术社区  ›  Thanks

UIView的clipsToBounds和CALayer的masksToBounds之间的关系如何?

  •  52
  • Thanks  · 技术社区  · 17 年前

    A. UIView 有一个 CALayer .这很确定。但两者似乎都提供了相同的意义。

    如果我准备好了 clipsToBounds=YES ,这是否也会设置图层的 masksToBounds=YES ? 为什么有不同的名字?有人知道吗?

    2 回复  |  直到 12 年前
        1
  •  137
  •   Gordon Childs    7 年前

    它们是不同的名字,因为 UIView CALayer 它们是不同的,有不同的术语,但它们在功能上是等价的。如果你拆开 clipsToBounds 你会看到它只是在呼唤 masksToBounds (从模拟器框架中分解,因此x86):

    -(BOOL)[UIView(Rendering) clipsToBounds]
        +0  3091938a  55              pushl    %ebp
        +1  3091938b  89e5            movl     %esp,%ebp
        +3  3091938d  e800000000      calll    0x30919392
        +8  30919392  59              popl     %ecx
        +9  30919393  8b4508          movl     0x08(%ebp),%eax
       +12  30919396  8b5004          movl     0x04(%eax),%edx         (CALayer)_layer
       +15  30919399  8b8186cb1301    movl     0x0113cb86(%ecx),%eax    masksToBounds
       +21  3091939f  89450c          movl     %eax,0x0c(%ebp)
       +24  309193a2  895508          movl     %edx,0x08(%ebp)
       +27  309193a5  c9              leave
       +28  309193a6  e92e211801      jmpl     0x31a9b4d9
    
        2
  •  6
  •   Vikram Sinha    8 年前

    有人问如果clipToBounds设置为false,maskToBounds设置为true会发生什么,或者如果有人添加屏幕截图会更好

    我偶然发现了同一个问题,我想在视图中设置角半径和阴影。

    但这是不可能的,因为一旦我将MasksToBond设置为“是”,它就会移除阴影,但会剪辑内容,如果我将其设置为“否”,它会显示阴影,但不会剪辑内容。

    我做了一个更好地解释它的项目 Understanding the ClipToBound And MasksToBound

    我得出的结论是,CALayer不是一个容器,但它只是一个类,在屏幕上用视觉内容表示一个矩形。 在UIView上完成的每个绘图都会在CALayer上进行,这会导致视觉内容的绘制

    masksToBound set to NO masksToBound set to YES

    推荐文章