iOS UI进阶-1.0 Quartz2D
概述
- 绘制图形 : 线条\三角形\矩形\圆\弧等
- 绘制文字
- 绘制\生成图片(图像)
- 读取\生成PDF
- 截图\裁剪图片
- 自定义UI控件
代码实现
绘制时,绘制内容必须写在-(void)drawRect:(CGRect)rect 这个方法内,因为在其它方法里,取不到图形上下文。
绘制线条
#import "LineView.h" @implementation LineView -(void)drawRect:(CGRect)rect
{
// Drawing code
// 1.获得图形上下文
CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2.拼接图形(路径)
// 设置线段宽度
CGContextSetLineWidth(ctx, ); // 设置线段头尾部的样式(圆型)
CGContextSetLineCap(ctx, kCGLineCapRound); // 设置线段转折点的样式(圆型)
CGContextSetLineJoin(ctx, kCGLineJoinRound); /** 第1根线段 **/
// 设置颜色
CGContextSetRGBStrokeColor(ctx, , , , );
// 设置一个起点
CGContextMoveToPoint(ctx, , );
// 添加一条线段到(100, 100)
CGContextAddLineToPoint(ctx, , ); // 渲染显示到view上面
CGContextStrokePath(ctx); /** 第2根线段 **/
// 设置颜色
CGContextSetRGBStrokeColor(ctx, , , , );
// 设置一个起点
CGContextMoveToPoint(ctx, , );
// 添加一条线段到(150, 40)
CGContextAddLineToPoint(ctx, , );
CGContextAddLineToPoint(ctx, , ); // 渲染显示到view上面
CGContextStrokePath(ctx);
}
@end
效果

