There are a lot of features can be added, note here to save slowly add it.
Code section
TouchView.h
#import <UIKit/UIKit.h>
@interface touchview:uiview
{
nsmutablearray *points;
Nsarray *points_all;
Cgcontextref context;
Uicolor *paint_clr;
}
@property (strong,nonatomic) Nsmutablearray *points;
@property (strong,nonatomic) Nsarray *points_all;
@property (strong,nonatomic) Uicolor *paint_clr;
@end
touchview.m
#import "TouchView.h" @implementation Touchview @synthesize points, Points_all, PAINT_CLR;
-(ID) initWithFrame: (CGRect) frame {self = [super Initwithframe:frame];
if (self) {//initialization code PAINT_CLR = [Uicolor Greencolor];
return self;
//Only override Drawrect:if for you perform custom drawing.
An empty implementation adversely affects performance during. -(void) DrawRect: (cgrect) Rect {//Drawing code if (!self.points) | | (Self.points.count < 2))
{return;
context = Uigraphicsgetcurrentcontext ();
Set Brush thickness cgcontextsetlinewidth (context, 5.0f);
Set the brush color//[[uicolor bluecolor]set];
[PAINT_CLR set];
Cgcontextsetstrokecolorwithcolor (context, [[Uicolor Bluecolor]cgcolor]); Cgcontextsetstrokecolorwithcolor (context, [PAINT_CLR Cgcolor]); Draw the previous trajectory for (int j = 0; J < [Self.points_all Count]; j +) {Nsmutablearray *points_tmp = [Points_
All Objectatindex:j]; for (int i = 0;i < [points_tmp count]-1;i++) {cgpoint point1 = [[Points_tmp Object
ATINDEX:I] Cgpointvalue];
Cgpoint Point2 = [[Points_tmp objectatindex: (i+1)] cgpointvalue];
Cgcontextmovetopoint (context, point1.x, point1.y);
Cgcontextaddlinetopoint (context, point2.x, point2.y);
Cgcontextstrokepath (context); ///Draw this for (int i=0 i < [self.points count]-1; i++) {Cgpoint
Point1 = [[Self.points objectatindex:i] cgpointvalue];
Cgpoint Point2 = [[Self.points objectatindex: (i+1)] cgpointvalue];
Cgcontextmovetopoint (context, point1.x, point1.y); CgcontextAddlinetopoint (context, point2.x, point2.y);
Cgcontextstrokepath (context);
Multi-point touch-(BOOL) ismultipletouchenabled {return no; //Create an array and record the initial ponit-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) Event {s
Elf.points = [Nsmutablearray array];
Cgpoint pt = [[Touches anyobject] locationinview:self];
[Self.points Addobject:[nsvalue valuewithcgpoint:pt]]; These points//call Setneedsdisplay are recorded in the//move process, triggering the invocation of the DrawRect Method-(void) touchesmoved: (Nsset *) touches withe
Vent: (Uievent *) event {Cgpoint pt = [[Touches anyobject] locationinview:self];
[Self.points Addobject:[nsvalue valuewithcgpoint:pt]];
[Self setneedsdisplay]; }-(void) touchesended: (Nsset *) touches withevent: (Uievent *) event {Nsmutablearray = [
Nsmutablearray alloc] initWithArray:self.points]; if (Self.points_all = = Nil) {Self.points_all = [[Nsarray alloc] initwithobjects:points_tmp, nil];
}else {self.points_all = [Self.points_all arraybyaddingobject:points_tmp]; }} @end