Core Animaiton - 7

Working with High-Resolution Images

Layers do not have any inherent knowledge of the resolution of the underlying device’s screen. A layer simply stores a pointer to your bitmap and displays it in the best way possible given the available pixels. If you assign an image to a layer’s contents property, you must tell Core Animation about the image’s resolution by setting the layer’scontentsScale property to an appropriate value. The default value of the property is1.0, which is appropriate for images intended to be displayed on standard resolution screens. If your image is intended for a Retina display, set the value of this property to2.0.

因为图层并不具备底层设备屏幕的技术支持,只牵涉到位图并且将它以最好的方式用可以达到的像素展示出来,所以必须通过contentsScale这个属性来告诉Core Animation有关屏幕的分辨率。默认值为1.0,然而如果是Retina屏幕的时候,设置值应该为2.0,这个知识点在了解 切图常识@1X@2X@3X的时候已经了解了。

Changing the value of thecontentsScaleproperty is only necessary if you are assigning a bitmap to your layer directly. A layer-backed view in UIKit and AppKit automatically sets the scale factor of its layer to an appropriate value based on the screen resolution and the content managed by the view. For example, if you assign an aNSImage object to thecontents property of a layer in OS X, AppKit looks to see if there are both standard- and high-resolution variants of the image. If there are, AppKit uses the correct variant for the current resolution and sets the value of the contentsScale property to match.


Adjusting a Layer’s Visual Style and Appearance

Layer objects have built in visual adornments such as a border and background color that you can use to supplement the layer’s main contents. Because these visual adornments do not require any rendering on your part, they make it possible to use layers as standalone entities in some situations. All you have to do is set a property on the layer and the layer handles the necessary drawing, including any animations. For additional illustrations of how these visual adornments affect the appearance of a layer.

层提供了很多可以改变可视化的样式和外貌


Layers Have Their Own Background and Border

A layer can display a filled background and a stroked border in addition to its image-based contents. The background color is rendered behind the layer’s contents image and the border is rendered on top of that image, as shown in Figure 2-3. If the layer contains sublayers, they also appear underneath the border. Because the background color sits behind your image, that color shines through any transparent portions of your image.

一个图层对象可以展示一个填充满了的背景和一个被描边来增加基于图像的内容。背景颜色会被渲染在层的内容图像并且边框会被渲染在这个图像的最高部分,如果层对象拥有子层,它们也会出现在边框一下,因为背景颜色位于图像的后面,这个颜色会通过任意透明部分闪耀出来。


这里将backgroundColor 背景色, contents 内容本身, borderWidth和borderColor 边框宽度和颜色 分别属于三部分( 3 visual adornments ), 但是要记住哦,这都是属于层layer的属性哦。

myLayer.backgroundColor = [NSColor greenColor].CGColor;
myLayer.borderColor = [NSColor blackColor].CGColor;
myLayer.borderWidth = 3.0;

If you set your layer’s background color to an opaque color, consider setting the layer’s opaque property to YES. Doing so can improve performance when compositing the layer onscreen and eliminates the need for the layer’s backing store to manage an alpha channel. You must not mark a layer as opaque if it also has a nonzero corner radius, though.

这在以后的性能优化会提到,如果设置透明值,隐藏才是最好的。



接下来很常见的设置设置圆角的方法:

Layers Support a Corner Radius

You can create a rounded rectangle effect for your layer by adding a corner radius to it. A corner radius is a visual adornment that masks part of the corners of the layer’s bounds rectangle to allow the underlying content to show through, as shown in Figure 2-4. Because it involves applying a transparency mask, the corner radius does not affect the image in the layer’scontents property unless themasksToBounds property is set to YES. However, the corner radius always affects how the layer’s background color and border are drawn.


看上图才知道原来原理是如此的,在每一个角都设立一个圆形并且用这个圆形的半径来决定这个弧形的弯曲度。而方法masksToBounds : 设置masksToBounds为YES会把所有子层超出bounds范围的部分裁掉。

/* When true an implicit mask matching the layer bounds is applied to
 * the layer (including the effects of the `cornerRadius' property). If
 * both `mask' and `masksToBounds' are non-nil the two masks are
 * multiplied to get the actual mask values. Defaults to NO.
 * Animatable. */

@property BOOL masksToBounds;
这个方法的描述特别指出了对于cornerRadius属性的效果的作用。

Layers Support Built-In Shadows

The CALayer class includes several properties for configuring a shadow effect. A shadow adds depth to the layer by making it appear as if it is floating above its underlying content. This is another type of visual adornment that you might find useful in specific situations for your app. With layers, you can control the shadow’s color, placement relative to the layer’s content, opacity, and shape.

The opacity value for layer shadows is set to0 by default, which effectively hides the shadow.Changing the opacity to a nonzero value causes Core Animation to draw the shadow. Because shadows are positioned directly under the layer by default, you might also need to change the shadow’s offset before you can see it. It is important to remember, though, that the offsets you specify for the shadow are applied using the layer’s native coordinate system, which is different on iOS and OS X. Figure 2-5 shows a layer with a shadow that extends down and to the right of the layer. In iOS, this requires specifying a positive value for the y axis but in OS X the value needs to be negative.

阴影作为界面元素的一个重要的装饰样式,它可以将视图变的悬浮起来的感觉,注意的是设置它offset的值。


When adding shadows to a layer, the shadow is part of the layer’s content but actually extends outside the layer’s bounds rectangle. As a result, if you enable themasksToBounds property for the layer, the shadow effect is clipped around the edges. If your layer contains any transparent content, this can cause an odd effect where the portion of the shadow directly under your layer is still visible but the part extending beyond your layer is not. If you want a shadow but also want to use bounds masking, you use two layers instead of one. Apply the mask to the layer containing your content and then embed that layer inside a second layer of the exact same size that has the shadow effect enabled.

这里提到一个问题阴影是属于图层contents的,并且是属于超过bounds范围的,那么我们如果使用方法masksToBounds就一定会使得超出bounds的失效(被遮掩),那如果我们想要实现一个圆角阴影的图层,那么怎么办呢?  原来网上很多的方法都是由官方文档来提供的,就是使用两个图层来组合composition!


Adding Custom Properties to a Layer

The CAAnimation andCALayer classes extend the key-value coding conventions to support custom properties. You can use this behavior to add data to a layer and retrieve it using a custom key you define. You can even associate actions with your custom properties so that when you change the property, a corresponding animation is performed.

For information about how to set and get custom properties, seeKey-Value Coding Compliant Container Classes. For information about adding actions to your layer objects, see Changing a Layer’s Default Behavior.

CAAnimaiton 和 CALayer都是遵循KVO的编程模式的。


Printing the Contents of a Layer-Backed View

During printing, layers redraw their contents as needed to accommodate the printing environment. Whereas Core Animation normally relies on cached bitmaps when rendering to the screen, it redraws that content when printing. In particular, if a layer-backed view uses the drawRect: method to provide the layer contents, Core Animation callsdrawRect: again during printing to generate the printed layer contents.

这里的printing还真不知道是啥,然后找了一找官方文档:In iOS 4.2 and later, apps can add support for printing content to local AirPrint-capable printers。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值