当我们使用Cocoa的视图的时候,我们必须继承NSView或者UIView并且重载函数drawRect:来显示任何内容。但是CALayer实例可以直接使用,而无需继承子类。因为CALayer是一个键-值编码兼容的容器类,你可以在实例里面存储任意值,所以子类实例化完全可以避免。
1.1 给CALayer提供内容
  你可以通过以下任何一种方法指定CALayer实例的内容:
  (1)使用包含图片内容的CGImageRef来显式的设置图层的contents的属性。
  (2)指定一个委托,它提供或者重绘内容。
  (3)继承CALayer类重载显示的函数。
1.1.1 设置contents属性
  图层的图片内容可以通过指定contents属性的值为CGImageRef。当图层被创建的时候或者在任何其他时候,这个操作可以在其他实体上面完成(如表3所示)。 
  代码 1 设定layer的contents属性
  CALayer *theLayer;
  // create the layer and set the bounds and position
  theLayer=[CALayer layer];
  theLayer.position=CGPointMake(50.0f,50.0f);
  theLayer.bounds=CGRectMake(0.0f,0.0f,100.0f,100.0f);
  // set the contents property to a CGImageRef
  // specified by theImage (loaded elsewhere)
  theLayer.contents=theImage;
 
1.1.2 通过委托提供内容
  你可以绘制图层的内容,或更好的封装图层的内容图片,通过创建一个委托类实现下列方法之一:
  displayLayer:或drawLayer:inContext:
  
  实现委托重绘的方法并不意味会自动的触发图层使用实现的方法来重绘内容。而是你要显式的告诉一个图层实例来重新缓存内容,通过发送以下任何一个方法setNeedsDisplay或者setNeedsDisplayInRect:的消息,或者把图层的needsDisplayOnBoundsChange属性值设置为YES。
  
  通过委托实现方法displayLayer:可以根据特定的图层决定显示什么图片,还可以更加需要设置图层的contents属性值。下面的例子是“图层的坐标系”部分的,它实现displayerLayer:方法根据state的值设置theLayer的contents属性。子类不需要存储state的值,因为CALayer的实例是一个键-值编码容器。
  代码 2  委托方法displayLayer:的实现示例
  - (void)displayLayer:(CALayer *)theLayer
  {
      // check the value of the layer's state key
      if ([[theLayer valueForKey:@"state"] boolValue])
      {
          // display the yes image
          theLayer.contents=[someHelperObject loadStateYesImage];
      }
      else {
          // display the no image
          theLayer.contents=[someHelperObject loadStateNoImage];
      }
  }
 
  如果你必须重绘图层的内容,而不是通过加载图片,那你需要实现drawLayer:inContext:方法。通过委托可以决定哪些内容是需要的并使用CGContextRef来重绘内容。
  下面的例子是“指定图层的几何”部分内容,它实现了drawLayer:inContext:方法使用lineWidth键值来重绘一个路径(path),返回therLayer。
 
  代码 3  代理方法drawLayer:inContext:的实现示例
  - (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,
                          [[theLayer valueForKey:@"lineWidth"] floatValue]);
    CGContextStrokePath(theContext);
    // release the path
    CFRelease(thePath);
  }
 
1.1.3 通过子类提供图层的内容
  虽然通常情况不需要这样做,但是你仍可以继承CALayer直接重载重绘和显示方法。这个通常发生在你的图层需要定制行为而委托又无法满足需求的时候。
  子类可以重载CALayer的显示方法,设置图层的内容为适当的图片。下面的例子是“变换图层的几何”部分的内容,它提供了和“图层的坐标系”例子相同的功能。不同的是子类定义state为实例的属性,而不是根据CALayer的键-值编码容器获取。
 
  代码 4  CALayer display 方法的覆盖示例
  - (void)display
  {
    // check the value of the layer's state key
    if (self.state)
    {
        // display the yes image
        self.contents=[someHelperObject loadStateYesImage];
    }
    else {
        // display the no image
        self.contents=[someHelperObject loadStateNoImage];
    }
  }
 
  CALayer子类可以通过重载drawInContext:绘制图层的内容到一个图形上下文。下面的例子是“修改变换的数据结构”的内容,它和“指定图层的几何”里面实现委托的办法一样产生相同的图片内容。唯一的不同的是实现委托里面的lineWidth和lineColor现在是子类实例的属性。
  Listing 5  覆盖layer的drawInContext:方法示例
  - (void)drawInContext:(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,
                          self.lineWidth);
    CGContextSetStrokeColorWithColor(theContext,
                                     self.lineColor);
    CGContextStrokePath(theContext);
    CFRelease(thePath);
  }
  
  继承CALayer并且实现其中的重绘方法并不意味重绘会自动发生。你必须显式的促使实例重新缓存其内容,可以通过发送以下任何一个方法setNeedsDisplay或setNeedsDisplayInRect:的消息,亦或者设置图层的needsDisplaOnBoundsChange属性为YES。
 
