
TRANSPARENT LAYER INKIST CODE
The next six lines of code set fill colors and fill the three rectangles shown in Figure 9-3. From this point on, drawing occurs to this layer. Signals the start of the transparency layer. (A value of 0 specifies a hard edge shadow with no blur.) Sets the shadow, specifying a value of 10 as the blur value. This shadow is offset by 10 units in the horizontal direction and –20 units in the vertical direction. Sets up a CGSize data structure that contains the x and y offset values for the shadow. Takes three parameters-a graphics context and a width and height to use when constructing the rectangles. Void MyDrawTransparencyLayer (CGContext m圜ontext, // 1ĬGSize myShadowOffset = CGSizeMake (10, -20) // 2ĬGContextSetShadow (m圜ontext, myShadowOffset, 10) // 3ĬGContextBeginTransparencyLayer (m圜ontext, NULL) // 4ĬGContextSetRGBFillColor (m圜ontext, 0, 1, 0, 1) ĬGContextFillRect (m圜ontext, CGRectMake (wd/3+ 50,ht/2 ,wd/4,ht/4)) ĬGContextSetRGBFillColor (m圜ontext, 0, 0, 1, 1) ĬGContextFillRect (m圜ontext, CGRectMake (wd/3-50,ht/2-100,wd/4,ht/4)) ĬGContextSetRGBFillColor (m圜ontext, 1, 0, 0, 1) ĬGContextFillRect (m圜ontext, CGRectMake (wd/3,ht/2-50,wd/4,ht/4)) ĬGContextEndTransparencyLayer (m圜ontext) // 6 Listing 9-1 Painting to a transparency layer A detailed explanation for each numbered line of code follows the listing.
TRANSPARENT LAYER INKIST HOW TO
The function in Listing 9-1 shows how to use a transparency layer to generate the rectangles in Figure 9-3. Figure 9-3 Three rectangles painted to a transparency layer Quartz renders the shadow as if the rectangles are a single unit. The three rectangles in Figure 9-3 are painted to a transparency layer. Painting to a transparency layer requires three steps:Ĭall the function CGContextBeginTransparencyLayer.ĭraw the items you want to composite in the transparency layer.Ĭall 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. When you are finished drawing, you call the function CGContextEndTransparencyLayer. This backdrop is treated as a separate destination buffer from the context. Drawing operations in the specified context are drawn as a composite into a fully transparent backdrop. 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.Īfter you begin a transparency layer, you perform whatever drawing you want to appear in that layer. 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. You signal the start of a transparency layer by calling the function CGContextBeginTransparencyLayer, which takes as parameters a graphics context and a CFDictionary object. But because layers are always part of a stack, you can’t manipulate them independently. Quartz maintains a transparency layer stack for each context and transparency layers can be nested. Quartz transparency layers are similar to layers available in many popular graphics applications. Figure 9-2 Three circles painted as separate entities How Transparency Layers Work 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. Figure 9-1 Three circles as a composite in a transparency layer 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. The resulting composite is treated as a single object. A transparency layer consists of two or more objects that are combined to yield a composite graphic.
