摘要 : 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. Facebook的Fairseq模型详解(Convolutional Sequence to Sequence Learning)

    1. 前言 近年来,NLP领域发展迅速,而机器翻译是其中比较成功的一个应用,自从2016年谷歌宣布新一代谷歌翻译系统上线,神经机器翻译(NMT,neural machine translation)就 ...

  2. iOSUITableView头部带有图片并且下拉图片放大效果

    最近感觉UITableview头部带有图片,并且下拉时图片放大这种效果非常炫酷,所以动手实现了一下,效果如下图: 1.gif 实现原理很简单,就是在UITableview上边添加一个图片子视图,在ta ...

  3. 项目抛弃Tomcat容器,用代码启动Tomcat插件

    tomato启动代码如下: package tomcat; import org.apache.catalina.connector.Connector; import org.apache.cata ...

  4. plot sin 动态配置rc settings

    plot sin 动态配置rc settings 坐标轴颜色 线的颜色 绘图前景色 Code #!/usr/bin/env python # -*- coding: utf-8 -*- import ...

  5. 区块链blockchina简述

    区块链是比特币的底层技术和基础架构,本质上是一个去中心化的数据库.区块链是一串使用密码学方法相关联产生的数据块,每一个数据块中包含了一次比特币网络交易的信息,用于验证其信息的有效性(防伪)并生成下一个 ...

  6. linux异步IO--aio

    简述 linux下异步方式有两种:异步通知和异步IO(AIO),异步通知请参考:linux异步通知 Linux的I/O机制经历了一下几个阶段的演进: 1. 同步阻塞I/O: 用户进程进行I/O操作,一 ...

  7. Hbase 学习(九) 华为二级索引(原理)

    这个是华为的二级索引方案,已经开放源代码了,下面是网上的一篇讲解原理的帖子,发出来和大家共享一下. 经过本人认真阅读了一下代码,发现这个源码仅供参考,想要集成到原有的集群当中是有点儿难度的,它对hba ...

  8. 【jquery】基于 jquery 的翻牌效果 flip

    最近做了个类似于塔罗牌翻牌的效果,分享给大家. <!doctype html> <html lang="en"> <head> <meta ...

  9. http://www.gasi.ch/blog/inside-deep-zoom-2/

    Inside Deep Zoom – Part II: Mathematical Analysis Welcome to part two of Inside Deep Zoom. In part o ...

  10. 6、Qt Meta Object system 学习

    原文地址:http://blog.csdn.net/ilvu999/article/details/8049908 使用 meta object system 继承自 QOject 类定义中添加 Q_ ...