IOS Quartz2D 学习一 手写板
来源:互联网 发布:我的世界枪械手机版js 编辑:程序博客网 时间:2024/05/16 08:07
NSMutableArray *allPoint;
NSMutableArray *allLine;
allLine 在view init 的时候初始化
接下来要捕获手指和设备的交互的位置。
代码如下:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
allPoint = [[NSMutableArrayalloc]init];
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
LocationMode *mode = [[LocationModealloc]init];
mode.x = location.x;
mode.y = location.y;
[allPoint addObject:mode];
[mode release];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
LocationMode *mode = [[LocationModealloc]init];
mode.x = location.x;
mode.y = location.y;
[allPoint addObject:mode];
[mode release];
[selfsetNeedsDisplay];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[allLine addObject:allPoint];
[allPoint release];
[selfsetNeedsDisplay];
}
现在要做的就是 去将这些点绘制出来
- (void)drawRect:(CGRect)rect
因为我们每次在触摸屏幕的时候,在touchend 的时候才会将 点数组 添加到 allLine 数组中,所以要分两次绘制,就是之前的所有路径,当前正在绘制的路径。
具体代码:
CGContextRef cgContext = UIGraphicsGetCurrentContext();
//设置笔冒
CGContextSetLineCap(cgContext,kCGLineCapRound);
//设置画线的连接处 拐点圆滑
CGContextSetLineJoin(cgContext,kCGLineJoinRound);
CGContextBeginPath(cgContext);
if(allLine && allLine.count > 0)
{
for(int j =0;j<allLine.count;j++)
{
NSMutableArray *array = [allLineobjectAtIndex:j];
if(array)
{
[array retain];
for(int i =0;i<array.count;i++)
{
LocationMode *location = [arrayobjectAtIndex:i];
if (i == 0)
{
CGContextMoveToPoint(cgContext, location.x, location.y);
}
else
{
CGContextAddLineToPoint(cgContext, location.x, location.y);
}
}
[array release];
}
}
}
if(allPoint &&allPoint.count >0)
{
for(int i =0;i<allPoint.count;i++)
{
LocationMode *location = [allPointobjectAtIndex:i];
if (i == 0)
{
CGContextMoveToPoint(cgContext, location.x, location.y);
}
else
{
CGContextAddLineToPoint(cgContext, location.x, location.y);
}
}
}
CGContextSetFillColorWithColor(cgContext, [[UIColorblueColor]CGColor]);
CGContextSetStrokeColorWithColor(cgContext, [UIColorredColor].CGColor);
CGContextSetLineWidth(cgContext,3.0);
CGContextStrokePath(cgContext);
- IOS Quartz2D 学习一 手写板
- [IOS]手写板
- IOS学习之Quartz2D
- iOS学习笔记--Quartz2D
- iOS学习 - Quartz2D绘图学习
- iOS 学习笔记 Quartz2D绘图
- iOS Core Graphics手写板
- Android绘图学习 - 手写板
- iOS开发—Quartz2D简单使用(一)
- iOS Quartz2D基本图形绘制(一)
- iOS之Quartz2D绘图(一)
- iOS开发学习之Quartz2D绘图
- iOS学习笔记08-Quartz2D绘图
- iOS quartz2D 的从零到一学习使用
- iOS开发学习之Quartz2D绘图
- iOS quartz2D 的从零到一学习使用
- iOS学习笔记-051.Quartz2D简介
- IOS:Quartz2D
- JavaSE第六十八讲:Java动态代理总结
- 文件的逻辑结构
- androdid设置View的Margin
- oracle dba 常用语句7(password,role,user)
- PHP访问Mysqli的实例
- IOS Quartz2D 学习一 手写板
- 64位程序
- 工厂方法模式实现数据库连接
- ajax发送xml
- NSURLConnection 网络超时
- 概率论与数理统计:随机变量、分布律,分布函数,密度函数
- ASIHTTPRequest-直接读取磁盘数据流的请求体 ASIHTTPRequest-数据压缩
- eclipse环境下,继承父类或接口的方法,写上@Override报错问题
- 程万云:做淘宝客需要思考和实践还要有成功的欲望 .