简体   繁体   English

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

[英]How is the relation between UIView's clipsToBounds and CALayer's masksToBounds?

A UIView has a CALayer . UIView有一个CALayer That's pretty sure. 这很确定。 But both seem to provide something that means the same thing. 但两者似乎都提供了同样的东西。

If I'd set clipsToBounds=YES , would this also set the layer's masksToBounds=YES ? 如果我设置clipsToBounds=YES ,这还会设置图层的masksToBounds=YES吗? Why different names? 为什么不同名字? Anyone knows? 有谁知道?

They are different names because UIView and CALayer are different and have different terminology associated with them, but they are functionally equivalent. 它们是不同的名称,因为UIViewCALayer是不同的,并且具有与它们相关联的不同术语,但它们在功能上是等同的。 If you disassemble clipsToBounds you will see it just calls masksToBounds (disassembly from the simulator framework, so x86): 如果您反汇编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

Some one asked what will happen if clipToBounds is set to false and maskToBounds is set to true or it can be better if someone add screenshots 有人问到如果clipToBounds设置为false并且maskToBounds设置为true会发生什么,或者如果有人添加屏幕截图可能会更好

I stumbled upon the same issue where I wanted to set corner radius and shadow both to the view. 我偶然发现了同样的问题,我想在视图中设置角半径和阴影。

but it is not possible because as soon as I set masksToBound to YES it removes the shadow but clip the content and if I set it to NO it shows the shadow but does not clip the content. 但这是不可能的,因为只要我将masksToBound设置为YES就会删除阴影但剪切内容,如果我将其设置为NO则显示阴影但不剪切内容。

I have made the project which explains it better Understanding the ClipToBound And MasksToBound 我已经完成了解释它的项目了解ClipToBound和MasksToBound

I have come to the conclusion that CALayer is not a container but it is simply a class representing a rectangle on the screen with visual content. 我得出结论,CALayer不是一个容器,但它只是一个代表屏幕上带有可视内容的矩形的类。 every drawing done on UIView goes on CALayer which causes the visual content to draw 在UIView上完成的每个绘图都在CALayer上进行,这会导致可视内容的绘制

masksToBound设置为NO masksToBound设置为YES

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM