使用 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 进行简单内容绘制的更多相关文章
- 窗体皮肤实现 - 在VC中简单实现绘制(五)
到第四部分Delphi XE3的代码能基本完成窗体界面的绘制.窗口中的其他控件的处理方法也是相同的,截获消息处理消息. 问题这个编译出来的个头可不小.Release版本竟然2.43M,完全是个胖子.系 ...
- 基于Tags的简单内容推荐的实现
原来为了简单方便,自己小网站上的文章页的相关内容推荐就是从数据库里随机抽取数据来填充一个列表,所以一点相关性都没有,更本没有办法引导用户去访问推荐内容. 算法选择 如何能做到相似内容的推荐呢,碍于小网 ...
- OpenGL学习 (一) - 简单窗口绘制
一.OpenGL 简介 OpenGL 本质: OpenGL(Open Graphics Library),通常可以认为是API,其包含了一系列可以操作图形.图像的函数.但深究下来,它是由Khronos ...
- H5简单内容
1.简单认识H5 HTML5不仅仅是作为HTML标记语言的一个最新版本,更重要的是它指定了Web开发的一系列标准,成为第一个将Web作为应用开发平台的HTML语言. 我们日常讨论的H5其实是有一个泛称 ...
- R数据可视化手册学习简单的绘制常见的图形
1.绘制散点图 # 使用ggplot2 library(ggplot2) ggplot(data = mtcars, aes(x = wt, y = mpg)) + geom_point() 2.绘制 ...
- CMS简单内容管理系统
架构 NewsDaoSQLServerImpl public class NewsDaoSQLServerImpl extends BaseDao implements NewsDao { publi ...
- ArcGIS Engine简单图形绘制功能的实现(点、线、面)
我们添加点.线.面来实现图形的编辑需要使用Geometry对象类. Point(点) 是一个0维的几何图形,具有X.Y坐标值,以及可选的属性,如高程值(Z值).度量值(M值).ID值等,可用于描述需要 ...
- python-opencv笔记 图像的读取和简单几何图形绘制
- android之简单图形绘制
首先编写MyView类 代码如下: package com.example.myhello; import android.content.Context; import android.graphi ...
随机推荐
- 树型结构递归 实体递归 JSON格式
用递归实现无限级菜单,产品分类,盖楼式评论.留言等功能. 下列代码不能直接使用 CREATE TABLE [dbo].[P_Category]( [Code] [varchar](36) NOT NU ...
- 开发错误处理记录(无法激活服务,因为它不支持 ASP.NET 兼容性)
错误提示:无法激活服务,因为它不支持 ASP.NET 兼容性.已为此应用程序启用了 ASP.NET 兼容性.请在 web.config 中关闭 ASP.NET 兼容性模式或将 AspNetCompat ...
- JAVA实用工具--javamail
在实现javamail之前首先要搭建邮件服务器 James 在进行WEB程序开发的时候需要使用Tomcat服务器,但是Tomcat服务器并不支持邮件的处理操作,所以要想进行邮件的发送,还需要配置一个单 ...
- java泛型中<? super String>和<? extends String> 的区别
(1)<? super String> is any class which is a superclass of String (including String itself). (I ...
- [mqtt]mqtt嵌入式移植
github eclipse paho source code: https://github.com/mqtt/mqtt.github.io/wiki/libraries STM32 mqtt移植: ...
- java获取上周任意一天的日期
/** * 获取上周周几的日期,默认一周从周一开始 * @param dayOfWeek * @param weekOffset * @return */ public static Date get ...
- javascrpt 代码
一.window.open关闭子页面时刷新父页面 window.opener.location.reload(); window.opener=null;window.close(); 二.AJAX返 ...
- android开发(43) 动画演示,会跑的小人,从屏幕左侧跑到右侧
想做一个动画,一个会跑的小人,从屏幕右侧跑道右侧,于是做了个尝试,上图: 要完成这样需要三步: 1. 做一个 帧动画 (frame animation),由多张图片组成,组成小人连续跑动的样子. 2. ...
- JQuery控制radio选中和不选中方法总结
一.设置选中方法 代码如下: $("input[name='名字']").get(0).checked=true; $("input[name='名字']"). ...
- 自然语言交流系统 phxnet团队 创新实训 个人博客 (十)
下载emacs-23.1.tar.gz http://ftp.gnu.org/pub/gnu/emacs/emacs-23.1.tar.gz cd /opt //cp emacs-23.1.ta ...