iOS绘图功能
2014-04-19 22:34
423 查看
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSMutableArray *pointArray = [NSMutableArray arrayWithCapacity:1];
[_lineArry addObject:pointArray];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
// 获取当前点的位置
CGPoint point = [touch locationInView:self];
NSLog(@"point = %@", NSStringFromCGPoint(point));
// 拿到最后一个对象
NSMutableArray *pointArray = [_lineArry lastObject];
// 转换成对象类型
NSValue *pointValue = [NSValue valueWithCGPoint:point];
// 添加到数组对象中
[pointArray addObject:pointValue];
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
// 拿到绘图的配置信息(绘画的属性信息)
// 通过图片得到上下文
CGContextRef context = UIGraphicsGetCurrentContext();
// 设置画笔的颜色
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
// 设置画笔的粗细
CGContextSetLineWidth(context, 2.0);
for (int i = 0; i < [_lineArry count]; i++) {
NSMutableArray *pointArry = [_lineArry objectAtIndex:i];
for (int j = 0; j < (int)pointArry.count-1; j++) {
NSValue *firstPointValue = [pointArry objectAtIndex:j];
NSValue *secondPointValue = [pointArry objectAtIndex:j + 1];
CGPoint firstPoint = [firstPointValue CGPointValue];
CGPoint secondPoint = [secondPointValue CGPointValue];
// 把笔触移动到一个点
CGContextMoveToPoint(context, firstPoint.x, firstPoint.y);
// 笔触和另一点形成一个路径(从move到point)
CGContextAddLineToPoint(context, secondPoint.x, secondPoint.y);
}
}// 绘制图形
CGContextStrokePath(context);
}
{
NSMutableArray *pointArray = [NSMutableArray arrayWithCapacity:1];
[_lineArry addObject:pointArray];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
// 获取当前点的位置
CGPoint point = [touch locationInView:self];
NSLog(@"point = %@", NSStringFromCGPoint(point));
// 拿到最后一个对象
NSMutableArray *pointArray = [_lineArry lastObject];
// 转换成对象类型
NSValue *pointValue = [NSValue valueWithCGPoint:point];
// 添加到数组对象中
[pointArray addObject:pointValue];
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
// 拿到绘图的配置信息(绘画的属性信息)
// 通过图片得到上下文
CGContextRef context = UIGraphicsGetCurrentContext();
// 设置画笔的颜色
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
// 设置画笔的粗细
CGContextSetLineWidth(context, 2.0);
for (int i = 0; i < [_lineArry count]; i++) {
NSMutableArray *pointArry = [_lineArry objectAtIndex:i];
for (int j = 0; j < (int)pointArry.count-1; j++) {
NSValue *firstPointValue = [pointArry objectAtIndex:j];
NSValue *secondPointValue = [pointArry objectAtIndex:j + 1];
CGPoint firstPoint = [firstPointValue CGPointValue];
CGPoint secondPoint = [secondPointValue CGPointValue];
// 把笔触移动到一个点
CGContextMoveToPoint(context, firstPoint.x, firstPoint.y);
// 笔触和另一点形成一个路径(从move到point)
CGContextAddLineToPoint(context, secondPoint.x, secondPoint.y);
}
}// 绘制图形
CGContextStrokePath(context);
}
相关文章推荐
- 我的iPhone桌面
- Object-c Archiving归档
- 西蒙iphone-OpenGL ES教程英文原帖地址以及实例代码
- [开源项目]开心英语8000句 Iphone+Ipad版
- 月光微博客
- 月光微博客
- iPhone动画效果
- iPhone动画属性详解
- 获取crash report ,并解析定位代码
- 在iPhone项目中使用讯飞语音SDK实现语音识别和语音合成
- iPhone表情符号编码
- 深入浅出Cocoa之Bonjour网络编程(服务器端)
- iOS6全系列设备完美越狱工具发布
- 家庭文件共享:FreeNas服务器的搭建过程
- 如何设置iPhone无需密钥使用L2TP协议***
- HTML5 Loading来了
- [分享]iOS 5.0.1 桌面出现 四个白图标 的解决办法!AdSheet FieldTest iOS Diagnostics 设置。
- iphone Web App 导航设计探讨
- [转]从实现iPhone的OAuth封装看国内互联网和开放平台