UIView上签名绘图示例 .

本文介绍了一款iPad上的绘图软件,特别关注一款由小朋友开发的涂鸦应用,并提供了示例代码。代码展示了如何在Canvas2D视图中实现基本的绘画功能,包括初始化、绘制线条、响应触摸事件等。
 

ipad上绘图的软件不错吧,以有有一个小朋友写了一个涂鸦软件,大买呀。这儿有一个示例。

Canvas2D.h

  1. #import <UIKit/UIKit.h>   
  2. #import <QuartzCore/QuartzCore.h>   
  3.   
  4.   
  5. @interface Canvas2D : UIView {  
  6.     NSMutableArray* arrayStrokes;  
  7.     UIColor *currentColor;  
  8.       
  9. }  
  10.   
  11. @property (retain) NSMutableArray* arrayStrokes;  
  12.   
  13. @end  

Canvas2D.m

  1. #import "Canvas2D.h"   
  2.   
  3.   
  4. @implementation Canvas2D  
  5.   
  6. @synthesize arrayStrokes;  
  7.   
  8. - (id)initWithFrame:(CGRect)frame  
  9. {  
  10.     self = [super initWithFrame:frame];  
  11.     if (self) {  
  12.         // Initialization code   
  13.         self.arrayStrokes = [NSMutableArray array];  
  14.     }  
  15.     return self;  
  16. }  
  17.   
  18. - (id)initWithCoder:(NSCoder *)aDecoder {  
  19.     if ((self = [super initWithCoder:aDecoder])) {  
  20.         self.arrayStrokes = [NSMutableArray array];  
  21.     }  
  22.       
  23.     return self;  
  24. }  
  25.   
  26.   
  27. // Only override drawRect: if you perform custom drawing.  
  28. // An empty implementation adversely affects performance during animation.  
  29. - (void)drawRect:(CGRect)rect  
  30. {  
  31.     // Drawing code   
  32.     CGContextRef context = UIGraphicsGetCurrentContext();  
  33.       
  34.     CGContextSetLineWidth(context, 2.0);  
  35.       
  36.     CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();  
  37.       
  38.     CGFloat components[] = {0.0, 0.0, 1.0, 1.0};  
  39.     CGColorRef color = CGColorCreate(colorspace, components);  
  40.       
  41.     CGContextSetStrokeColorWithColor(context, color);  
  42.       
  43.     CGContextMoveToPoint(context, 0, 0);  
  44.     CGContextAddLineToPoint(context, 300, 400);  
  45.       
  46.     CGContextStrokePath(context);  
  47.     CGColorSpaceRelease(colorspace);  
  48.     CGColorRelease(color);  
  49.       
  50.     if (self.arrayStrokes)  
  51.     {  
  52.         int arraynum = 0;  
  53.         // each iteration draw a stroke  
  54.         // line segments within a single stroke (path) has the same color and line width  
  55.         for (NSDictionary *dictStroke in self.arrayStrokes)  
  56.         {  
  57.             NSArray *arrayPointsInstroke = [dictStroke objectForKey:@"points"];  
  58.             UIColor *color = [dictStroke objectForKey:@"color"];  
  59.             float size = [[dictStroke objectForKey:@"size"] floatValue];  
  60.             [color set];        // equivalent to both setFill and setStroke  
  61.               
  62.             // draw the stroke, line by line, with rounded joints  
  63.             UIBezierPath* pathLines = [UIBezierPath bezierPath];  
  64.             CGPoint pointStart = CGPointFromString([arrayPointsInstroke objectAtIndex:0]);  
  65.             [pathLines moveToPoint:pointStart];  
  66.             for (int i = 0; i < (arrayPointsInstroke.count - 1); i++)  
  67.             {  
  68.                 CGPoint pointNext = CGPointFromString([arrayPointsInstroke objectAtIndex:i+1]);  
  69.                 [pathLines addLineToPoint:pointNext];  
  70.             }  
  71.             pathLines.lineWidth = size;  
  72.             pathLines.lineJoinStyle = kCGLineJoinRound;  
  73.             pathLines.lineCapStyle = kCGLineCapRound;  
  74.             [pathLines stroke];  
  75.               
  76.             arraynum++;  
  77.         }  
  78.     }  
  79.   
  80. }  
  81.   
  82. // Start new dictionary for each touch, with points and color  
  83. - (void) touchesBegan:(NSSet *) touches withEvent:(UIEvent *) event  
  84. {  
  85.     NSMutableArray *arrayPointsInStroke = [NSMutableArray array];  
  86.     NSMutableDictionary *dictStroke = [NSMutableDictionary dictionary];  
  87.     [dictStroke setObject:arrayPointsInStroke forKey:@"points"];  
  88.     [dictStroke setObject:[UIColor blackColor] forKey:@"color"];  
  89.     [dictStroke setObject:[NSNumber numberWithFloat:5] forKey:@"size"];  
  90.       
  91.     CGPoint point = [[touches anyObject] locationInView:self];  
  92.     [arrayPointsInStroke addObject:NSStringFromCGPoint(point)];  
  93.       
  94.     [self.arrayStrokes addObject:dictStroke];  
  95. }  
  96.   
  97. // Add each point to points array  
  98. - (void) touchesMoved:(NSSet *) touches withEvent:(UIEvent *) event  
  99. {  
  100.     CGPoint point = [[touches anyObject] locationInView:self];  
  101.     CGPoint prevPoint = [[touches anyObject] previousLocationInView:self];  
  102.     NSMutableArray *arrayPointsInStroke = [[self.arrayStrokes lastObject] objectForKey:@"points"];  
  103.     [arrayPointsInStroke addObject:NSStringFromCGPoint(point)];  
  104.       
  105.     CGRect rectToRedraw = CGRectMake(\  
  106.                                      ((prevPoint.x>point.x)?point.x:prevPoint.x)-5,\  
  107.                                      ((prevPoint.y>point.y)?point.y:prevPoint.y)-5,\  
  108.                                      fabs(point.x-prevPoint.x)+2*5,\  
  109.                                      fabs(point.y-prevPoint.y)+2*5\  
  110.                                      );  
  111.     [self setNeedsDisplayInRect:rectToRedraw];  
  112. }  
  113.   
  114. // Send over new trace when the touch ends  
  115. - (void) touchesEnded:(NSSet *) touches withEvent:(UIEvent *) event  
  116. {  
  117. }  
  118.   
  119.   
  120.   
  121. - (void)dealloc  
  122. {  
  123.     [arrayStrokes release];  
  124.     [super dealloc];  
  125. }  
  126.   
  127. @end  


记得加入QuartzCore.framework



用法: 在xib中的view关联Canvas2D

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值