简体   繁体   English

如何在CALayer中定位“ contents” png?

[英]How do I position `contents` png in CALayer?

Do I have to move the layer frame or apply translate matrix transformation to layer? 我是否必须移动图层框架或对图层应用转换矩阵变换? Or perhaps I can move the contents inside of the layer? 或者也许我可以将contents移动到图层内部? If contents is not movable inside of layer, how it would position initially? 如果内容在图层内部不可移动,则最初将如何定位?

A CALayer has a frame (or, equivalently, a bounds and an origin), which is used logically to determine what to draw. CALayer具有框架(或等效地,边界和原点),该框架在逻辑上用于确定要绘制的内容。 When drawInContext: or equivalent is called, it's the frame that determines how the contents are produced. 当调用drawInContext:或等效方法时,框架决定了内容的生成方式。

However, like OS X, iOS adopts a compositing window manager, which means that views know how to draw their output to a buffer and the buffers are combined to create the view, with the window manager figuring out what to do about caching and video memory management in between. 但是,像OS X一样,iOS采用了复合窗口管理器,这意味着视图知道如何将其输出绘制到缓冲区中,并且将缓冲区合并以创建视图,而窗口管理器会确定如何处理缓存和视频内存管理之间。

If you adjust the transform property of the view or of the layer class, then you adjust how the compositing happens. 如果调整视图或图层类的transform属性,则调整合成的方式。 However, the results of drawInContext: should explicitly still be the same so the window manager knows it can just use the cached image. 但是,drawInContext:的结果应该仍然明确相同,以便窗口管理器知道它可以只使用缓存的图像。

So, for example, if you set a frame of size 128x128 and then a transform that scales the CALayer up to double, you'll occupy a 256x256 area of the screen but the image used for compositing will be only 128x128 in size, making each source pixel into four target pixels. 因此,例如,如果您设置了一个尺寸为128x128的帧,然后进行了将CALayer缩放为两倍的转换,则您将占据屏幕的256x256区域,但是用于合成的图像大小仅为128x128源像素分成四个目标像素。 If you set a frame of size 256x256 and the identity transform, you'll cover the same amount of screen space but with each source pixel being 1:1 related to a target pixel. 如果设置尺寸为256x256的帧并进行身份转换,则将覆盖相同数量的屏幕空间,但每个源像素与目标像素的比例为1:1。

A side effect is that changing the frame causes a redraw from first principles. 副作用是,更改框架会导致重蹈覆辙。 Changing the transform doesn't. 更改变换不会。 So the latter is usually faster, and is also the thing to do if you decide to use something like CATiledLayer (as used in Safari, Maps, etc) that draws in a separate thread and may take a while to come up with results. 因此,后者通常更快,如果您决定使用CATiledLayer(在Safari,Maps等中使用)之类的东西来绘制单独的线程,并且可能需要一段时间才能得出结果,这也是要做的事情。

As a rule of thumb, you use the frame to set the initial position and update the frame for normal work stuff. 根据经验,您可以使用框架设置初始位置并更新框架以进行常规工作。 You play with the transform for transitions and other special effects. 您可以使用变换进行过渡和其他特殊效果。 However, all of the frame and transform properties of a CATiledLayer are animatable in the CoreAnimation sense, so that's really still at your discretion. 但是,从CoreAnimation的角度来看,CATiledLayer的所有frame和transform属性都是可动画的,因此实际上仍由您决定。

Most people don't work on the level of a CALayer, but prefer to work with UIViews. 大多数人不使用CALayer级别,但更喜欢使用UIViews。 In which case the comments are mostly the same, with the caveat that you can then adjust the [2d] transform on the view or the [3d] transform on the view's layer and have the compositor figure it all out, but change the frame to prompt a redraw. 在这种情况下,注释基本相同,但需要注意的是,您可以调整视图的[2d]变换或视图层的[3d]变换,让合成器将其全部弄清楚,但将框架更改为提示重画。

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

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