I. Description
The program uses event processing mechanism and drawings to complete a simple graffiti board application, using the mouse to drag in the graffiti board can be doodle, click the Save to Album button, you can save the completed graffiti to the mobile phone album, click the Fallback button can back to a step, click the empty can let the Doodle board empty.
File structure and interface setup:
Second, code example
YYVIEWCONTROLLER.M file
Copy Code
1//
2//YYVIEWCONTROLLER.M
3//02-Artboard Program
4//
5//Created by Apple on 14-6-12.
6//Copyright (c) 2014 itcase. All rights reserved.
7//
8
9 #import "YYViewController.h"
Ten #import "YYView.h"
#import "Uiimage+yycaptureview.h"
#import "Mbprogresshud+nj.h"
13
@interface Yyviewcontroller ()
-(Ibaction) Clearonclick: (UIButton *) sender;
@property (Weak, nonatomic) Iboutlet Yyview *customview;
-(Ibaction) Backonclick: (UIButton *) sender;
(ibaction) Savebtnonclick: (UIButton *) sender;
19
20
@end
22
@implementation Yyviewcontroller
24
-(void) viewdidload
26 {
[Super Viewdidload];
28}
29
30
-(Ibaction) Clearonclick: (UIButton *) Sender {
32//Call cleanup method
[Self.customview ClearView];
34}
35
-(Ibaction) Backonclick: (UIButton *) Sender {
37//Call fallback method
[Self.customview Backview];
39}
40
(ibaction) Savebtnonclick: (UIButton *) Sender {
42//Call the method in the category to get the picture
UIImage *newimage = [UIImage YYcaptureImageWithView:self.customView];
44//Save the picture to your phone's album
Uiimagewritetosavedphotosalbum (NewImage, Self, @selector (image:didFinishSavingWithError:contextInfo:), nil);
46}
47
48//Handling Picture Save events
(void) Image: (UIImage *) image didfinishsavingwitherror: (nserror *) error ContextInfo: (void *) ContextInfo
50 {
51//Use a third-party framework to provide message alerts
if (Error) {
[Mbprogresshud showerror:@ "Doodle save failed, please check the permissions"];
}else
55 {
[Mbprogresshud showsuccess:@] Save success! "];
57}
58
59}
@end
Copy Code
YYView.h file
Copy Code
1//
2//YYView.h
3//02-Artboard Program
4//
5//Created by Apple on 14-6-12.
6//Copyright (c) 2014 itcase. All rights reserved.
7//
8
9 #import <UIKit/UIKit.h>
10
@interface Yyview:uiview
12
-(void) ClearView;
-(void) Backview;
@end
Copy Code
YYVIEW.M file
Copy Code
1//
2//YYVIEW.M
3//02-Artboard Program
4//
5//Created by Apple on 14-6-12.
6//Copyright (c) 2014 itcase. All rights reserved.
7//
8
9 #import "YYView.h"
10
11//Private extension
@interface Yyview ()
13/**
14 * Used to store all the path information
15 */
@property (Nonatomic,strong) Nsmutablearray *paths;
@end
@implementation Yyview
19
#pragma mark-lazy Loading
-(Nsmutablearray *) paths
22 {
if (_paths==nil) {
_paths=[nsmutablearray array];
25}
_paths return;
27}
28
29//Start Finger touch Event
(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
31 {
32//1. Get the Uitoch object for your finger
Uitouch *touch=[touches Anyobject];
34
35//2. Get the position of finger touch via Uitoch object
Cgpoint Starpoint=[touch LocationInView:touch.view];
37
38//3. Create a path when the user's finger is pressed
Uibezierpath *path=[uibezierpath Bezierpath];
40
41//Set the related properties of the path
[Path Setlinewidth:5];
[Path Setlinejoinstyle:kcglinejoinround];
[Path Setlinecapstyle:kcglinecapround];
45
46//4. Set the starting point of the current path
[Path Movetopoint:starpoint];
48
49//5. Adding a path to an array
[Self.paths Addobject:path];
51}
52
53//Finger Move Event
Touchesmoved-(void): (Nsset *) touches withevent: (Uievent *) event
55 {
56//1. Get the Uitoch object for your finger
Uitouch *touch=[touches Anyobject];
58//2. Get the position of finger touch via Uitoch object
Cgpoint Movepoint=[touch LocationInView:touch.view];
60//3. Remove the current path
Uibezierpath *currentpath=[self.paths Lastobject];
62//4. Set the end point of the current path
[Currentpath Addlinetopoint:movepoint];
64
65//5. Call the DrawRect method to redraw the view
[Self setneedsdisplay];
67}
68
69////lift up your finger
//-(void) touchesended: (Nsset *) touches withevent: (Uievent *) event
71//{
//[self touchesmoved:touches withevent:event];
73//}
74
75//Draw line
-(void) DrawRect: (cgrect) rect
77 {
78//Draw all segments according to the path
(Uibezierpath *path in self.paths) {
[Path stroke];
81}
82}
83
84/**
85 * Empty Panel
86 */
ClearView-(void)
88 {
89//Clear all paths
[Self.paths removeallobjects];
91//Call method to re-draw
[Self setneedsdisplay];
93}
94
95/**
96 * Fallback operation
97 */
98-(void) Backview
99 {
100//Remove the last element in the path array (last path)
101 [self.paths Removelastobject];
102//Re-drawing
103 [self setneedsdisplay];
104}
@end
Copy Code
Provides a classification that encapsulates functionality.
Uiimage+yycaptureview.h file
Copy Code
1//
2//Uiimage+yycaptureview.h
3//02-Artboard Program
4//
5//Created by Apple on 14-6-12.
6//Copyright (c) 2014 itcase. All rights reserved.
7//
8
9 #import <UIKit/UIKit.h>
10
@interface UIImage (Yycaptureview)
12
13/**
14 * This category provides a way to receive a view parameter and return a views
15 */
+ (UIImage *) Yycaptureimagewithview: (UIView *) view;
@end
Copy Code
UIIMAGE+YYCAPTUREVIEW.M file
Copy Code
1//
2//UIIMAGE+YYCAPTUREVIEW.M
3//02-Artboard Program
4//
5//Created by Apple on 14-6-12.
6//Copyright (c) 2014 itcase. All rights reserved.
7//
8
9 #import "Uiimage+yycaptureview.h"
10
@implementation UIImage (Yycaptureview)
12
+ (UIImage *) Yycaptureimagewithview: (UIView *) view
14 {
15//1. Creating a bitmap graphics context
Uigraphicsbeginimagecontext (view.frame.size);
17//2. The layer of the view that will be saved is drawn into the bitmap graphics context
[View.layer Renderincontext:uigraphicsgetcurrentcontext ()];
19//3. Remove the drawn picture
UIImage *newimage=uigraphicsgetimagefromcurrentimagecontext ();
21//4. Returns the captured picture
NewImage return;
23}
@end
iOS Development UI chapter-Event handling (implementation of a simple doodle board)