UIView上签名绘图示例
来源:互联网 发布:java总是安装失败 编辑:程序博客网 时间:2024/05/16 11:26
ipad上绘图的软件不错吧,以有有一个小朋友写了一个涂鸦软件,大买呀。这儿有一个示例。
Canvas2D.h
#import <UIKit/UIKit.h>#import <QuartzCore/QuartzCore.h>@interface Canvas2D : UIView { NSMutableArray* arrayStrokes;UIColor *currentColor;}@property (retain) NSMutableArray* arrayStrokes;@end
Canvas2D.m
#import "Canvas2D.h"@implementation Canvas2D@synthesize arrayStrokes;- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { // Initialization codeself.arrayStrokes = [NSMutableArray array]; } return self;}- (id)initWithCoder:(NSCoder *)aDecoder { if ((self = [super initWithCoder:aDecoder])) {self.arrayStrokes = [NSMutableArray array]; } return self;}// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect{ // Drawing codeCGContextRef context = UIGraphicsGetCurrentContext();CGContextSetLineWidth(context, 2.0);CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();CGFloat components[] = {0.0, 0.0, 1.0, 1.0};CGColorRef color = CGColorCreate(colorspace, components);CGContextSetStrokeColorWithColor(context, color);CGContextMoveToPoint(context, 0, 0);CGContextAddLineToPoint(context, 300, 400);CGContextStrokePath(context);CGColorSpaceRelease(colorspace);CGColorRelease(color);if (self.arrayStrokes){int arraynum = 0;// each iteration draw a stroke// line segments within a single stroke (path) has the same color and line widthfor (NSDictionary *dictStroke in self.arrayStrokes){NSArray *arrayPointsInstroke = [dictStroke objectForKey:@"points"];UIColor *color = [dictStroke objectForKey:@"color"];float size = [[dictStroke objectForKey:@"size"] floatValue];[color set];// equivalent to both setFill and setStroke// draw the stroke, line by line, with rounded jointsUIBezierPath* pathLines = [UIBezierPath bezierPath];CGPoint pointStart = CGPointFromString([arrayPointsInstroke objectAtIndex:0]);[pathLines moveToPoint:pointStart];for (int i = 0; i < (arrayPointsInstroke.count - 1); i++){CGPoint pointNext = CGPointFromString([arrayPointsInstroke objectAtIndex:i+1]);[pathLines addLineToPoint:pointNext];}pathLines.lineWidth = size;pathLines.lineJoinStyle = kCGLineJoinRound;pathLines.lineCapStyle = kCGLineCapRound;[pathLines stroke];arraynum++;}}}// Start new dictionary for each touch, with points and color- (void) touchesBegan:(NSSet *) touches withEvent:(UIEvent *) event{NSMutableArray *arrayPointsInStroke = [NSMutableArray array];NSMutableDictionary *dictStroke = [NSMutableDictionary dictionary];[dictStroke setObject:arrayPointsInStroke forKey:@"points"];[dictStroke setObject:[UIColor blackColor] forKey:@"color"];[dictStroke setObject:[NSNumber numberWithFloat:5] forKey:@"size"];CGPoint point = [[touches anyObject] locationInView:self];[arrayPointsInStroke addObject:NSStringFromCGPoint(point)];[self.arrayStrokes addObject:dictStroke];}// Add each point to points array- (void) touchesMoved:(NSSet *) touches withEvent:(UIEvent *) event{CGPoint point = [[touches anyObject] locationInView:self];CGPoint prevPoint = [[touches anyObject] previousLocationInView:self];NSMutableArray *arrayPointsInStroke = [[self.arrayStrokes lastObject] objectForKey:@"points"];[arrayPointsInStroke addObject:NSStringFromCGPoint(point)];CGRect rectToRedraw = CGRectMake(\ ((prevPoint.x>point.x)?point.x:prevPoint.x)-5,\ ((prevPoint.y>point.y)?point.y:prevPoint.y)-5,\ fabs(point.x-prevPoint.x)+2*5,\ fabs(point.y-prevPoint.y)+2*5\ );[self setNeedsDisplayInRect:rectToRedraw];}// Send over new trace when the touch ends- (void) touchesEnded:(NSSet *) touches withEvent:(UIEvent *) event{}- (void)dealloc{[arrayStrokes release]; [super dealloc];}@end
用法: 在xib中的view关联Canvas2D
- UIView上签名绘图示例
- UIView上签名绘图示例 .
- UIView自定义绘图
- UIView绘图之UIBezierPath
- UIView绘图与重绘
- UI01-UIView(示例代码)
- Quartz绘图使用示例
- 使用CGContext绘图示例
- python绘图示例
- UIView自定义绘图2--相应触摸事件
- UIView的layout(布局)与draw(绘图)
- [IOS]绘图、写字(重写UIView)
- 动画,(UIView上)
- ios--UIView动画简单示例
- 2.7 UIView动画简单示例
- PaintView 绘图 UIView使用UIImageView实现绘图效果
- tikz的函数绘图示例
- Matplotlib基础绘图函数示例
- s(秒)、ms(毫秒)、μs(微秒)、ns(纳秒)
- Linux下如何生成core dump文件
- oracle临时表空间的管理!
- 【经典】程序设计中的堆与栈
- 线性链表 查找 插入 删除 创建 merge
- UIView上签名绘图示例
- Android 常用工具类封装
- 制作第一个 iOS 应用程序
- 带滚动条VC 双缓存技术的
- Java 编程基础
- 指针与内存
- OS中的内存管理的发展历史
- 无法加入域,隶属于域,工作组选项为灰色
- hibernate缓存使用