1.2 修改图层内容的位置
  CALayer的属性contentsGravity允许你在图层的边界内容修改图层的contents图片的位置或者伸缩值。默认情况下,内容的图像完全填充层的边界,忽视自然的图像宽高比。
  使用contentsGravity位置常量,你可以指定图片位于图层任何一个边界,比如位于图层的角落,或者图层边界的中心。然而当你使用位置常量的时候,contentsCenter属性会被忽略。表1列举了位置常量和他们相应的位置。
   
  layer的contentsGravity属性的定位常量
  kCAGravityTopLeft
    Positions the content image in the top left corner of the layer.
  kCAGravityTop
    Positions the content image horizontally centered along the top edge of the layer.
  kCAGravityTopRight
    Positions the content image in the top right corner of the layer.
  kCAGravityLeft
    Positions the content image vertically centered on the left edge of the layer.
  kCAGravityCenter
    Positions the content image at the center of the layer.
  kCAGravityRight
    Positions the content image vertically centered on the right edge of the layer.
  kCAGravityBottomLeft
    Positions the content image in the bottom left corner of the layer.
  kCAGravityBottom
    Positions the content image centered along the bottom edge of the layer. 
  kCAGravityBottomRight
    Positions the content image in the top right corner of the layer.
 
  “图层的坐标系”标识了所支持的内容位置和他们相应的常量。
  图 1  layer的contentsGravity属性的定位常量
     
  通过设置contentsGravity属性为其他一个常量(如表2所示)。图层的内容图片可以被向上或者向下拉伸, 仅当使用其他任何一个调整大小的常量的时候,contentsCenter属性才会对内容图片起作用。
 
  表 2  Layer的 contentsGravity 属性的缩放常量
  kCAGravityResize
    Resize the content image to completely fill the layer bounds, potentially ignoring the natural aspect of the content. This is the default.
  kCAGravityResizeAspect
    Resize the content image to scale such that it is displayed as large as possible within the layer bounds, yet still retains its natural aspect.
  kCAGravityResizeAspectFill
    Resize the content image to scale such that it is displayed filling the layer bounds, yet retaining its natural aspect. This may cause the content to extend outside the layer bounds.
  “变换图层的几何”演示了如何使用调整大小的模式来调整一个正方形图像的大小让其适应图层的方形边界。
  图 2  Layer的 contentsGravity 属性的缩放常量
      
  注意:使用任何常量kCAGravityResize、kCAGravityResizeAspect和kCAGravityResizeAspectFill和表1中的重心位置常量无关。图层的内容将会填充整个边界,所以使用这些常量无法改变图层内容的位置。

转自梦维:http://www.dreamingwish.com/dream-2012/coreanimation-programming-guide-e-the-content-layer.html

