DrawInView.h
DrawInView.h
1 #import <UIKit/UIKit.h> 2 3 @interface DrawInView : UIView 4 5 @property (nonatomic, retain) NSDictionary *pointsDictionary; 6 @property (nonatomic,assign) NSInteger beganCount; 7 @property (nonatomic, retain) NSMutableArray *pointsArray; 8 9 -(CGPoint) getCGPoint:(NSString *) key andPointIndex:(NSInteger) index; 10 -(void) clean; 11 12 @end
DrawInView.m
DrawInView.m
1 #import "DrawInView.h" 2 3 @implementation DrawInView 4 5 @synthesize pointsDictionary; 6 @synthesize beganCount; 7 @synthesize pointsArray; 8 9 - (id)initWithFrame:(CGRect)frame 10 { 11 self = [super initWithFrame:frame]; 12 if (self) { 13 self.backgroundColor=[UIColor whiteColor]; 14 15 //添加清除按钮 16 UIButton *cleanButton=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 17 [cleanButton setFrame:CGRectMake(280, 25, 35, 20)]; 18 [cleanButton setTitle:@"清除" forState:UIControlStateNormal]; 19 [cleanButton addTarget:self action:@selector(clean) forControlEvents:UIControlEventTouchUpInside]; 20 [self addSubview:cleanButton]; 21 } 22 return self; 23 } 24 25 -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 26 if (!self.pointsDictionary) { 27 self.pointsDictionary=[NSMutableDictionary dictionary]; 28 beganCount=0; 29 } 30 31 self.pointsArray=[NSMutableArray array]; 32 33 CGPoint firstPoint=[[touches anyObject] locationInView:self]; 34 NSValue *firstPointValue=[NSValue valueWithBytes:&firstPoint objCType:@encode(CGPoint)]; 35 36 [self.pointsArray addObject:firstPointValue]; 37 38 [self.pointsDictionary setValue:self.pointsArray forKey:[NSString stringWithFormat:@"%d",beganCount]]; 39 40 //累加 作为NSDictionary的Key 41 beganCount++; 42 } 43 44 -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 45 CGPoint point=[[touches anyObject] locationInView:self]; 46 [self.pointsArray addObject:[NSValue valueWithCGPoint:point]]; 47 48 [self setNeedsDisplay]; 49 } 50 51 //系统函数 52 - (void)drawRect:(CGRect)rect 53 { 54 if (!self.pointsDictionary) return; 55 56 //得到当前上下文 57 CGContextRef context=UIGraphicsGetCurrentContext(); 58 //设置绘图颜色 59 CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); 60 //设置画笔宽度 61 CGContextSetLineWidth(context, 4.0f); 62 63 64 for (int i=0; i<[self.pointsDictionary count]; i++) { 65 for (int j=0; j<[[self.pointsDictionary objectForKey:[NSString stringWithFormat:@"%d",i]] count]-1; j++) { 66 CGPoint firstPoint=[self getCGPoint:[NSString stringWithFormat:@"%d",i] andPointIndex:j]; 67 CGPoint secondPoint=[self getCGPoint:[NSString stringWithFormat:@"%d",i] andPointIndex:j+1]; 68 69 //以下内容是画线 70 CGContextMoveToPoint(context, firstPoint.x, firstPoint.y); 71 CGContextAddLineToPoint(context, secondPoint.x, secondPoint.y); 72 CGContextStrokePath(context); 73 } 74 75 } 76 } 77 78 //通过NSDiction中的key和NSArray中index得到CGPoint变量 79 -(CGPoint) getCGPoint:(NSString *) key andPointIndex:(NSInteger) index;{ 80 return [[[self.pointsDictionary objectForKey:key] objectAtIndex:index] CGPointValue]; 81 } 82 83 //点击清除按钮时触发该事件 84 -(void) clean{ 85 if (self.pointsDictionary) { 86 self.pointsDictionary=nil; 87 [self setNeedsDisplay]; 88 } 89 } 90 91 -(void) dealloc{ 92 [pointsDictionary release]; 93 [pointsArray release]; 94 95 [super dealloc]; 96 }
将上面的DrawInView(UIView的子类)添加到UIViewController子类中就可以了!
效果展示: