简单的实现涂鸦功能
2014-03-08 09:24
471 查看
- (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code // 定义了一个线数组,来存储所有的线 self.lineArray = [[NSMutableArray alloc] initWithCapacity:1]; // 设置一个清除按钮 UIButton *clear = [UIButton buttonWithType:UIButtonTypeSystem]; clear.frame = CGRectMake(frame.size.width-100, frame.size.height-100, 100, 100); [clear setTitle:@"清除" forState:UIControlStateNormal]; [clear addTarget:self action:@selector(clearScreen) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:clear]; } return self; } - (void)clearScreen{ // 清除所有的线,即存在线数组中的对象 [_lineArray removeAllObjects]; // 重绘(具体概念在动画中更明显,这里理解为,接受到了命令,接下来就是点击开始按钮运行) [self setNeedsDisplay]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ // 一触摸屏幕,就将启动点存进pointArray中,并且将绘成第一条线所需的点数组作为元素存进定义的_lineArray中 // 为什么这样设置呢?因为手一离开屏幕,又是一个新的启动点传入,也是另一条新线的划入 NSMutableArray * pointArray = [NSMutableArray arrayWithCapacity:1]; [_lineArray addObject:pointArray]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ // 获得触摸屏幕的所有对象 UITouch * touch = [touches anyObject]; // 获得触摸屏幕的所有点的位置 CGPoint point = [touch locationInView:self]; // 利用NSValue将获得的点封装成NSValue类型对象,以便能存进NSMutableArray中 NSValue * pointCount = [NSValue valueWithCGPoint:point]; // 与touchesBegan方法想呼应,一触摸屏幕所设置的点数组也就是最后一个点数组,这里取出来,存移动中所有点 NSMutableArray * pointArray = [_lineArray lastObject]; [pointArray addObject:pointCount]; // 重新绘制这个self.view [self setNeedsDisplay]; } // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // 获得上下文本(我更愿意理解为获得进行绘画资格,作为一个通行证般的参数,让你能设置画笔颜色,线条尺寸,以及如何绘画) CGContextRef context = UIGraphicsGetCurrentContext(); // 设置画笔 CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); // 设置线条粗细 CGContextSetLineWidth(context, 2.0); for (int i = 0; i < _lineArray.count; i++) { NSMutableArray * pointArray = [_lineArray objectAtIndex:i]; // 这里(int)pointArray.count进行了类型转换,可以思考下是为什么 for (int j = 0; j < (int)pointArray.count - 1; j++) { // 获得封装的起点 NSValue * headPointValue = [pointArray objectAtIndex:j]; // 获得封装的终点 NSValue * tailPointValue = [pointArray objectAtIndex:(j + 1)]; // 转化为CGPoint类型 CGPoint headPoint = [headPointValue CGPointValue]; // 转化为CGPoint类型 CGPoint tailPoint = [tailPointValue CGPointValue]; // 设置画笔的起点 CGContextMoveToPoint(context, headPoint.x, headPoint.y); // 画笔路径的途径直到终点 CGContextAddLineToPoint(context, tailPoint.x, tailPoint.y); } } // 绘制这个文本(总算可以画画了) CGContextStrokePath(context); }
相关文章推荐
- 简单的实现涂鸦功能
- 使用bitmapData简单实现涂鸦中的橡皮功能(as3)
- 使用bitmapData简单实现涂鸦中的橡皮功能[Flex]
- iOS实现白板、画板功能,有趣的涂鸦工具,已封装,简单快捷使用
- iOS开发------简单实现图片多选功能(Photos.framework篇)
- html制作简单框架网页 实现自己的音乐驿站 操作步骤及源文件下载 (播放功能限mp3文件)
- 超简单实现iOS列表的索引功能
- 浅谈百度地图的简单开发之结合方向传感器实现定位功能(三)
- python实现简单爬虫功能
- Servlet学习之二实现简单的登录功能
- unity实现简单的加密使用次数限制时间限制功能
- Android 使用Path实现涂鸦功能
- Android简单的短信验证功能的实现代码
- 在一个程序中需要用到全局变量(在多个class之间共享数据),请问如何定义具有这种功能的变量?或者是否有其他的方法解决多个class之间的数据共享(尽量简单实现)。 首先应该明确 Java中没有全局变
- 自定义一个view,并实现最简单的手势识别功能(下)
- Java使用cookie实现简单的单点登录功能
- 【Android游戏开发十六】Android Gesture之【触摸屏手势识别】操作!利用触摸屏手势实现一个简单切换图片的功能!
- 分享一个iOS下实现基本绘画板功能的简单方法
- SDK编程:任务栏TaskBar与对话框Dialog实现一个简单功能