CoreAnimation编程指南(五)图层内容相关推荐

  1. CoreAnimation编程指南(九)图层布局

    NSView提供了经典的"stuts and springs"模式,用于视图调整大小的时候把关联到它父图层的视图重新调整位置.图层支持该模式,而且Mac OS X上面的核心动画提供 ...

  2. CoreAnimation编程指南(四)图层树结构

    图层不但给自己提供可视化的内容和管理动画,而且充当了其他图层的容器类,构建图层层次结构.  本章介绍了图层层次结构,以及如何操纵该图层层次结构.   一.什么是图层树的层次结构 1.图层树是核心动画里 ...

  3. CoreAnimation编程指南(七)图层Action

    图层的行为在以下情况发生的时候被触发:从图层树里面插入或者删除一个图层,图层的属性值被修改了,或者程序显式要求.通常情况下,行为触发器是动画显示的结果所在.   1.1 行为对象的角色   一个行为对 ...

  4. CoreAnimation编程指南(简介)

    一.核心动画编程介绍    1.本文档介绍了在使用核心动画时所涉及的基本概念.核心动画的是Objective – C的框架,它通过简单的动画编程接口来提供一套高性能的动画引擎.    2.你应该阅读此 ...

  5. CoreAnimation编程指南(简介)转自:http://www.dreamingwish.com/

    核心动画编程介绍 本文档介绍了在使用核心动画时所涉及的基本概念.核心动画的是Objective – C的框架,它通过简单的动画编程接口来提供一套高性能的动画引擎. 你应该阅读此文档来理解Cocoa应用 ...

  6. CoreAnimation编程指南(六)动画 转自:http://www.dreamingwish.com/

    http://www.dreamingwish.com/dream-2012/coreanimation-programming-guide-f-animation.html 动画是当今用户界面的关键 ...

  7. CoreAnimation编程指南(六)动画

        动画是当今用户界面的关键因素.当使用核心动画的时候,动画是自动完成的.没有动画的循环和计数器.你的应用程序不负负责重绘,也不负责跟踪动画的当前状态.动画在独立线程里面自动执行,没有和你的应用程 ...

  8. CoreAnimation编程指南(一)概念

    一.概述 1.核心动画是一套包含图形绘制,投影,动画的Objective–C类集合.它通过开发人员所熟悉的应用程序套件和Cocoa Touch视图架构的抽象分层模式,同时使用先进的合作效果提供了一套流 ...

  9. iOS之有关动画的总结/CoreAnimation编程指南、简单动画实例

    facebook的动画框架pop:https://github.com/facebook/pop or 核心动画类有以下分类: 提供显示内容的图层类. 动画和计时类. 布局和约束类. 事务类,在原子更 ...

最新文章

  1. Week8 Teamework from Z.XML-Z.XML游戏功能说明
  2. STM32下SysTick的一个容易发生的错误,时钟频率设置
  3. Python成长笔记 - 基础篇 (六)python模块
  4. javasript 操作option select
  5. python监控网页更新_python监控网页更新
  6. map按kye排序 按value排序
  7. 伊万卡·特朗普的迈阿密豪华公寓楼接受加密付款
  8. 2阶魔方矩阵matlab,matlab魔方矩阵
  9. vivado下载地址和ISE下载地址
  10. python开发浏览器_Python制作简易浏览器
  11. 寻找春天 九宫格日记-2013.01.12
  12. 用python证明采样定理_这一切都从指数函数开始(4)——采样定理
  13. 计算机ps基础知识大全,全国计算机ps一级考试题型
  14. 与电影同行的日子(同步更新)
  15. C# 的1ms延时函数
  16. unity material之tiling和offset属性
  17. tableau app android,tableau 安卓
  18. 国二python是什么意思_国二都考什么啊
  19. VUE实现登录和登出
  20. win10连接win7共享打印机提示无法连接到打印机

热门文章

  1. Catalan数总结
  2. BZOJ 4819: [Sdoi2017]新生舞会
  3. HTML5中的时间类型,另外EL表达式的时间值来读取时间,并且还可以更改时间
  4. Java小对象的解决之道——对象池(Object Pool)的设计与应用
  5. UVA 10173 旋转卡壳
  6. ZeroMQ全面介绍
  7. 中文WAP浏览器WapDisplayer V1.5发布
  8. 数据库高级知识——mysql架构介绍(一)
  9. 算法竞赛入门经典 例题6-2 铁轨(C、python)
  10. 新视窗java_《计算机组成原理实验》教学大纲 - 兰州大学信息科学与工程学院.DOC...