UIView上签名绘图示例

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 code self.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 code CGContextRef 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 width for (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 joints UIBezierPath* 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


记得加入QuartzCore.framework



用法: 在xib中的view关联Canvas2D



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值