摘要 : CGContextRef 功能强大,我们借助它可以画各种图形。这里所举例子只是简单内容绘制,冰山一角,对此感兴趣的朋友可以举一反三,实现各种酷炫效果。

效果如下:

KMDrawView.h

 #import <UIKit/UIKit.h>

 @interface KMDrawView : UIView

 @end

KMDrawView.m

 #import "KMDrawView.h"

 @interface KMDrawView ()
- (void)drawFont;
- (void)drawLine;
- (void)drawCircle;
- (void)drawRectangle; @end @implementation KMDrawView - (instancetype)initWithFrame:(CGRect)frame{
if (self=[super initWithFrame:frame]) {
self.backgroundColor = [UIColor colorWithWhite:0.7 alpha:1.0];
}
return self;
} - (void)drawRect:(CGRect)rect {
[self drawFont];
[self drawLine];
[self drawCircle];
[self drawRectangle]; [super drawRect:rect];
} #pragma mark - 绘制『文字』、『线条』、『圆形』、『矩形』
- (void)drawFont {
NSDictionary *dicAttribute = @{
NSForegroundColorAttributeName : [UIColor brownColor],
NSFontAttributeName : [UIFont systemFontOfSize:18.0]
}; [@"我是文字" drawInRect:CGRectMake(20.0, 20.0, 100.0, 30.0) withAttributes:dicAttribute];
} - (void)drawLine {
CGContextRef contextRef = UIGraphicsGetCurrentContext(); //获取绘制上下文对象实例
CGContextSetRGBStrokeColor(contextRef, 0.5, 0.5, 0.5, 1.0); //设置笔画颜色
CGContextSetLineWidth(contextRef, 2.0); //设置线条粗细大小 CGContextMoveToPoint(contextRef, 20.0, 100.0); //设置直线的首端
CGContextAddLineToPoint(contextRef, 320.0, 100.0); //设置直线的末端
CGContextStrokePath(contextRef); //沿着要求的路径,开始绘制
} - (void)drawCircle {
CGContextRef contextRef = UIGraphicsGetCurrentContext(); //获取绘制上下文对象实例
CGContextSetRGBStrokeColor(contextRef, 1.0, 1.0, 1.0, 1.0); //设置笔画颜色
CGContextSetLineWidth(contextRef, 2.0); //设置线条粗细大小 //voidCGContextAddArc(CGContextRef c,CGFloat x,CGFloat y,CGFloat radius,CGFloat startAngle,CGFloat endAngle,int clockwise)
//1弧度=180°/π(≈57.3°)度
//360°=360 * π/180=2π弧度
//x,y为圆点坐标,radius半径,startAngle为开始的弧度,endAngle为结束的弧度,clockwise0为顺时针,1为逆时针。
CGContextAddArc(contextRef, 70.0, 200.0, 50.0, , *M_PI, ); //添加一个圆;M_PI为180度
CGContextDrawPath(contextRef, kCGPathStroke); //绘制路径
} - (void)drawRectangle {
CGContextRef contextRef = UIGraphicsGetCurrentContext(); //获取绘制上下文对象实例
CGContextSetRGBStrokeColor(contextRef, 0.0, 0.0, 0.0, 1.0); //设置笔画颜色
CGContextSetLineWidth(contextRef, 2.0); //设置线条粗细大小 CGContextAddRect(contextRef, CGRectMake(20.0, 300.0, 200.0, 100.0)); //设置矩形位置和宽高
CGContextStrokePath(contextRef); //沿着要求的路径,开始绘制
} @end

ViewController.h

 #import <UIKit/UIKit.h>

 @interface ViewController : UIViewController

 @end

ViewController.m

 #import "ViewController.h"
#import "KMDrawView.h" @interface ViewController ()
- (void)layoutUI;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; [self layoutUI];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (void)layoutUI {
KMDrawView *drawView = [[KMDrawView alloc] initWithFrame:self.view.frame];
[self.view addSubview:drawView];
} @end

