MainViewControl.m
#import "MainViewController.h"
#import "TouchView.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (void)dealloc
{
[super dealloc];
}
- (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.
TouchView * aView = [[TouchView alloc] initWithFrame:CGRectMake(20, 20, 280, 440)];
[aView setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:aView];
[aView release];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
TouchView.h
#import <UIKit/UIKit.h>
@interface TouchView : UIView
{
//所有在.h里声明的容器类变量,只能在.m方法里初始化
NSMutableArray * _pointArray;
//建立可变数组,存数第一笔所有点组成的线
NSMutableArray * _lineArray;
//存储 改变颜色
UIColor * _color;
}
@end
#import "TouchView.h"
@implementation TouchView
- (void) dealloc
{
[_pointArray release];
_pointArray= nil;
[_lineArray release];
_lineArray = nil;
[super dealloc];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
_pointArray = [[NSMutableArray alloc] init];
_lineArray = [[NSMutableArray alloc] init];
_color = [UIColor blackColor];
}
NSArray * arr = [NSArray arrayWithObjects:[UIColor redColor],[UIColor magentaColor],[UIColor yellowColor],[UIColor orangeColor],[UIColor greenColor],[UIColor cyanColor],[UIColor blueColor],[UIColor purpleColor],[UIColor brownColor],[UIColor grayColor],[UIColor blackColor],[UIColor clearColor], nil];
int i = 0;
for (UIColor * color in arr) {
UIButton * abutton = [[UIButton alloc] initWithFrame:CGRectMake(23*i+2, 417, 23, 23)];
[abutton setBackgroundColor:color];
[abutton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
if (color == [UIColor clearColor]) {
[abutton setTitle:@"c" forState:UIControlStateNormal];
[abutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//Button不能设置边框 ,所以要从底层调用,用.layer
[abutton.layer setBorderWidth:1];
//UIColor是对象类型,边框颜色需要CGColorRef类型的结构体,(只读的属性).所以后面要.CGColor
[abutton.layer setBorderColor:[UIColor redColor].CGColor];
}
[self addSubview:abutton];
[abutton release];
i++;
}
return self;
}
if ([button.currentTitle isEqualToString:@"c"]) {
[_lineArray removeLastObject];
}else{
_color = button.backgroundColor;
}
[self setNeedsDisplay];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//清空
// [_pointArray removeAllObjects];
//获得集合里的元素
UITouch * touch = [touches anyObject];
//获得起始点坐标
CGPoint point = [touch locationInView:self];
//结构体转换为对象,全部用NSValue CGPoint是结构体 转换为 对象
NSValue * value = [NSValue valueWithCGPoint:point];
//把元素存入数组
// [_pointArray addObject:value];
//局部变量要用便利构造器 不用释放 系统自动release
NSMutableArray * points = [NSMutableArray array];//创建火车头,也就是数组线
//放入第一个椅子
[points addObject:value];
//把车厢挂到火车头上
// [_lineArray addObject:points];
//其他的在move中添加
NSMutableDictionary * dic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:_color,@"color",points,@"point", nil];
//把字典传入数组里
[_lineArray addObject:dic];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch * touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
NSValue * value = [NSValue valueWithCGPoint:point];
// [_pointArray addObject:value];
//通过最后一个元素,取数组
// NSMutableArray * points = [_lineArray lastObject];
//把移动的坐标放到数组中
// [points addObject:value];
NSMutableDictionary * dic = [_lineArray lastObject];
NSMutableArray * points = [dic objectForKey:@"point"];
//把对象添加到数组里
[points addObject:value];
//调用下面绘画的方法
[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
}
// 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 (NSMutableDictionary * dic in _lineArray) {
//获得起始点坐标
NSValue * firstValue = [[dic objectForKey:@"point"] firstObject];
//转换
CGPoint firstPoint = [firstValue CGPointValue];
UIColor * c = [dic objectForKey:@"color"];
CGContextSetStrokeColorWithColor(context, c.CGColor);
//起始点
CGContextMoveToPoint(context, firstPoint.x, firstPoint.y);
for (NSValue * value in [dic objectForKey:@"point"]) {
CGPoint currentPoint = [value CGPointValue];
//添加到线上
CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y);
}
CGContextStrokePath(context);
}
}
@end