TouchView.h
#import <UIKit/UIKit.h> @interface TouchView : UIView { NSMutableArray * _lineArray; } //初始化线条颜色 @property (nonatomic,retain)UIColor * pointColor; @end
TouchView.m
#import "TouchView.h" #define REDTAG 10 #define YELLOWTAG 11 #define ORANGETAG 12 #define GREENTAG 13 #define PURPLETAG 14 @implementation TouchView - (void)dealloc { [_lineArray release]; _lineArray = Nil; [_pointColor release]; _pointColor = Nil; [super dealloc]; } - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code _lineArray = [[NSMutableArray alloc]init]; //初始化颜色为黑色 _pointColor = [UIColor blackColor]; //删除按钮 UIButton * del = [[UIButton alloc]initWithFrame:CGRectMake(0, 330, 250, 20)]; [del setBackgroundColor:[UIColor brownColor]]; [del setTitle:@"撤销" forState:UIControlStateNormal]; [del addTarget:self action:@selector(delAction) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:del]; [del release]; //颜色按钮 UIButton * redButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 350, 50, 50)]; [redButton setBackgroundColor:[UIColor redColor]]; redButton.tag = REDTAG; [redButton addTarget:self action:@selector(colorAction:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:redButton]; [redButton release]; UIButton * yellowButton = [[UIButton alloc]initWithFrame:CGRectMake(50, 350, 50, 50)]; [yellowButton setBackgroundColor:[UIColor yellowColor]]; yellowButton.tag = YELLOWTAG; [yellowButton addTarget:self action:@selector(colorAction:)forControlEvents:UIControlEventTouchUpInside]; [self addSubview:yellowButton]; [yellowButton release]; UIButton * orangeButton = [[UIButton alloc]initWithFrame:CGRectMake(100, 350, 50, 50)]; [orangeButton setBackgroundColor:[UIColor orangeColor]]; orangeButton.tag = ORANGETAG; [orangeButton addTarget:self action:@selector(colorAction:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:orangeButton]; [orangeButton release]; UIButton * greenButton = [[UIButton alloc]initWithFrame:CGRectMake(150, 350, 50, 50)]; [greenButton setBackgroundColor:[UIColor greenColor]]; greenButton.tag = GREENTAG; [greenButton addTarget:self action:@selector(colorAction:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:greenButton]; [greenButton release]; UIButton * purpleButton = [[UIButton alloc]initWithFrame:CGRectMake(200, 350, 50, 50)]; [purpleButton setBackgroundColor:[UIColor purpleColor]]; purpleButton.tag = PURPLETAG; [purpleButton addTarget:self action:@selector(colorAction:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:purpleButton]; [purpleButton release]; } return self; } //按钮点击事件 - (void)colorAction:(id)sender { UIButton * button = (UIButton *)sender; switch (button.tag) { case REDTAG: { _pointColor = [UIColor redColor]; } break; case YELLOWTAG: { _pointColor = [UIColor yellowColor]; } break; case ORANGETAG: { _pointColor = [UIColor orangeColor]; } break; case GREENTAG: { _pointColor = [UIColor greenColor]; } break; case PURPLETAG: { _pointColor = [UIColor purpleColor]; } break; default: break; } } //4个方法进入.h中点UIView,点UIResponder进入找 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch * touch = [touches anyObject];//获取触摸动作 CGPoint point = [touch locationInView:self];//获得初始坐标 NSValue * value = [NSValue valueWithCGPoint:point];//结构体转换成对象;也能用于把对象转换成结构体,这是固定格式; NSMutableArray * points = [NSMutableArray array];//局部变量 遍历构造器不用释放 [points addObject:value];// NSMutableDictionary * dic = [NSMutableDictionary dictionary]; [dic setObject:points forKey:@"points"]; [dic setObject:_pointColor forKey:@"color"]; [_lineArray addObject:dic];//将字典放到数组线中 } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch * touch = [touches anyObject]; CGPoint ponit = [touch locationInView:self]; NSValue * value = [NSValue valueWithCGPoint:ponit]; // NSMutableArray * points = [_lineArray lastObject];//取出线数组里最后一个元素 // // [points addObject:value];//把移动坐标放到线数组里 NSMutableDictionary * dic = [_lineArray lastObject];// NSMutableArray * points = [dic objectForKey:@"points"];//从大字典里取出数组 [points addObject:value];//往小数组里添加所有点 [self setNeedsDisplay];//调用下面画图方法;系统不允许用drawRect,用setNeedsDisplay手动调用 } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { } - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { } //删除事件 - (void)delAction { [_lineArray removeLastObject];//移除最后一划 [self setNeedsDisplay]; } // 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(); for (NSDictionary * lineDic in _lineArray) { NSArray * line = [lineDic objectForKey:@"points"]; NSValue * firstValue = [line firstObject];//得到起始点 CGPoint firstPoint = [firstValue CGPointValue];//转换为Point类型;正常数据转换 UIColor * color = [lineDic objectForKey:@"color"]; CGContextSetStrokeColorWithColor(context, color.CGColor); CGContextMoveToPoint(context, firstPoint.x, firstPoint.y);//起始点 for (NSValue * value in [lineDic objectForKey:@"points"]) { CGPoint currentPonit = [value CGPointValue]; CGContextAddLineToPoint(context, currentPonit.x, currentPonit.y); } CGContextStrokePath(context); } } @end