Can someone explain the CALayer contentsRect property's coordinate system to me?

空扰寡人 提交于 2019-12-19 03:11:49

问题


I realize that the contentsRect property of CALayer (documentation here) allows one to define how much of the layer to use for drawing, but I do not understand how the coordinate system works, I think.

It seems that when the width/height are smaller, the area used for content is bigger and vice versa. Similarly, negative x,y positions seem to move the content area down and to the right which is the opposite of my intuition.

Can someone explain why this is? I'm sure there is a good reason, but I assume I'm missing some graphics programming background.


回答1:


the contentsRect property of CALayer (documentation here) allows one to define how much of the layer to use for drawing

No, you're thinking about it incorrectly.

The contentsRect specifies which part of the contents image will be displayed in the layer.

That part is then arranged in the layer according to the contentsGravity property.

If this is kCAGravityResize, the default, this will cause the part to be resized to fit the layer. That would explain the counterintuitive behavior you're seeing -- you make contentsRect smaller, but the layer appears to be the same size, and it appears to "zoom in" on the selected part of the image. You might find it easier to understand if you set contentsGravity to kCAGravityCenter, which won't resize.

Most of the time, you would set the contentsRect to some sub-rect of the identity rect { {0, 0}, {1, 1} }, so you choose to see only part of the contents.

(Think of these as percentages if you like -- if contentsRect has a size of {0.5, 0.5}, you're choosing 50% of the contents.)

If part of the contentsRect goes outside the identity rect, then CA will extend the edge pixels of the contents outwards. This is handy in some cases, but it's not something you'd use on its own -- you'd use it in combination with a mask or with some other layers to achieve some effect.



来源:https://stackoverflow.com/questions/10117755/can-someone-explain-the-calayer-contentsrect-propertys-coordinate-system-to-me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!