Code section
TouchView.h
Copy Code code as follows:
#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
Copy Code code as follows:
#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 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 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 objectatindex: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 not supported
-(BOOL) ismultipletouchenabled
{
return NO;
}
Create an array and record the initial ponit
-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
{
Self.points = [Nsmutablearray array];
Cgpoint pt = [[Touches anyobject] locationinview:self];
[Self.points Addobject:[nsvalue valuewithcgpoint:pt]];
}
Record these points during the move
Calling Setneedsdisplay will trigger a call to the DrawRect method
-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) event
{
Cgpoint pt = [[Touches anyobject] locationinview:self];
[Self.points Addobject:[nsvalue valuewithcgpoint:pt]];
[Self setneedsdisplay];
}
-(void) touchesended: (Nsset *) touches withevent: (Uievent *) event
{
Nsmutablearray *points_tmp = [[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
ViewController.h
Copy Code code as follows:
#import <UIKit/UIKit.h>
@class Touchview;
@interface Viewcontroller:uiviewcontroller
{
Touchview *tv;
}
@end
Viewcontroller.m
Copy Code code as follows:
#import "ViewController.h"
#import "TouchView.h"
@interface Viewcontroller ()
@end
@implementation Viewcontroller
-(void) viewdidload
{
[Super Viewdidload];
Do no additional setup after loading the view, typically from a nib.
self.view.userInteractionEnabled = YES;
Touchview *TV = [[Touchview alloc]initwithframe:cgrectmake (0.0f, 0.0f, 260.0f, 260.0f)];
TV = [[Touchview alloc]initwithframe:self.view.frame];
Tv.backgroundcolor = [Uicolor blackcolor];
[Self.view ADDSUBVIEW:TV];
Uisegmentedcontrol *seg = [[Uisegmentedcontrol alloc] initwithitems:[@ "White Red Blue Green Yellow" componentsseparatedbystring:@ ""]];
Seg.segmentedcontrolstyle = Uisegmentedcontrolsegmentcenter;
Seg.tintcolor = [Uicolor blackcolor];
Seg.center = Cgpointmake (self.view.center.x, (self.view.bounds.size.height-seg.bounds.size.height));
[Self.view addsubview:seg];
[Seg addtarget:self Action: @selector (Colorchange:) forcontrolevents:uicontroleventvaluechanged];
}
-(void) viewdidunload
{
[Super Viewdidunload];
Release any retained subviews of the main view.
}
-(BOOL) Shouldautorotatetointerfaceorientation: (uiinterfaceorientation) interfaceorientation
{
Return (interfaceorientation!= uiinterfaceorientationportraitupsidedown);
}
-(void) Colorchange: (Uisegmentedcontrol *) seg
{
switch ([seg Selectedsegmentindex])
{
Case 0:
TV.PAINT_CLR = [Uicolor Whitecolor];
Break
Case 1:
TV.PAINT_CLR = [Uicolor Redcolor];
Break
Case 2:
TV.PAINT_CLR = [Uicolor Bluecolor];
Break
Case 3:
TV.PAINT_CLR = [Uicolor Greencolor];
Break
Case 4:
TV.PAINT_CLR = [Uicolor Yellowcolor];
Break
Default
Break
}
}
@end
Effect chart