http://donbe.blog.163.com/blog/static/138048021201052093633776/ 详解

代码如下:

DJView 绘制线段 基本图形

 //
// DJView.m
// 基本图形绘制
//
// Created by zjj on 15/6/27.
// Copyright (c) 2015年 zjj. All rights reserved.
// #import "DJView.h" @implementation DJView /**
* 绘制两个三角形
*/
- (void)drawRect:(CGRect)rect {
//1获取上下文
CGContextRef ctr = UIGraphicsGetCurrentContext();
//2拼接一个图形
// 2.0设置A起点
// 设置颜色
CGContextSetRGBStrokeColor(ctr, , , , );
CGContextMoveToPoint(ctr, , );
// 2.1设置A终点
CGContextAddLineToPoint(ctr, , );
CGContextAddLineToPoint(ctr, , );
// 3渲染到屏幕上(空心)
CGContextStrokePath(ctr); CGContextMoveToPoint(ctr, , );
// 设置颜色
CGContextSetRGBStrokeColor(ctr, , , , );
CGContextAddLineToPoint(ctr, , );
CGContextAddLineToPoint(ctr, , );
// 2.2设置B终点
CGContextAddLineToPoint(ctr, , );
// 2.3设置C终点
CGContextAddLineToPoint(ctr, , );
//链接终点
// CGContextClosePath(ctr);
// 3渲染到屏幕上(实心)
// CGContextFillPath(ctr);
// 3渲染到屏幕上(空心)
CGContextStrokePath(ctr);
[self hourglass];
[self juxing];
}
/**
* 绘空心矩形
*/
- (void)juxing
{
CGContextRef ctr = UIGraphicsGetCurrentContext();
// 设置线段宽度
CGContextSetLineWidth(ctr, );
// 设置颜色
// CGContextSetRGBStrokeColor(ctr, 0, 0, 0, 1);
// [[UIColor blackColor]setStroke];//(推荐设置颜色方式)
[[UIColor blackColor]set];//实心空心通用 setStroke设置空心颜色 setFill 设置实心颜色
// 设置线段头尾部样式
CGContextSetLineCap(ctr, kCGLineCapRound);
// 设置线段转折点样式
CGContextSetLineJoin(ctr, kCGLineJoinRound);
CGContextAddRect(ctr, CGRectMake(, , , ));
CGContextStrokePath(ctr);
}
- (void)hourglass
{
//1获取上下文
CGContextRef ctr = UIGraphicsGetCurrentContext();
//2拼接一个图形
// 2.0设置A起点
// 设置颜色
// CGContextSetRGBStrokeColor(ctr, 0, 1, 1, 1);
// [[UIColor blackColor] setFill];
CGContextMoveToPoint(ctr, , );
// 2.1设置A终点
CGContextAddLineToPoint(ctr, , );
CGContextAddLineToPoint(ctr, , );
CGContextAddLineToPoint(ctr, , );
CGContextAddLineToPoint(ctr, , );
// 2.2设置B终点
CGContextAddLineToPoint(ctr, , ); //链接终点
CGContextClosePath(ctr);
// 3渲染到屏幕上(空心)
// CGContextStrokePath(ctr);
// 3渲染到屏幕上(实心)
CGContextFillPath(ctr);
}
@end
DJCircle 代码 绘制几何图形
 //
// DJCircle.m
// 基本图形绘制
//
// Created by zjj on 15/6/27.
// Copyright (c) 2015年 zjj. All rights reserved.
// #import "DJCircle.h" @implementation DJCircle // Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
[self drawArc];
[self drawCircle];
}
// 画圆弧
- (void)drawArc
{
CGContextRef crf = UIGraphicsGetCurrentContext();//获得上下文
CGContextAddArc(crf,, , , , M_PI, );
[[UIColor redColor] set];//设置颜色
CGContextFillPath(crf);// 渲染 现实所绘制的东西 CGContextRef crf1 = UIGraphicsGetCurrentContext();//获得上下文
CGContextAddArc(crf1,, , , , M_PI, );
CGContextFillPath(crf1);// 渲染 现实所绘制的东西
//风车
// 上面圆弧
CGContextRef crf2 = UIGraphicsGetCurrentContext();//获得上下文
CGContextAddArc(crf2,, , , -M_PI_2, M_PI_2, );
CGContextFillPath(crf2);// 渲染 现实所绘制的东西
//右边圆弧
CGContextRef crf3 = UIGraphicsGetCurrentContext();//获得上下文
CGContextAddArc(crf3,, , , , M_PI, );
CGContextFillPath(crf3);// 渲染 现实所绘制的东西
//下边圆弧
CGContextRef crf4 = UIGraphicsGetCurrentContext();//获得上下文
CGContextAddArc(crf4,, , , -M_PI_2, M_PI_2, );
CGContextFillPath(crf4);// 渲染 现实所绘制的东西
//左边圆弧
CGContextRef crf5 = UIGraphicsGetCurrentContext();//获得上下文
CGContextAddArc(crf5,, , , , M_PI, );
CGContextFillPath(crf5);// 渲染 现实所绘制的东西
//3/4圆弧
CGContextRef crf6 = UIGraphicsGetCurrentContext();//获得上下文
CGContextAddArc(crf6,, , , M_PI_2, M_PI, );
CGContextFillPath(crf6);// 渲染 现实所绘制的东西
//1/4左边圆弧
CGContextRef crf7 = UIGraphicsGetCurrentContext();//获得上下文
CGContextAddArc(crf7,, , , M_PI, M_PI_2, );
CGContextFillPath(crf7);// 渲染 现实所绘制的东西 //25%圆
CGContextRef crf8 = UIGraphicsGetCurrentContext();//获得上下文
CGContextMoveToPoint(crf8, , );
CGContextAddLineToPoint(crf8, , );
CGContextAddArc(crf8,, , , M_PI_2, M_PI, );
CGContextClosePath(crf8);
CGContextFillPath(crf8);// 渲染 现实所绘制的东西
} - (void)drawCircle
{
//画实心圆
CGContextRef crf = UIGraphicsGetCurrentContext();//获得上下文
CGContextAddEllipseInRect(crf,CGRectMake(, , , ));//画圆
[[UIColor blueColor] set];//设置颜色
CGContextFillPath(crf);// 渲染 现实所绘制的东西
//画圆圈/圆环
CGContextRef crf1 = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(crf1,CGRectMake(, , , ));
CGContextSetLineWidth(crf1, );
CGContextStrokePath(crf1);
//画同心圆圈/圆环
CGContextRef crf2 = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(crf2,CGRectMake(, , , ));
CGContextSetLineWidth(crf2, );
CGContextStrokePath(crf2);
} @end
DJTextImageView 绘制图片和文字
 //
