Share a simple way to implement basic drawing board functionality under IOS _ios

Source: Internet
Author: User
Tags uikit

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.