1、ViewController.h文件里
- (void)viewDidLoad
{
[super viewDidLoad];
TouchView *tv=[[TouchView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
tv.backgroundColor=[UIColor grayColor];
[self.view addSubview:tv];
}
2、TouchView.h文件
#import
@interface TouchView : UIView
@property(retain,nonatomic)NSMutableArray *lineArray,*color;
@property(retain,nonatomic)UISegmentedControl *sg;
@end
3、TouchView.m文件
#import "TouchView.h"
@implementation TouchView
@synthesize lineArray,color,sg;
- (id)initWithFrame:(CGRect)frame
{
NSLog(@"%s",__FUNCTION__);
self = [super initWithFrame:frame];
if (self) {
self.userInteractionEnabled=YES;//支持触摸
//初始化加button
UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame=CGRectMake(100, 400, 100, 50);
[button setTitle:@"撤销" forState:UIControlStateNormal];
[button addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpIns
ide];
[self addSubview:button];
self.lineArray=[[NSMutableArray alloc]init];//线数组创建初始化。写在这里是因为只创建一次就可以了
self.color=[[NSMutableArray alloc]init];
self.sg=[[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"红",@"蓝",@"绿", nil]];
[self addSubview:sg];
[sg addTarget:self action:@selector(colorchange:) forControlEvents:UIControlEventValueChang
ed];
}
[self setNeedsDisplay];
return self;
}
-(void)colorchange:(UISegmentedControl*)sender
{
}
//点击撤销函数
-(void)back:(id)sender
{
NSLog(@"%s",__FUNCTION__);
[self.lineArray removeLastObject];
[self.color removeLastObject];//撤销颜色
[self setNeedsDisplay];
}
//重写触摸启动
参数 touches代表触摸
event表示事件
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"%s",__FUNCTION__);
int sl=self.sg.selectedSegmentIndex;
if (sl==0)
{
[self.color addObject:[UIColor redColor]];
}
else if (sl==1)
{
[self.color addObject:[UIColor blueColor]];
}
else if (sl==2)
{
[self.color addObject:[UIColor greenColor]];
}
else
{
[self.color addObject:[UIColor yellowColor]];
}
UITouch *uc=[touches anyObject];//从set中取出一个值
CGPoint pt=[uc locationInView:self];//获取到触摸位置
NSValue *pp=[NSValue valueWithCGPoint:pt];//将触摸位置CGPoint转化为NSValue用于存储在oc容器中
NSMutableArray *pointArray=[[NSMutableArray alloc]init];//创建点数组
[pointArray addObject:pp];
[self.lineArray addObject:pointArray];
}
//重写触摸移动
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event//NSSet是集合
与数组的唯一区别是存的数据不能有重复的
{
NSLog(@"%s",__FUNCTION__);
UITouch *uc=[touches anyObject];//从set中取出一个值
CGPoint pt=[uc locationInView:self];//获取到触摸位置
CGPoint是c语言的结构体类型
NSValue *pp=[NSValue valueWithCGPoint:pt];//将触摸位置CGPoint转化为NSValue用于存储在oc容器中
将c语言的结构体转化为可用的oc对象
NSMutableArray *pointArray=[[NSMutableArray alloc]init];
pointArray=[self.lineArray lastObject];//代表最后一笔
[pointArray addObject:pp];//将移动中的经过的点存入数组
[self setNeedsDisplay];//重绘界面 最后调用drawRect:
}
//重写该方法 实现自定义界面绘图
- (void)drawRect:(CGRect)rect
{
// Drawing code
NSLog(@"%s",__FUNCTION__);
for (int j=0; j<[self.lineArray count]; j++)
{
CGContextRef ref=UIGraphicsGetCurrentCont
ext();//获取上下文
CGContextSetLineWidth(ref, 5);//设置画笔线宽5
CGColorRef cf=[[self.color objectAtIndex:j]CGColor];
CGContextSetStrokeColorW
ithColor(ref, cf);//将画笔设置为红色
NSMutableArray *pointArray=[self.lineArray objectAtIndex:j];
for (int i=0; i<[pointArray count]-1; i++)//-1防止越界
{
NSValue *vv=[pointArray objectAtIndex:i];
CGPoint pp=[vv CGPointValue];//起点
NSValue *vv2=[pointArray objectAtIndex:i+1];
CGPoint pp2=[vv2 CGPointValue];//终点
CGContextMoveToPoint(ref, pp.x, pp.y);
CGContextAddLineToPoint(ref, pp2.x, pp2.y);
}
CGContextStrokePath(ref);//具体执行画图动作
}
}
@end