使用 CGContextRef 进行简单内容绘制的更多相关文章

  1. 窗体皮肤实现 - 在VC中简单实现绘制(五)

    到第四部分Delphi XE3的代码能基本完成窗体界面的绘制.窗口中的其他控件的处理方法也是相同的,截获消息处理消息. 问题这个编译出来的个头可不小.Release版本竟然2.43M,完全是个胖子.系 ...

  2. 基于Tags的简单内容推荐的实现

    原来为了简单方便,自己小网站上的文章页的相关内容推荐就是从数据库里随机抽取数据来填充一个列表,所以一点相关性都没有,更本没有办法引导用户去访问推荐内容. 算法选择 如何能做到相似内容的推荐呢,碍于小网 ...

  3. OpenGL学习 (一) - 简单窗口绘制

    一.OpenGL 简介 OpenGL 本质: OpenGL(Open Graphics Library),通常可以认为是API,其包含了一系列可以操作图形.图像的函数.但深究下来,它是由Khronos ...

  4. H5简单内容

    1.简单认识H5 HTML5不仅仅是作为HTML标记语言的一个最新版本,更重要的是它指定了Web开发的一系列标准,成为第一个将Web作为应用开发平台的HTML语言. 我们日常讨论的H5其实是有一个泛称 ...

  5. R数据可视化手册学习简单的绘制常见的图形

    1.绘制散点图 # 使用ggplot2 library(ggplot2) ggplot(data = mtcars, aes(x = wt, y = mpg)) + geom_point() 2.绘制 ...

  6. CMS简单内容管理系统

    架构 NewsDaoSQLServerImpl public class NewsDaoSQLServerImpl extends BaseDao implements NewsDao { publi ...

  7. ArcGIS Engine简单图形绘制功能的实现(点、线、面)

    我们添加点.线.面来实现图形的编辑需要使用Geometry对象类. Point(点) 是一个0维的几何图形,具有X.Y坐标值,以及可选的属性,如高程值(Z值).度量值(M值).ID值等,可用于描述需要 ...

  8. python-opencv笔记 图像的读取和简单几何图形绘制

  9. android之简单图形绘制

    首先编写MyView类 代码如下: package com.example.myhello; import android.content.Context; import android.graphi ...

随机推荐

  1. button上传替换file上传按钮,并显示图片缩略图,纯jsp操作

    1.jsp代码 <div class="inputBox"> <span id="tu" <c:if test="${pd = ...

  2. Java 数据库中文变成问号???解决办法

    在连接的URL地址后面加上: url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8 于是在正式 ...

  3. [转]bootstrapTable refresh 方法使用简单举例

    原文地址:https://blog.csdn.net/lanyang123456/article/details/55805478 本文就bootstrapTable refresh 方法如何传递参数 ...

  4. [转]Ext.grid常用属性和方法

    原文地址:http://blog.csdn.net/fm19901229/article/details/8113969 1.Ext.grid.GridPanel  主要配置项:  store:表格的 ...

  5. C# 执行bat批处理文件

    private void RunBat(string batPath) { Process pro = new Process(); FileInfo file = new FileInfo(batP ...

  6. openwrt MT7620 固件编译记录

    下载,安装相关软件 git clone git@github.com:openwrt-mirror/openwrt.git sudo apt-get install gcc g++ binutils ...

  7. java小技巧-生成重复的字符

    今天碰到个需求,根据字段个数,动态生成sql的占位符,如下: public static void main(String[] args) { System.out.println(String.jo ...

  8. linux 的服务与进程管理(二)

    2.linux 的服务与进程管理 [2.1]系统启动流程 简单的介绍下linux的系统启动流程,方便我们深入了解linux操作系统,对排除linux系统故障进行帮助.启动流程虽然简单但背后还有着更加复 ...

  9. Android设计 - 图标设计概述(Iconography)

    2014-10-30 张云飞VIR 翻译自:https://developer.android.com/design/style/iconography.html Iconography 图标设计概述 ...

  10. Repeater 嵌套,子级Repeater获取 父级Repeater 中的值

    第一种方法,子级Repeater中绑定父级的某个字段: <%# DataBinder.Eval((Container.NamingContainer.NamingContainer as Rep ...