Core Animation Basics

It achieves this behavior by caching the contents of views into bitmaps that can be manipulated directly by the graphics hardware

它把UIView 的内容转化为位图,直接在图形硬件上操作。

Layer objects  是在3D空间的2D表现。使用Core Animation 进行操作的重点. 

层管理的信息有几何,内容,展现出来的视觉属性。跟UIView 不一样的是,层不定义自己的外表(appearance)。层只是管理周边位图信息的状态。

The bitmap itself can be the result of a view drawing itself or a fixed image that you specify. For this reason, the main layers you use in your app are considered to be model objects because they primarily manage data

层  被认为是模型对象。主要管理数据

多数层不进行实际的绘制。实际上层收集APP提供的内容并且缓存层位图(backing store)。当你改变一个层的属性 , 实际上你是修改的与层有关的信息状态。当一个改变产生了动画,Core Animation 就好传递层的位图和 信息状态到图形硬件,主要工作就是用新的信息去渲染位图。简言之,硬件比软件快。

因为它操作的是静态位图,layer-based drawing 不同于传统的 view-based drawing . view-based drawing 会调用drawRect: 方法用新的方法来重回内容。但是这种操作是很耗费资源的,因为他占用主线程CPU。虽然Core Animation 尽可能的缓存内容,但是你的app 仍然需要提供原始的内容和一遍一遍的更新。

提供层对象,你可以提供位图内容的方法

1.分配一个图片对象直接付给层对象的内容属性。(This technique is best for layer content that never, or rarely, changes.)

因为一个层就是管理位图图片的容器,所以你可以这么操作。这层直接使用你提供的图片而不必再去copy一份。这有利于节省内存。但你的APP 中在多个地方使用同一个图片就会节省内存。你分配的图片必须是CGImageRef 类型。用UIImage也行,不过你得提供与设备分辨相应的图片。

2.分配一个代理对象给层(layer)并且让代理来绘制层的内容。(最佳适用情况是层的内容可能会周期性变化或者是被其他额外的对象提供的 比如UIView)

.At display time, the layer calls the methods of your delegate to provide the needed content: 在显示的时候,层会调用代理的方法添加内容。

如果你的代理实现了displayLayer: method 。这个方法负责创建一个位图并且分配它到位图的内容属性

如果你的代理实现了drawLayer:inContext:方法,Core Animation 创建一个位图,创建一个图形上下文来绘制位图,然后调用你的代理方法 填充位图。

All your delegate method has to do is draw into the provided graphics context.

If the delegate implements both the displayLayer: and drawLayer:inContext: method, the layer calls only the displayLayer: method.

在以下代码示例中 displayYesImage是个自定义的属性

- (void)displayLayer:(CALayer *)theLayer {


    // Check the value of some state property


    if (self.displayYesImage) {


        // Display the Yes image


        theLayer.contents = [someHelperObject loadStateYesImage];


    }


    else {


        // Display the No image


        theLayer.contents = [someHelperObject loadStateNoImage];


    }


}

Drawing the contents of a layer

- (void)drawLayer:(CALayer *)theLayer inContext:(CGContextRef)theContext {


    CGMutablePathRef thePath = CGPathCreateMutable();


 


    CGPathMoveToPoint(thePath,NULL,15.0f,15.f);


    CGPathAddCurveToPoint(thePath,


                          NULL,


                          15.f,250.0f,


                          295.0f,250.0f,


                          295.0f,15.0f);


 


    CGContextBeginPath(theContext);


    CGContextAddPath(theContext, thePath);


 


    CGContextSetLineWidth(theContext, 5);


    CGContextStrokePath(theContext);


 


    // Release the path


    CFRelease(thePath);


}


3.定义一个层的子类并且重写他的绘制方法来自己填充内容

(This technique is appropriate if you have to create a custom layer subclass anyway or if you want to change the fundamental drawing behavior of the layer.)

唯一你需要考虑的就是为自己创建的层提供内容时。If your app contains nothing but layer-backed views,你不用担心。

  调整内容。你提供的图片对层来说或大或小

使用层的contentsGravity 属性确保你的内容尽可能以好的方式展示

分为两类

* 这 position-based  (pin)不缩放图片

* 这 scaling-based (stretch) 有不同的选项

Layer Style Property Animations

几何属性

层的几何属性确定相对于父层,他是怎么展示的。

bounds position frame anchorPoint cornerRadius transform zPosition 

Background Properties

Core Animation 首先渲染的就是层的背景。你可以确定一个背景色。

有圆角的层不会自动修剪他们的内容;然而,设定masksToBounds 属性YES 就会修剪他的圆角


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值