定义一个UIView:主要是在这个View里面加一个UIImageView,绘图都在这个UIImageView里面进行

@property(nonatomic) CGPoint prePoint;  //手指在进入move事件之前的那个点
@property(nonatomic) CGPoint oppsitePoint; //手指在进入move事件之前的那个点
@property(nonatomic, retain) UIImageView* drawImage;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.drawImage = [[UIImageView alloc] initWithImage:nil];
self.drawImage.frame = self.frame;
[self addSubview:_drawImage]; }
return self;
}

然后处理手指的事件

#pragma mark - deal touch
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"touchesBegan");
//以下两句知道手指在屏幕上的点的信息
UITouch* touch = [touches anyObject];
CGPoint point = [touch locationInView:self]; if (touch) {
self.prePoint = point;
_oppsitePoint = point;
_oppsitePoint.x = 320.0f - point.x;
}
} -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"touchesMoved");
UITouch* touch = [touches anyObject];
if (touch) {
CGPoint point = [touch locationInView:self];
UIGraphicsBeginImageContext(self.frame.size);
[_drawImage.image drawInRect:CGRectMake(0, 0, _drawImage.frame.size.width, _drawImage.frame
.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0f);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),0.314, 0.486, 0.859, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), _prePoint.x, _prePoint.y);
CGContextAddQuadCurveToPoint(UIGraphicsGetCurrentContext(), _prePoint.x, _prePoint.y, point.x, point.y); CGContextMoveToPoint(UIGraphicsGetCurrentContext(), _oppsitePoint.x, _oppsitePoint.y);
CGContextAddQuadCurveToPoint(UIGraphicsGetCurrentContext(), _oppsitePoint.x, _oppsitePoint.y, 320.0f - point.x, point.y); // CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), point.x, point.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
_drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); _prePoint = point;
_oppsitePoint = point;
_oppsitePoint.x = 320.0f - point.x; } } -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"touchesEnded"); //这段的作用是假设在屏幕上点击能够画出点
UIGraphicsBeginImageContext(self.frame.size);
[_drawImage.image drawInRect:CGRectMake(0, 0, _drawImage.frame.size.width, _drawImage.frame
.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 3.0f);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),0.314, 0.486, 0.859, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), _prePoint.x, _prePoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), _prePoint.x, _prePoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
_drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); self.isTouch = NO;
}

以下为效果图

ios创建画笔的样例(双笔画效果)的更多相关文章

  1. iOS创建安全的单例

    创建安全的单例 #import "Singleton.h" @implementation Singleton static Singleton* _instance = nil; ...

  2. 构造Scala开发环境并创建ApiDemos演示样例项目

    从2011年開始写Android ApiDemos 以来.Android的版本号也更新了非常多,眼下的版本号已经是4.04. ApiDemos中的样例也添加了不少,有必要更新Android ApiDe ...

  3. AppCan移动应用开发平台新增9个超有用插件(内含演示样例代码)

    使用AppCan平台进行移动开发.你所须要具备的是Html5+CSS +JS前端语言基础.此外.Hybrid混合模式应用还需结合原生语言对功能模块进行封装,对于没有原生基础的开发人员,怎样实现App里 ...

  4. 最简单的基于FFmpeg的移动端样例:Android HelloWorld

    ===================================================== 最简单的基于FFmpeg的移动端样例系列文章列表: 最简单的基于FFmpeg的移动端样例:A ...

  5. 01_MUI之Boilerplate中:HTML5演示样例,动态组件,自己定义字体演示样例,自己定义字体演示样例,图标字体演示样例

     1安装HBuilder5.0.0,安装后的界面截图例如以下: 2 依照https://www.muicss.com/docs/v1/css-js/boilerplate-html中的说明,创建上 ...

  6. PHPCMS中GET标签概述、 get 标签语法、get 标签创建工具、get 调用本系统演示样例、get 调用其它系统演示样例

    一.get 标签概述 通俗来讲,get 标签是Phpcms定义的能直接调用数据库里面内容的简单化.友好化代码,她可调用本系统和外部数据,仅仅有你对SQL有一定的了解,她就是你的绝世好剑!也就是适合熟悉 ...

  7. 10分钟理解Android数据库的创建与使用(附具体解释和演示样例代码)

    1.Android数据库简单介绍. Android系统的framework层集成了Sqlite3数据库.我们知道Sqlite3是一种轻量级的高效存储的数据库. Sqlite数据库具有以下长处: (1) ...

  8. 最简单的基于FFmpeg的移动端样例:IOS 视频解码器

    ===================================================== 最简单的基于FFmpeg的移动端样例系列文章列表: 最简单的基于FFmpeg的移动端样例:A ...

  9. 最简单的基于FFmpeg的移动端样例:IOS 视频转码器

    ===================================================== 最简单的基于FFmpeg的移动端样例系列文章列表: 最简单的基于FFmpeg的移动端样例:A ...

随机推荐

  1. CERC 2013 Magical GCD

    题目大意如下:给定一个序列,每个序列有值xi,现给定t个数列,对于每个长n的数列,求一段[l,r]使 [r-l+1]*gcd(l,r)最大,gcd(l,r)指的是该连续区间的最大公约数. 不难想到n^ ...

  2. 设计模式 - 适配器模式(adapter pattern) 具体解释

    适配器模式(adapter pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 适配器模式(adapter pattern): 将一个类的接 ...

  3. 百度前端技术学院Html&CSS学习资源

    Web相关名词通俗解释 https://www.zhihu.com/question/22689579 MDN HTML入门 https://developer.mozilla.org/zh-CN/d ...

  4. if和switch的区别,循环的for 和while的区别, 字符串常用的7种方法

    相同点: 都是用于多重选择 不同点: 多重IF没有switch选择结构的限制,特别适合变量处于某个连续区间的情况 switch只能处理等值条件判断的情况,而且条件必须是整型变量或者字符串变量 字符串的 ...

  5. CentOS7 安装JDK

    链接地址:http://jingyan.baidu.com/article/c74d60007b85510f6a595dfa.html VMware 10 centos 安装jdk 及mysql 方法 ...

  6. MyEclipse下JDBC-MySQL配置总结

    原创文章,转载请注明:MyEclipse下JDBC-MySQL配置总结  By Lucio.Yang 新手,初期配置未成功,后将网上的方法几乎全部尝试才弄好,下面的方法全而不简练,希望高手指正. 1. ...

  7. Web开发找错基本思路

    WEB开发主要是两个交互(B/S数据) 浏览器:  1 包含html.css.js等客户端代码运行错误. 传输端: 1 路径引入错误,常用插件的引入(artDialog.Ztree.JQuery等). ...

  8. Subsets 【dfs】

    Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...

  9. jQuery tablesort插件推荐

    搜索结果的第一条网址(似乎是Official Site)似乎有问题(也可能是我弄错了  总之chrome中有个叉叉) 所以还是用这个吧http://mottie.github.io/tablesort ...

  10. 顺序队列之C++实现

    下面介绍下用C++实现的顺序队列,在VC6下调试通过. 1.文件组织形式 2.sq.h顺序队列类的说明 #ifndef _SQ_H_ #define _SQ_H_ typedef int dataTy ...