绘制四边形
/**
* 画四边形
*/
void draw4Rect()
{
// 1.获得上下文
CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2.画矩形
CGContextAddRect(ctx, CGRectMake(, , , )); // set : 同时设置为实心和空心颜色
// setStroke : 设置空心颜色
// setFill : 设置实心颜色
[[UIColor whiteColor] set]; // 3.绘制图形
CGContextFillPath(ctx);
}
绘制三角形
/**
* 画三角形
*/
void drawTriangle()
{
// 1.获得上下文
CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2.画三角形
CGContextMoveToPoint(ctx, , );
CGContextAddLineToPoint(ctx, , );
CGContextAddLineToPoint(ctx, , );
// 关闭路径(连接起点和最后一个点)
CGContextClosePath(ctx); // 颜色
CGContextSetRGBStrokeColor(ctx, , , , ); // 3.绘制图形
CGContextStrokePath(ctx);
}
绘制圆形和圆弧
/**
* 画圆弧
*/
void drawArc()
{
// 1.获得上下文
CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2.画圆弧
// x\y : 圆心
// radius : 半径
// startAngle : 开始角度
// endAngle : 结束角度
// clockwise : 圆弧的伸展方向(0:顺时针, 1:逆时针)
CGContextAddArc(ctx, , , , M_PI_2, M_PI, ); // 3.显示所绘制的东西
CGContextFillPath(ctx);
} /**
* 画圆
*/
void drawCircle()
{
// 1.获得上下文
CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2.画圆
CGContextAddEllipseInRect(ctx, CGRectMake(, , , )); CGContextSetLineWidth(ctx, ); // 3.显示所绘制的东西
CGContextStrokePath(ctx);
}
常用方法
拼接函数
// 新建一个起点
void CGContextMoveToPoint(CGContextRef c, CGFloat x, CGFloat y) // 添加新的线段到某个点
void CGContextAddLineToPoint(CGContextRef c, CGFloat x, CGFloat y) // 添加一个矩形
void CGContextAddRect(CGContextRef c, CGRect rect) // 添加一个椭圆
void CGContextAddEllipseInRect(CGContextRef context, CGRect rect) // 添加一个圆弧
void CGContextAddArc(CGContextRef c, CGFloat x, CGFloat y,
CGFloat radius, CGFloat startAngle, CGFloat endAngle, int clockwise) // Mode参数决定绘制的模式
void CGContextDrawPath(CGContextRef c, CGPathDrawingMode mode) // 绘制空心路径
void CGContextStrokePath(CGContextRef c) // 绘制实心路径
void CGContextFillPath(CGContextRef c)
提示:一般以CGContextDraw、CGContextStroke、CGContextFill开头的函数,都是用来绘制路径的
图形上下文栈的操作
// 将当前的上下文copy一份,保存到栈顶(那个栈叫做”图形上下文栈”)
void CGContextSaveGState(CGContextRef c) // 将栈顶的上下文出栈,替换掉当前的上下文
void CGContextRestoreGState(CGContextRef c)
另:重绘方法setNeedsDisplay。比如,我们已经绘制了一个图片或者线条,想滑动改变尺寸,可以通过这样设置:
- (void)setImage:(UIImage *)image
{
_image = image; [self setNeedsDisplay];
}
源代码下载:点击下载
Quartz 2D 官方文档:点击下载
iOS UI进阶-1.0 Quartz2D的更多相关文章
- [iOS UI进阶 - 6.0] CALayer
A.基本知识 1.需要掌握的 CALayer的基本属性 CALayer和UIView的关系 position和anchorPoint的作用 2.概念 在iOS中,你能看得见摸得着的东西基本上都是U ...
- [iOS UI进阶 - 3.0] 触摸事件的基本处理
A.需要掌握和练习的 1.介绍事件类型2.通过按钮的事件处理引出view的事件处理3.响应者对象 --> UIResponder --> UIView4.view的拖拽* 实现触摸方法,打 ...
- [iOS UI进阶 - 2.0] 彩票Demo v1.0
A.需求 1.模仿“网易彩票”做出有5个导航页面和相应功能的Demo 2.v1.0 版本搭建基本框架 code source:https://github.com/hellovoidworld/H ...
- iOS UI进阶-4.0 地图与定位
在移动互联网时代,移动app能解决用户的很多生活琐事,比如 导航:去任意陌生的地方 周边:找餐馆.找酒店.找银行.找电影院 在上述应用中,都用到了地图和定位功能,在iOS开发中,要想加入这2大功能 ...
- [iOS UI进阶 - 4.0] 涂鸦app Demo
A.需求 1.超简易画图,只有一种画笔 2.清屏功能 3.回退功能 4.保存功能 5.使用了cocos2D code source: https://github.com/hellovoidwor ...
- iOS UI进阶-3.0 核心动画
Core Animation是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍,使用它需要先添加QuartzCore.framework和引入对应的框架<Quar ...
- iOS UI进阶-2.0 CALayer
在iOS中,你能看得见摸得着的东西基本上都是UIView,比如一个按钮.一个文本标签.一个文本输入框.一个图标等等,这些都是UIView 其实UIView之所以能显示在屏幕上,完全是因为它内部的一个图 ...
- iOS UI进阶-1.1 Quartz2D 图片水印/裁剪/截图
图片水印 UIImage+MJ.h #import <UIKit/UIKit.h> @interface UIImage (MJ) /** * 打水印 * * @param bg 背景图片 ...
- [iOS UI进阶 - 5.0] 手势解锁Demo
A.需求 1.九宫格手势解锁 2.使用了绘图和手势事件 code source: https://github.com/hellovoidworld/GestureUnlockDemo B ...
随机推荐
- bootstrapValidator多字段联合验证(如开始日期和结束日期中,开始日期不可晚于结束日期)
接触bootstrapvalidator时间不久,最近需要多个字段共同验证,网上查了一下未找到,查阅api文档,发现确实可以实现. 先看dom <div class="form-gro ...
- F#周报2018年第49期
新闻 ML.NET 0.8--Machine Learning for .NET .NET Core 3预览 1以及开源Windows桌面框架 .NET Core 2.2 尝试C# 8.0 .NET ...
- [No000013B]初识Ildasm.exe——IL反编译的实用工具
Ildasm.exe 概要: 一.前言: 微软的IL反编译实用程序——Ildasm.exe,可以对可执行文件(ex,经典的控制台Hello World 的 exe 可执行文件)抽取出 IL 代码,并且 ...
- [No0000BE]控制台切换字符格式&Code Page Identifiers
cmd chcp命令切换字符格式 命令介绍: chcp 65001 #换成utf-8代码页 chcp 936 #换成默认的gbk chcp 437 #美国英语 一般默认为gbk,若要修改成 utf-8 ...
- phpstorm之ssh链接远程Linux服务器
save ssh session inPHPstorm. open PHPstorm,open File,> Settings >search for 'Deployment' > ...
- Ubuntu启动时a start job is running for dev-disk-by延时解决
写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文! 本博客全网唯一合法URL:ht ...
- lsof and dynamic array in bash/shell
https://unix.stackexchange.com/questions/171519/lsof-warning-cant-stat-fuse-gvfsd-fuse-file-system F ...
- 【pyqtgraph】pyqtgraph-鼠标互动
pyqtgraph绘图库官方文档学习-鼠标互动(mouse interaction) 鼠标互动 大多数使用pyqtgraph数据可视化的应用程序都会生成可以使用鼠标进行交互式缩放,平移和配置的小部件. ...
- 【PyQt5-Qt Designer】按钮系列
[PyQt5-Qt Designer]按钮系列 复选框(QCheckBox) 效果如下: 参考: https://zhuanlan.zhihu.com/p/30509947 完整代码: from Py ...
- Scala笔记
定义包 package com.runoob { class HelloWorld } 引用包 import java.awt.Color // 引入Color import java.awt._ / ...