路径构造

1 前言

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

2 代码实例

ZYViewControllerView.m

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

隐藏状态条

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


运行结果


3 结语

以上是所有内容,希望对大家有所帮助。

Demo实例下载:http://download.csdn.net/detail/u010013695/5374923

原文地址:https://www.cnblogs.com/yulang314/p/3720618.html