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

IOS开发(82)之绘制矩形

程序员文章站 2023-10-27 13:34:40
1 前言 用 cgpathaddrect 向路径中添加一个矩形,然后在图形环境上绘制这条路径。 2 代码实例 zyviewcontrollerview.m  ...

1 前言
用 cgpathaddrect 向路径中添加一个矩形,然后在图形环境上绘制这条路径。


2 代码实例
zyviewcontrollerview.m

 

[plain]  - (void)drawrect:(cgrect)rect{ 
    //创建图形路径句柄 
    cgmutablepathref path = cgpathcreatemutable(); 
    //设置矩形的边界 
    cgrect rectangle = cgrectmake(10.0f, 10.0f,200.0f, 300.0f); 
    //添加矩形到路径中 
    cgpathaddrect(path,null, rectangle); 
    //获得上下文句柄 
    cgcontextref currentcontext = uigraphicsgetcurrentcontext(); 
    //添加路径到上下文中 
    cgcontextaddpath(currentcontext, path); 
    //填充颜色 
    [[uicolor colorwithred:0.20f green:0.60f blue:0.80f alpha:1.0f] setfill]; 
    //设置画笔颜色 
    [[uicolor browncolor] setstroke]; 
    //设置边框线条宽度 
    cgcontextsetlinewidth(currentcontext,5.0f); 
    //画图 
    cgcontextdrawpath(currentcontext, kcgpathfillstroke); 
    /* 释放路径 */ 
    cgpathrelease(path);