摘要 : 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. python 下载虾米音乐

    #!/usr/bin/env python2 # coding:utf-8 import urllib import re import sys import urllib2 # xml => ...

  2. iOS隐藏状态栏

    1.整个项目隐藏状态栏 在Targets->General->勾选中Hide status bar . 整个项目隐藏状态栏 2.单个界面隐藏状态栏,例如登录注册页面 1.首先在info.p ...

  3. java基础篇---新I/O技术(NIO)

    在JDK1.4以前,I/O输入输出处理,我们把它称为旧I/O处理,在JDK1.4开始,java提供了一系列改进的输入/输出新特性,这些功能被称为新I/O(NEW I/O),新添了许多用于处理输入/输出 ...

  4. 处理异常json串的jar包JsonSerde

    参考下面文章: https://blog.csdn.net/SunnyYoona/article/details/70170173

  5. 【oneday_onepage】——Ten Changes To Make A Difference In Your Life

    When you want to change something in your life, it can feel overwhelming. Whether it’s losing 50lbs ...

  6. SpringBoot 中 @RequestBody的正确使用方法

    SpringBoot 中 @RequestBody的正确使用方法 最近在接收一个要离职同事的工作,接手的项目是用SpringBoot搭建的,其中看到了这样的写法: @RequestMapping(&q ...

  7. 攻城狮送女友的CSS3生日蛋糕

    在线预览:http://keleyi.com/keleyi/phtml/html5/29.htm 代码如下: <!DOCTYPE html> <html> <head&g ...

  8. USB学习笔记连载(十一):CY7C68013A的启动方式-EEPROM

       上述的应用笔记中有介绍FX2LP的启动选项,主要包括I2C启动和USB启动. 说白了I2C启动需要使用外部的EEPROM,USB启动,只是使用上位机控制软件将配置程序FX2LP中,不用EEPRO ...

  9. 《FPGA全程进阶---实战演练》第一章之FPGA介绍

    1 什么是FPGA FPGA也即是Field Programmable Gate Array的缩写,翻译成中文就是现场可编程门阵列.FPGA是在PAL.GAL.CPLD等可编程器件的基础上发展起来的新 ...

  10. svn解决不能clean的方法

    http://blog.csdn.net/victory08/article/details/42100325 svn执行clean up后出现提示:svn cleanup failed–previo ...