ios 学习之你画我话绘图五 构造路径基础知识

来源:互联网 发布:微信程序源码 编辑:程序博客网 时间:2024/04/30 22:59

1 前言
构造和绘制路径,能够在图形环境上画任意形状.

2 代码实例
ZYViewControllerView.m

 

[plain]  - (void)drawRect:(CGRect)rect{ 
    //创建路径 创建一个新的 CGMutablePathRef 类型的可变路径并返回其句柄。 
    CGMutablePathRef path = CGPathCreateMutable(); 
    /* How big is our screen? We want the X to cover the whole screen */ 
    //范围为整个屏幕 
    CGRect screenBounds = [[UIScreen mainScreen] bounds]; 
    //从左上角开始画路径 将路径上当前画笔位置移动到 CGPoint 类型的参数指定的点。 
    CGPathMoveToPoint(path, NULL,screenBounds.origin.x, screenBounds.origin.y); 
    //从左上角连线到右下角 从画笔当前位置向指定位置绘制一条线段。 
    CGPathAddLineToPoint(path,NULL, screenBounds.size.width, screenBounds.size.height); 
    //开始另一点从右上角 
    CGPathMoveToPoint(path,NULL, screenBounds.size.width, screenBounds.origin.y); 
    //从右上角到左下角 
    CGPathAddLineToPoint(path,NULL, screenBounds.origin.x, screenBounds.size.height); 
    //获得当前图形的上下文 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 
    /* Add the path to the context so we candraw it later */ 
    //添加路径到路径上下文中 向图形环境上添加一个路径(由一个路径句柄指定),该路径已经准备好被绘制。 
    CGContextAddPath(currentContext,path); 
    //设置蓝色 
    [[UIColor blueColor] setStroke]; 
    //画图 在图形环境上绘制指定路径 
    /*kCGPathStroke 
    画线来标记路径的边界或边缘,使用选中的绘图色。 
    kCGPathFill 
    用选中的填充色,填充被路径包围的区域。 
    kCGPathFillStroke 
    组合绘图和填充。用当前填充色填充路径,并用当前绘图色绘制路径边界。下面我们会看到一个使用此方 法的例子。 
    */ 
    CGContextDrawPath(currentContext, kCGPathStroke); 
    //释放路径 
    CGPathRelease(path); 

- (void)drawRect:(CGRect)rect{
    //创建路径 创建一个新的 CGMutablePathRef 类型的可变路径并返回其句柄。
    CGMutablePathRef path = CGPathCreateMutable();
    /* How big is our screen? We want the X to cover the whole screen */
    //范围为整个屏幕
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    //从左上角开始画路径 将路径上当前画笔位置移动到 CGPoint 类型的参数指定的点。
    CGPathMoveToPoint(path, NULL,screenBounds.origin.x, screenBounds.origin.y);
    //从左上角连线到右下角 从画笔当前位置向指定位置绘制一条线段。
    CGPathAddLineToPoint(path,NULL, screenBounds.size.width, screenBounds.size.height);
    //开始另一点从右上角
    CGPathMoveToPoint(path,NULL, screenBounds.size.width, screenBounds.origin.y);
    //从右上角到左下角
    CGPathAddLineToPoint(path,NULL, screenBounds.origin.x, screenBounds.size.height);
    //获得当前图形的上下文
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    /* Add the path to the context so we candraw it later */
    //添加路径到路径上下文中 向图形环境上添加一个路径(由一个路径句柄指定),该路径已经准备好被绘制。
    CGContextAddPath(currentContext,path);
    //设置蓝色
    [[UIColor blueColor] setStroke];
    //画图 在图形环境上绘制指定路径
    /*kCGPathStroke
    画线来标记路径的边界或边缘,使用选中的绘图色。
    kCGPathFill
    用选中的填充色,填充被路径包围的区域。
    kCGPathFillStroke
    组合绘图和填充。用当前填充色填充路径,并用当前绘图色绘制路径边界。下面我们会看到一个使用此方 法的例子。
    */
    CGContextDrawPath(currentContext, kCGPathStroke);
    //释放路径
    CGPathRelease(path);
}
隐藏状态条

此例中,我要隐藏程序的状态条:请找到 Info.plist,并增加一个 UIStatusBarHidden 键,将其值设置为 YES


原创粉丝点击