Delegate.h
@property (Retain, nonatomic) UIWindow *window;
Delegate.m
-(void) dealloc{
[_window release];
[Super Dealloc];
}
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {
Self.window = [[[UIWindow alloc] Initwithframe:[[uiscreen Mainscreen] bounds]]autorelease];
Override point for customization after application launch.
Self.window.backgroundColor = [Uicolor Whitecolor];
[Self.window makekeyandvisible];
Mainviewcontroller *MAINVC = [[[Mainviewcontroller alloc]init]autorelease];
Self.window.rootViewController = MAINVC;
return YES;
}
Creating the Linemode Class
LineMode.h
@interface Linemodel:nsobject
@property (Nonatomic,retain) Uibezierpath *path; Record touch traces using Bezier curves
@property (Nonatomic,retain) Uicolor *color; Line Color
@property (nonatomic,assign) cgfloat width; Line width
@end
Linemode.m
-(void) dealloc{
[_path release];
[_color release];
[Super Dealloc];
}
Create a paintview that belongs to UIView
PaintView.h
@property (Nonatomic,retain) Nsmutablearray *alllines; Used to save all line objects on the artboard.
@property (Nonatomic,retain) Uicolor *linecolor; The color of the current line
@property (nonatomic,assign) cgfloat linewidth; The width of the current line
-(void) undolastdrawing; Undo Last Draw
-(void) Allclean; Empty artboard
paintview.m
#import "PaintView.h"
#import "LineModel.h"
@implementation Paintview
-(void) dealloc{
[_alllines release];
[_linecolor release];
[Super Dealloc];
}
-(Nsmutablearray *) alllines{
if (!_alllines) {
Self.alllines = [Nsmutablearray array];
}
return _alllines;
}
-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{
Uitouch *atouch = Touches.anyobject;
Cgpoint startPoint = [Atouch LocationInView:self.superview];
Creates a Bezier object and sets the starting point of the curve.
Uibezierpath*bezierpath=[uibezierpath Bezierpath];
[Bezierpath Movetopoint:startpoint];
Create a line model object and save three of the line's data
Linemodel*line=[[[linemodel Alloc]init]autorelease];
Line.path=bezierpath;
Line.color=self.linecolor;
Line.width=self.linewidth;
[Self.alllines Addobject:line];
}
-(void) touchesmoved: (Nsset *) touches withevent: (uievent *) event{
Uitouch *atouch = Touches.anyobject;
Cgpoint currentpoint =[atouch LocationInView:self.superview];
Gets the corresponding line model (the last object of the array corresponds to the curve currently being drawn during the touch move)
Linemodel *aline = Self.allLines.lastObject;
[Aline.path Addlinetopoint:currentpoint];
[Self setneedsdisplay]; Calling this method is the notification view, drawing the interface, and once this method is called, the system automatically calls the DrawRect: method to draw the interface.
}
Only override Drawrect:if perform custom drawing.
An empty implementation adversely affects performance during animation.
-(void) DrawRect: (cgrect) Rect {
Drawing Code
For (Linemodel *aline in Self.alllines) {
Set Fill Color
[Aline.color Setstroke];
Set the line width when drawing
[Aline.path SetLineWidth:aLine.width];
[Aline.path stroke]; Start Drawing Curves
}
}
-(void) undolastdrawing{
[Self.alllines Removelastobject];
[Self setneedsdisplay]; Removes the last Curve object in the array and redraws the interface.
}
-(void) allclean{
[Self.alllines removeallobjects];
[Self setneedsdisplay];
}
@end
Created by Mainviewcontroller
Mainviewcontroller.m
-(void) loadview{
Paintview *painview = [[Paintview alloc]initwithframe:[[uiscreen Mainscreen]bounds]];
Painview.backgroundcolor = [Uicolor Whitecolor];
Painview.linewidth = 5;
Painview.linecolor = [Uicolor blackcolor];
Self.view = Painview;
[Painview release];
}
-(void) Viewdidload {
[Super Viewdidload];
Do any additional setup after loading the view.
Create a menu button
UIButton *menubutton = [UIButton Buttonwithtype:uibuttontypesystem];
Menubutton.frame = CGRectMake (Cgrectgetwidth (self.view.bounds)-80, 40, 60, 40);
[Menubutton settitle:@ "menu" forstate:uicontrolstatenormal];
[Menubutton addtarget:self Action: @selector (handlemenubuttonaction:) forcontrolevents:uicontroleventtouchupinside ];
[Self.view Addsubview:menubutton];
Uitextfield *textfield = [[Uitextfield Alloc]initwithframe:cgrectzero];
Textfield.tag = 123;
[Self.view Addsubview:textfield];
[TextField release];
UIView *inputview = [[UIView alloc]initwithframe:cgrectmake (0, 0, cgrectgetwidth (self.view.bounds), 60)];
Inputview.backgroundcolor = [Uicolor Whitecolor];
Textfield.inputview = Inputview;
[Inputview release];
Nsarray *titles = @[@ "Decrease", @ "Increase", @ "undo", @ "empty", @ "color"];
CGFloat width = (cgrectgetwidth (self.view.bounds) -20*6)/5;
CGFloat height = width;
CGFloat originy = (60-height)/2;
CGFloat Originx = 20;
for (int i = 0; i<titles.count; i++) {
UIButton *button = [UIButton Buttonwithtype:uibuttontypesystem];
Button.tag = 100+i;
Button.frame = CGRectMake (originx+ (Width+originx) *i, originy, width, height);
[Button settitle:titles[i] forstate:uicontrolstatenormal];
Button.backgroundcolor = [Uicolor colorwithred:arc4random ()%256/255.0 green:arc4random ()%256/255.0 blue:arc4random ( )%256/255.0 alpha:0.6];
[Button Settitlecolor:[uicolor Whitecolor] forstate:uicontrolstatenormal];
Sets the corner radius of the button, which is half the width of the button
Button.layer.cornerRadius = width/2.0;
Button.layer.masksToBounds = YES; Crops the view by the specified radius.
[Button addtarget:self action: @selector (handlebuttonaction:) forcontrolevents:uicontroleventtouchupinside];
[Inputview Addsubview:button];
}
}
-(void) Handlemenubuttonaction: (UIButton *) sender{
Get TextField
Uitextfield *textfield = (Uitextfield *) [Self.view viewwithtag:123];
Determine if TextField is currently the first responder, revoke its first responder permission, or make it the first responder.
if (Textfield.isfirstresponder) {
[TextField Resignfirstresponder];
}else{
[TextField becomefirstresponder];//becomes the first responder
}
}
-(void) Handlebuttonaction: (UIButton *) sender{
Paintview *paintview = (Paintview *) Self.view; Get artboard View
Switch (Sender.tag) {
Case 100:
if (Paintview.linewidth > 2) {
Paintview.linewidth-=1;
}
Break
Case 101:
Paintview.linewidth +=1;
Break
Case 102:
[Paintview undolastdrawing];
Break
Case 103:
[Paintview Allclean];
Break
Case 104:
Sender.backgroundcolor = [Uicolor colorwithred:arc4random ()%256/255.0 green:arc4random ()%256/255.0 blue:arc4random ( )%256/255.0 Alpha:1];
Set Brush color
Paintview.linecolor = Sender.backgroundcolor;
Break
Default
Break
}
}
Simple Graffiti Board