CoreAnimation编程指南(五)图层内容

来源:互联网 发布:edraw max mac破解方法 编辑:程序博客网 时间:2024/05/15 13:00
    当我们使用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属性的定位常量
     CoreAnimation编程指南(五)图层内容 - 米安格 - 米安格的博客
 
  通过设置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 属性的缩放常量
      CoreAnimation编程指南(五)图层内容 - 米安格 - 米安格的博客
 
  注意:使用任何常量kCAGravityResize、kCAGravityResizeAspect和kCAGravityResizeAspectFill和表1中的重心位置常量无关。图层的内容将会填充整个边界,所以使用这些常量无法改变图层内容的位置。

转自梦维:http://www.dreamingwish.com/dream-2012/coreanimation-programming-guide-e-the-content-layer.html
0 0
原创粉丝点击