Quartz 2D 编程指南七:阴影

2017-06-12  本文已影响76人  bobociel

Shadows

A shadow is an image painted underneath, and offset from, a graphics object such that the shadow mimics the effect of a light source cast on the graphics object, as shown in Figure 7-1. Text can also be shadowed. Shadows can make an image appear three dimensional or as if it’s floating.

阴影是绘制在图像下面且有偏移的图形对象,阴影模拟在图形对象上投射的光源的效果,如图7-1所示。 文字也可以被遮蔽。 阴影可以使图像呈现出三维形状,或者像是浮动的。

Figure7-1 A shadowFigure7-1 A shadow

Shadows have three characteristics:

阴影有三个特点:

Figure7-2 Figure 7-2  A shadow with no blur and another with a soft edgeFigure7-2 Figure 7-2 A shadow with no blur and another with a soft edge

How Shadows Work(阴影如何工作)

Shadows in Quartz are part of the graphics state. You call the function CGContextSetShadow, passing a graphics context, offset values, and a blur value. After shadowing is set, any object you draw has a shadow drawn with a black color that has a 1/3 alpha value in the device RGB color space. In other words, the shadow is drawn using RGBA values set to {0, 0, 0, 1.0/3.0}.

Quartz的阴影是图形状态的一部分。您调用函数CGContextSetShadow,传递图形上下文,偏移值和模糊值。设置阴影后,您绘制的任何对象都将在设备RGB颜色空间中绘制一个黑色,具有1/3 alpha值的阴影。换句话说,使用设置为{0,0,0,1.0 / 3.0}的RGBA值绘制阴影。

You can draw colored shadows by calling the function CGContextSetShadowWithColor, passing a graphics context, offset values, a blur value, and a CGColor object. The values to supply for the color depend on the color space you want to draw in.

您可以通过调用函数CGContextSetShadowWithColor,传递图形上下文,偏移值,模糊值和CGColor对象来绘制彩色阴影。提供的颜色值取决于要绘制的颜色空间。

If you save the graphics state before you call CGContextSetShadow or CGContextSetShadowWithColor, you can turn off shadowing by restoring the graphics state. You also disable shadows by setting the shadow color to NULL.

如果在调用CGContextSetShadow或CGContextSetShadowWithColor之前保存图形状态,则可以通过恢复图形状态来关闭阴影。您还可以通过将阴影颜色设置为NULL来禁用阴影。

Shadow Drawing Conventions Vary Based on the Context(阴影绘制规则基于上下文而变化)

The offsets described earlier specify where the shadow is located related to the image that cast the shadow. Those offsets are interpreted by the context and used to calculate the shadow’s location:

前面描述的偏移指定阴影所在的位置与投射阴影的图像有关。 根据上下文解释可计算阴影的位置和偏移量:

Painting with Shadows(绘制阴影)

Follow these steps to paint with shadows:

绘制阴影的步骤:

Follow these steps to paint with colored shadows:

绘制彩色阴影的步骤:

The two rectangles in Figure 7-3 are drawn with shadows—one with a colored shadow.

Figure 7-3  A colored shadow and a gray shadowFigure 7-3 A colored shadow and a gray shadow

The function in Listing 7-1 shows how to set up shadows to draw the rectangles shown in Figure 7-3. A detailed explanation for each numbered line of code appears following the listing.

Listing 7-1 A function that sets up shadows

void MyDrawWithShadows (CGContextRef myContext, // 1
                         CGFloat wd, CGFloat ht);
{
    CGSize          myShadowOffset = CGSizeMake (-15,  20);// 2
    CGFloat           myColorValues[] = {1, 0, 0, .6};// 3
    CGColorRef      myColor;// 4
    CGColorSpaceRef myColorSpace;// 5
 
    CGContextSaveGState(myContext);// 6
 
    CGContextSetShadow (myContext, myShadowOffset, 5); // 7
    // Your drawing code here// 8
    CGContextSetRGBFillColor (myContext, 0, 1, 0, 1);
    CGContextFillRect (myContext, CGRectMake (wd/3 + 75, ht/2 , wd/4, ht/4));
 
    myColorSpace = CGColorSpaceCreateDeviceRGB ();// 9
    myColor = CGColorCreate (myColorSpace, myColorValues);// 10
    CGContextSetShadowWithColor (myContext, myShadowOffset, 5, myColor);// 11
    // Your drawing code here// 12
    CGContextSetRGBFillColor (myContext, 0, 0, 1, 1);
    CGContextFillRect (myContext, CGRectMake (wd/3-75,ht/2-100,wd/4,ht/4));
 
    CGColorRelease (myColor);// 13
    CGColorSpaceRelease (myColorSpace); // 14
 
    CGContextRestoreGState(myContext);// 15
}

Here’s what the code does:

上一篇 下一篇

猜你喜欢

热点阅读