// DJTextImageView.m
// 基本图形绘制
//
// Created by zjj on 15/6/28.
// Copyright (c) 2015年 zjj. All rights reserved.
// #import "DJTextImageView.h" @implementation DJTextImageView
/**
* 涂鸦 withAttributes: 字典设置用什么属性 如颜色字体大小
*/
- (void)drawRect:(CGRect)rect
{
[self drawImg];
[self drawTest]; }
- (void)drawImg
{ UIImage *img = [UIImage imageNamed:@"格子1.jpg"];
// [img drawAtPoint:CGPointZero];
// [img drawInRect:CGRectMake(110, 10, 100, 100)];
[img drawAsPatternInRect:CGRectMake(, , , )];//平铺图片
NSString *strShuiyin = @"水印http://www.DJweibo.com/168";
UIImage *pngs = [UIImage imageNamed:@"格子.jpg"];
NSMutableDictionary *arrs = [NSMutableDictionary dictionary];
arrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
[strShuiyin drawInRect:CGRectMake(, , , ) withAttributes:arrs];
[pngs drawInRect:CGRectMake(, , , )]; } /**
* 画文字 withAttributes: 字典设置用什么属性 如颜色字体大小
*/
- (void)drawTest
{
CGRect rects = CGRectMake(, , , );
// 画一个矩形框
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextAddRect(ctx, rects);
// CGContextFillPath(ctx);
CGContextStrokePath(ctx);
//画文字
NSMutableDictionary *arrs = [NSMutableDictionary dictionary];
arrs[NSForegroundColorAttributeName] = [UIColor redColor];
arrs[NSFontAttributeName] = [UIFont systemFontOfSize:]; NSString *str = @"土地是以它的肥沃和收获而被估价的;才能也是土地,不过它生产的不是粮食,而是真理。如果只能滋生瞑想和幻想的话,即使再大的才能也只是砂地或盐池,那上面连小草也长不出来的。 —— 别林斯基";
//[str drawAtPoint:CGPointZero withAttributes:nil];
[str drawInRect:rects withAttributes:arrs];
}
@end

最新文章

  1. MySQL Cluster 7.3.5 集群配置参数优化(优化篇)
  2. 基于Bootstrap的JQuery TreeView树形控件,数据支持json字符串、list集合(MVC5)<二>
  3. ADF_ADF Faces系列4_ADF数据可视化组件简介之建立BarChart/Gauge/ExportExcel
  4. MySQL中进行树状所有子节点的查询
  5. 《shell下sort排序命令的使用》
  6. Object-C 类实现
  7. dtree基础
  8. 【web开发--js学习】functionName 如果是一个属性值,函数将不会被调用
  9. 论山寨手机与Android联姻 【7】 MTK手机软件系统
  10. yum执行时Another app is currently holding the yum lock; waiting for it to exit... The other application is: yum
  11. [译] 关于 Angular 依赖注入你需要知道的
  12. 爬虫 需要什么样的 CPU,内存 和带宽
  13. localstorage和vue结合使用2
  14. 1-51单片机ESP8266学习-AT指令(开发板介绍)
  15. Kotlin 资料
  16. 从一个例子学习 instanceof 和 getclass 的区别
  17. memcached 细究(一)
  18. 网络流—最大流(Edmond-Karp算法)
  19. 防止XSS攻击的方式
  20. linux自学(六)之开始centos学习,更换yum源

热门文章

  1. hdu 5339 Untitled
  2. POJ 2418 各种二叉排序树
  3. golang操作文件的四种方法
  4. 使用js加载器动态加载外部Javascript文件
  5. 转:C++语言的15个晦涩特性
  6. java实现数据库连接池
  7. 转!!java反射机制
  8. grep使用方法
  9. MySQL5.7 JSON实现简介
  10. linux笔记:linux常用命令-网络命令