Quartz 2D 编程指南九:透明层

Transparency Layers

A transparency layer consists of two or more objects that are combined to yield a composite graphic. The resulting composite is treated as a single object. Transparency layers are useful when you want to apply an effect to a group of objects, such as the shadow applied to the three circles in Figure 9-1.
透明层由两个或多个被组合以产生复合图形的对象组成。 所得到的复合体被视为单个对象。 当您想对一组对象应用效果(例如,应用于图9-1中的三个圆圈的阴影)时,透明度图层很有用。

Quartz 2D 编程指南九:透明层_第1张图片
Figure 9-1 Three circles as a composite in a transparency layer

If you apply a shadow to the three circles in Figure 9-1 without first rendering them to a transparency layer, you get the result shown in Figure 9-2.
如果将图9-1中的三个圆圈应用阴影,而不先将其渲染到透明层,则得到如图9-2所示的结果。

Quartz 2D 编程指南九:透明层_第2张图片
Figure 9-2 Three circles painted as separate entities

How Transparency Layers Work(透明层如何工作)

Quartz transparency layers are similar to layers available in many popular graphics applications. Layers are independent entities. Quartz maintains a transparency layer stack for each context and transparency layers can be nested. But because layers are always part of a stack, you can’t manipulate them independently.

Quartz透明层与程序中常见的层是一样的。 层是独立实体。 Quartz维护每个上下文的透明层堆栈,透明层可以嵌套。 但是因为层总是堆栈的一部分,您不能独立地操作它们。

You signal the start of a transparency layer by calling the function CGContextBeginTransparencyLayer, which takes as parameters a graphics context and a CFDictionary object. The dictionary lets you provide options to specify additional information about the layer, but because the dictionary is not yet used by the Quartz 2D API, you pass NULL. After this call, graphics state parameters remain unchanged except for alpha (which is set to 1), shadow (which is turned off), blend mode (which is set to normal), and other parameters that affect the final composite.

您通过调用函数CGContextBeginTransparencyLayer来表示透明层的开始,CGContextBeginTransparencyLayer将图形上下文和CFDictionary对象作为参数。 该字典允许您提供选项以指定有关图层的其他信息,但由于Quartz 2D API尚未使用字典,因此您将传递NULL。 在此调用之后,图形状态参数保持不变,除了alpha(其设置为1),阴影(关闭),混合模式(设置为正常)以及影响最终复合的其他参数。

After you begin a transparency layer, you perform whatever drawing you want to appear in that layer. Drawing operations in the specified context are drawn as a composite into a fully transparent backdrop. This backdrop is treated as a separate destination buffer from the context.

开始一个透明层之后,你可以执行你想在该图层中显示的任何图形。指定上下文中的绘图操作将作为一个复合形式绘制成完全透明的背景。该背景幕被视为上下文中的单独的目标缓冲区。

When you are finished drawing, you call the function CGContextEndTransparencyLayer. Quartz composites the result into the context using the global alpha value and shadow state of the context and respecting the clipping area of the context.

完成绘制后,可以调用函数CGContextEndTransparencyLayer。 Quartz使用全局alpha值和上下文的阴影状态将结果复合到上下文中,并且遵循上下文的剪切区域。

Painting to a Transparency Layer(绘制透明层)

Painting to a transparency layer requires three steps:

  • Call the function CGContextBeginTransparencyLayer.
  • Draw the items you want to composite in the transparency layer.
  • Call the function CGContextEndTransparencyLayer.

绘画到透明层需要三个步骤:

  • 调用函数CGContextBeginTransparencyLayer。
  • 在透明层中绘制要复合的项目。
  • 调用函数CGContextEndTransparencyLayer。

The three rectangles in Figure 9-3 are painted to a transparency layer. Quartz renders the shadow as if the rectangles are a single unit.
图9-3中的三个矩形被绘制到透明层。 Quartz将三个矩形作为一个整体来渲染阴影。

Quartz 2D 编程指南九:透明层_第3张图片
Figure 9-3 Three rectangles painted to a transparency layer

The function in Listing 9-1 shows how to use a transparency layer to generate the rectangles in Figure 9-3. A detailed explanation for each numbered line of code follows the listing.
清单9-1中的函数显示了如何使用透明层生成图9-3中的矩形。 并列出每个编号代码行的详细说明。

Listing 9-1 Painting to a transparency layer

void MyDrawTransparencyLayer (CGContext myContext, // 1
                               CGFloat wd,
                               CGFloat ht)
{
   CGSize          myShadowOffset = CGSizeMake (10, -20);// 2

   CGContextSetShadow (myContext, myShadowOffset, 10);   // 3
   CGContextBeginTransparencyLayer (myContext, NULL);// 4
   // Your drawing code here// 5
   CGContextSetRGBFillColor (myContext, 0, 1, 0, 1);
   CGContextFillRect (myContext, CGRectMake (wd/3+ 50,ht/2 ,wd/4,ht/4));
   CGContextSetRGBFillColor (myContext, 0, 0, 1, 1);
   CGContextFillRect (myContext, CGRectMake (wd/3-50,ht/2-100,wd/4,ht/4));
   CGContextSetRGBFillColor (myContext, 1, 0, 0, 1);
   CGContextFillRect (myContext, CGRectMake (wd/3,ht/2-50,wd/4,ht/4));
   CGContextEndTransparencyLayer (myContext);// 6
}

Here’s what the code does:

  • Takes three parameters—a graphics context and a width and height to use when constructing the rectangles.
  • Sets up a CGSize data structure that contains the x and y offset values for the shadow. This shadow is offset by 10 units in the horizontal direction and –20 units in the vertical direction.
  • Sets the shadow, specifying a value of 10 as the blur value. (A value of 0 specifies a hard edge shadow with no blur.)
  • Signals the start of the transparency layer. From this point on, drawing occurs to this layer.
  • The next six lines of code set fill colors and fill the three rectangles shown in Figure 9-3. You replace these lines with your own drawing code.
  • Signals the end of the transparency layer and signals that Quartz should composite the result into the context.

代码说明:

  • 获取三个参数 - 构建矩形时使用的图形上下文和宽度和高度。
  • 设置包含阴影的x和y偏移值的CGSize数据结构。 该阴影在水平方向上偏移了10个单位,在垂直方向上偏移了-20个单位。
  • 设置阴影,指定值为10作为模糊值。 (值为0表示没有模糊的硬边影)
  • 表示透明层的开始。 从这一点起,绘图发生将在这一层。
  • 接下来的六行代码集填充颜色并填充图9-3所示的三个矩形。 您用自己的绘制代码替换这些行。
  • 表示透明层的结束,并表示Quartz应将结果复合到上下文中。

你可能感兴趣的:(Quartz 2D 编程指南九:透明层)