欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

IOS开发(83)之为形状增加阴影

程序员文章站 2023-10-27 13:47:28
1 前言 使用 cgcontextsetshadow 过程,为绘制在图形环境上的形状应用阴影。 cgcontextsetshadowwithcolor 过程:这个过程接受的...

1 前言
使用 cgcontextsetshadow 过程,为绘制在图形环境上的形状应用阴影。


cgcontextsetshadowwithcolor 过程:这个过程接受的参数和 cgcontextsetshadow 完全相同,不过加了一个 cgcolorref 类型的参数,用于设 置阴影的颜色。


2 代码实例
zyviewcontrollerview.m

 

[plain]  -(void)drawrect:(cgrect)rect{ 
    [self drawrectattopofscreen]; 
    //由于第一个矩形画的时候已经有了阴影,所以就算第二个矩形绘图时候没有设置依然有阴影 
    [self drawrectatbottomofscreen]; 

 
- (void) drawrectattopofscreen{ 
    cgcontextref currentcontext = uigraphicsgetcurrentcontext(); 
    //用灰色设置背景颜色 
    /*第二个参数:阴影的位移,由 cgsize 类型值指定,从每个形状要应用阴影的右下部分开始。位移的 x 值越大,形状 
    右边的阴影就扩散得越远。位移的 y 值越大,下部的阴影就越低。 
     第三个参数:阴影的模糊值,以浮点值(cgfloat)来指定。指定 0.0f 将导致阴影成为固态形状。这个值越高,阴影就越 
     模糊。我们很快能看到例子。 
     */ 
    cgcontextsetshadowwithcolor(currentcontext,cgsizemake(10.0f, 10.0f), 20.0f,[[uicolor graycolor] cgcolor]); 
    /* create the path first. just the path handle. */ 
    cgmutablepathref path = cgpathcreatemutable(); 
    cgrect firstrect = cgrectmake(55.0f, 60.0f,150.0f, 150.0f); 
    cgpathaddrect(path,null, firstrect); 
    cgcontextaddpath(currentcontext, path); 
    [[uicolor colorwithred:0.20f green:0.60f blue:0.80f alpha:1.0f] setfill]; 
    cgcontextdrawpath(currentcontext, kcgpathfill); 
    cgpathrelease(path); 

- (void) drawrectatbottomofscreen{ 
    cgcontextref currentcontext = uigraphicsgetcurrentcontext(); 
    cgmutablepathref secondpath = cgpathcreatemutable(); 
    cgrect secondrect = cgrectmake(150.0f, 250.0f, 100.0f,100.0f); 
    cgpathaddrect(secondpath, null,secondrect); 
    cgcontextaddpath(currentcontext, secondpath); 
    [[uicolor purplecolor] setfill]; 
    cgcontextdrawpath(currentcontext, kcgpathfill); 
    cgpathrelease(secondpath); 

-(void)drawrect:(cgrect)rect{
    [self drawrectattopofscreen];
    //由于第一个矩形画的时候已经有了阴影,所以就算第二个矩形绘图时候没有设置依然有阴影
    [self drawrectatbottomofscreen];
}

- (void) drawrectattopofscreen{
    cgcontextref currentcontext = uigraphicsgetcurrentcontext();
    //用灰色设置背景颜色
    /*第二个参数:阴影的位移,由 cgsize 类型值指定,从每个形状要应用阴影的右下部分开始。位移的 x 值越大,形状
    右边的阴影就扩散得越远。位移的 y 值越大,下部的阴影就越低。
     第三个参数:阴影的模糊值,以浮点值(cgfloat)来指定。指定 0.0f 将导致阴影成为固态形状。这个值越高,阴影就越
     模糊。我们很快能看到例子。
     */
    cgcontextsetshadowwithcolor(currentcontext,cgsizemake(10.0f, 10.0f), 20.0f,[[uicolor graycolor] cgcolor]);
    /* create the path first. just the path handle. */
    cgmutablepathref path = cgpathcreatemutable();
    cgrect firstrect = cgrectmake(55.0f, 60.0f,150.0f, 150.0f);
    cgpathaddrect(path,null, firstrect);
    cgcontextaddpath(currentcontext, path);
    [[uicolor colorwithred:0.20f green:0.60f blue:0.80f alpha:1.0f] setfill];
    cgcontextdrawpath(currentcontext, kcgpathfill);
    cgpathrelease(path);
}
- (void) drawrectatbottomofscreen{
    cgcontextref currentcontext = uigraphicsgetcurrentcontext();
    cgmutablepathref secondpath = cgpathcreatemutable();
    cgrect secondrect = cgrectmake(150.0f, 250.0f, 100.0f,100.0f);
    cgpathaddrect(secondpath, null,secondrect);
    cgcontextaddpath(currentcontext, secondpath);
    [[uicolor purplecolor] setfill];
    cgcontextdrawpath(currentcontext, kcgpathfill);
    cgpathrelease(secondpath);
}