#import "MainViewController.h"
#import "DoodleView.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
DoodleView *doodleView1 = [[DoodleView alloc] initWithFrame:CGRectMake(10, 30, 300, 390)];
doodleView1.tag = 1;
[self.view addSubview:doodleView1];
[doodleView1 release];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setTintColor:[UIColor purpleColor]];
button.backgroundColor = [UIColor grayColor];
button.frame = CGRectMake(260, 430, 40, 40);
[button setTitle:@"橡皮" forState:UIControlStateNormal];
[self.view addSubview:button];
[button addTarget:self action:@selector(button1Clicked:) forControlEvents:UIControlEventTouchUpInside];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
[button1 setTintColor:[UIColor purpleColor]];
button1.backgroundColor = [UIColor blackColor];
button1.frame = CGRectMake(220, 430, 40, 40);
[button1 setTitle:@"撤销" forState:UIControlStateNormal];
[self.view addSubview:button1];
[button1 addTarget:self action:@selector(button2Clicked:) forControlEvents:UIControlEventTouchUpInside];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
[button2 setTintColor:[UIColor blueColor]];
[button2 setTitle:@"红色" forState:UIControlStateNormal];
button2.backgroundColor = [UIColor redColor];
button2.frame = CGRectMake(180, 430, 40, 40);
[self.view addSubview:button2];
[button2 addTarget:self action:@selector(button3Clicked:) forControlEvents:UIControlEventTouchUpInside];
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeSystem];
[button3 setTintColor:[UIColor purpleColor]];
[button3 setTitle:@"黄色" forState:UIControlStateNormal];
button3.backgroundColor = [UIColor yellowColor];
button3.frame = CGRectMake(140, 430, 40, 40);
[self.view addSubview:button3];
[button3 addTarget:self action:@selector(button4Clicked:) forControlEvents:UIControlEventTouchUpInside];
UIButton *button4 = [UIButton buttonWithType:UIButtonTypeSystem];
[button4 setTintColor:[UIColor purpleColor]];
[button4 setTitle:@"蓝色" forState:UIControlStateNormal];
button4.backgroundColor = [UIColor blueColor];
button4.frame = CGRectMake(100, 430, 40, 40);
[self.view addSubview:button4];
[button4 addTarget:self action:@selector(button5Clicked:) forControlEvents:UIControlEventTouchUpInside];
UIButton *button5 = [UIButton buttonWithType:UIButtonTypeSystem];
[button5 setTintColor:[UIColor purpleColor]];
[button5 setTitle:@"绿色" forState:UIControlStateNormal];
button5.backgroundColor = [UIColor greenColor];
button5.frame = CGRectMake(60, 430, 40, 40);
[self.view addSubview:button5];
[button5 addTarget:self action:@selector(button6Clicked:) forControlEvents:UIControlEventTouchUpInside];
UIButton *button6 = [UIButton buttonWithType:UIButtonTypeSystem];
[button6 setTintColor:[UIColor blueColor]];
[button6 setTitle:@"紫色" forState:UIControlStateNormal];
button6.backgroundColor = [UIColor purpleColor];
button6.frame = CGRectMake(60, 430, 40, 40);
[self.view addSubview:button6];
[button6 addTarget:self action:@selector(button7Clicked:) forControlEvents:UIControlEventTouchUpInside];
UIButton *button7 = [UIButton buttonWithType:UIButtonTypeSystem];
[button7 setTintColor:[UIColor blackColor]];
[button7 setTitle:@"粉色" forState:UIControlStateNormal];
button7.backgroundColor = [UIColor magentaColor];
button7.frame = CGRectMake(20, 430, 40, 40);
[self.view addSubview:button7];
[button7 addTarget:self action:@selector(button8Clicked:) forControlEvents:UIControlEventTouchUpInside];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(120, 40, 80, 30)];
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor grayColor];
label.textColor = [UIColor whiteColor];
label.text = @"彩色画板";
[self.view addSubview:label];
}
- (void)button1Clicked:(UIButton *)button
{
DoodleView *doodleView = (DoodleView *)[self.view viewWithTag:1];
doodleView.color = [UIColor brownColor];
}
- (void)button2Clicked:(UIButton *)button1
{
DoodleView *doodleView1 = (DoodleView *)[self.view viewWithTag:1];
[doodleView1.lines removeLastObject];
[doodleView1 setNeedsDisplay];
}
- (void)button3Clicked:(UIButton *)button2
{
DoodleView *doodleView2 = (DoodleView *)[self.view viewWithTag:1];
doodleView2.color = [UIColor redColor];
}
- (void)button4Clicked:(UIButton *)button3
{
DoodleView *doodleView = (DoodleView *)[self.view viewWithTag:1];
doodleView.color = [UIColor yellowColor];
}
- (void)button5Clicked:(UIButton *)button4
{
DoodleView *doodleView = (DoodleView *)[self.view viewWithTag:1];
doodleView.color = [UIColor blueColor];
}
- (void)button6Clicked:(UIButton *)button5
{
DoodleView *doodleView = (DoodleView *)[self.view viewWithTag:1];
doodleView.color = [UIColor greenColor];
}
- (void)button7Clicked:(UIButton *)button6
{
DoodleView *doodleView = (DoodleView *)[self.view viewWithTag:1];
doodleView.color = [UIColor purpleColor];
}
- (void)button8Clicked:(UIButton *)button7
{
DoodleView *doodleView = (DoodleView *)[self.view viewWithTag:1];
doodleView.color = [UIColor magentaColor];
}
#import "DoodleView.h"
#import "MainViewController.h"
@implementation DoodleView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.lines = [NSMutableArray array];
self.backgroundColor = [UIColor brownColor];
self.color = [UIColor blackColor];
}
return self;
}
- (void)dealloc
{
[_lines release];
[super dealloc];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 在触摸到屏幕的时候 就创建一个数组
NSMutableArray *line = [NSMutableArray array];
NSMutableDictionary *dicColor = [NSMutableDictionary dictionaryWithObjectsAndKeys:self.color,@"color",
line,@"point", nil];
// 添加到大的数组中
[self.lines addObject:dicColor];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// 当手指移动的时候 把获取到的每一个点放入所在的数组中
NSMutableArray *line = [[self.lines lastObject] objectForKey:@"point"];
// 获取点
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
// 把结构体类型 转换为 对象类型
NSValue *point = [NSValue valueWithCGPoint:p];
// 将点放到数组中
[line addObject:point];
// 强制view调用自己的drawRect:方法
[self setNeedsDisplay];
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
// 获得view的绘制信息
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 20);
for (NSDictionary *lineDic in self.lines) {
NSMutableArray * line = [lineDic objectForKey:@"point"];
UIColor *color = [lineDic objectForKey:@"color"];
CGContextSetStrokeColorWithColor(context, color.CGColor);
if (0 == [line count]) {
continue;
}
for (int i = 0; i < line.count - 1; i++) {
// 按顺序取一个点 以及 之后的一个点
NSValue *point1 = [line objectAtIndex:i];
CGPoint p1 = [point1 CGPointValue];
NSValue *point2 = [line objectAtIndex:i + 1];
CGPoint p2 = [point2 CGPointValue];
CGContextMoveToPoint(context, p1.x, p1.y);
CGContextAddLineToPoint(context, p2.x, p2.y);
}
CGContextStrokePath(context); // 绘制
}
}
@end