iOS Development-Advanced ui-Small app drawing board

Source: Internet
Author: User

Drawing board
1. Build the interface (3 buttons, 1 view)

2. To rewrite Touchesbegan: And so on, you need to customize a view, create a new view, named Njview, and then set the view's class to Njview on the storyboard.

3. Overriding methods in NJVIEW.M

Start touching
-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
{
1. Get the finger corresponding to the Uitouch object
Uitouch *touch = [touches anyobject];
2. Get the position of finger touch via Uitouch object
Cgpoint startPoint = [Touch LocationInView:touch.view];
3. Create a decimal group to hold all points in the current path
Nsmutablearray *subpoints = [Nsmutablearray array];
4. Store the start point of your finger touch in a decimal group
[Subpoints Addobejct:[nsvalue Valuewithcgpoint:startpoint];
5. Storing a decimal group in a large array
[Self.totalpoints addobejct:subpoints];
}
Move
-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) event
{
1. Get the finger corresponding to the Uitouch object
Uitouch *touch = [touches anyobject];
2. Get the position of finger touch via Uitouch object
Cgpoint movepoint = [Touch LocationInView:touch.view];

3. Remove the decimal group for the current path from the large array
Nsmutablearray *subpoints = [self.totalpoints lastobject];
4. Store the points touched when the finger moves in a decimal group
[Subpoints Addobejct:[nsvalue Valuewithcgpoint:movepoint];
5. Call DrawRect: method to redraw the view
[Self setneedsdisplay];
}
Leave view (Stop touch)
-(void) touchesended: (Nsset *) touches withevent: (Uievent *) event
{
1. Get the finger corresponding to the Uitouch object
Uitouch *touch = [touches anyobject];
2. Get the position of finger touch via Uitouch object
Cgpoint endPoint = [Touch LocationInView:touch.view];
3. Remove the decimal group for the current path from the large array
Nsmutablearray *subpoints = [self.totalpoints lastobject];
4. Store the points you touch when you leave your finger in a decimal group
[Subpoints Addobejct:[nsvalue Valuewithcgpoint:endpoint];
5. Call DrawRect: method to redraw the view
[Self setneedsdisplay];
}

Draw Line
The points obtained by the above methods cannot be used directly in the following method, declare an array in the class extension to load the points, and then take out the
Define a large Array (large array holds decimal groups, each fractional group holds a straight line all points)
@property (Nonatomic,strong) Nsmutablearray *totalpoints;
-(Nsmutablearray *) totalpoints
{
if (_totalpoints = = nil) {
_totalpoints = [Nsmutablearray array];
}
return _totalpoints;
}
-(void) DrawRect: (cgrect) rect
{
1. Get context
Cgcontextref CTX = Uigraphicsgetcurrentcontext ();
Iterate through large arrays, take out all decimal groups (each decimal group represents a line segment)
For (Nsmutablearray *subpointarray in self.totalpoints) {
Traverse a decimal group to remove all points in a decimal group
for (int index = 0;index < subpointarray.count;index++) {
1. Remove each point in the decimal group
Cgpoint *point = Self.points[index] cgpointvalue];
2. Draw Segments
if (0 = = index) {
2.1. Set the start point of the segment
Cgcontextmovetopoint = (CTX,POINT.X,POINT.Y);
} else
{
2.2. Set the end point of a segment
Cgcontextaddlinetopoint (CTX,POINT.X,POINT.Y);
}
}
}

for (int index = 0;index < self.points.count;index++) {
Cgpoint *point = Self.points[index] cgpointvalue];
2. Draw Segments
if (0 = = index) {
2.1. Set the start point of the segment
Cgcontextmovetopoint = (CTX,POINT.X,POINT.Y);
}else
{
2.2. Set the end point of a segment
Cgcontextaddlinetopoint (CTX,POINT.X,POINT.Y);
}
}
Cgcontextsetlinecap (Ctx,kcglinecapround);
Cgcontextsetlinejoin (Ctx,kcglinejoinround);
Cgcontextsetlinewidth (ctx,10);
3. Rendering
Cgcontextstrokepath (CTX);
}

4. Monitor the Clear button, rewind button and save button, and listen to the View properties (Controller header file)
5. Provide methods in NJView.h (object oriented thinking, controller is only responsible for invoking)
-(void) ClearView;
-(void) Backview;
6. How to implement in NJVIEW.M
-(void) ClearView
{
[Self.totalpoints removeallobjects];
[Self setneedsdisplay];
}
-(void) Backview
{
[Self.totalpoints removelastobjects];
[Self setneedsdisplay];
}
7. Implementing the method in the controller implementation file
-(Ibaction) clearbtnclick{
[Self.customview ClearView];
}

-(ibaction) Backbtnclick {
[Self.customview Backview];
}

-(ibaction) Savebtnclick {

UIImage *newimage = [UIImage CaptureImageWithView:self.customView];

Save pictures to albums
Uiimagewritetosavedphotosalbum (Newimage,self, @selector (image:didFinishSavingWithError:contextInfo:), nil);
}

-(void) Image: (UIImage *) image didfinishsavingwitherror: (nserror *) error ContextInfo: (void *) ContextInfo
{
if (Error) {
[Mbprogresshud showerror:@ "Save failed, please check if the app has access to the album"];
} else {
[Mbprogresshud showsuccess:@ "save Success"];
}
}

8. Create a new class for Captureview
(used to, only need to send in the required view)
In the CaptureView.h
+ (UIImage *) Captureimagewithview: (UIView *) view;
In the CAPTUREVIEW.M
+ (UIImage *) Captureimagewithview: (UIView *) view
{
1. Create a bitmap context
Uigraphicsbeginimagecontext (view.frame.size);
2. Draw the layer of the view you want to save into the bitmap context
[View.layer Renderincontext:uigraphicsgetcurrentcontext ()];
3. Take out the drawn picture
UIImage *newimage = Uigraphicsgetimagefromcurrentimagecontext ();
return newimage;
}
PS: You can only import this class after the screenshot, and then call Captureimagewithview: Method. Code that is often used like this will be easy to wrap up.

iOS Development-Advanced ui-Small app drawing board

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.