(source code) Cat Learn iOS (34) Implementation of QUARTZ2D drawing board of UI

Source: Internet
Author: User
Tags uikit

Cat Share, must boutique

Original articles, welcome reprint. Reprint Please specify: Sanayu's Blog
Address: http://blog.csdn.net/u013357243?viewmode=contents
Source: http://download.csdn.net/detail/u013357243/8666923

Effect:

Code: Nyview

NYView.h

////  NYView.h//  画画板////  Created by apple on 15-5-6.//  Copyright (c) 2015年 znycat. All rights reserved.//#import <UIKit/UIKit.h>@interface NYView : UIView- (void)clearView;- (void)backView;@end

nyview.m

////NYVIEW.M//drawing board////Created by Apple on 15-5-6.//Copyright (c) 2015 Znycat. All rights reserved.//#import "NYView.h"  @interface nyview ()@property(nonatomic,Strong)Nsmutablearray*paths;@end @implementation nyview - (Nsmutablearray*) paths{if(_paths = =Nil) {_paths = [NsmutablearrayArray]; }return_paths;}//Start Touch- (void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{//1. Get the finger corresponding to the Uitouch objectUitouch *touch = [touches anyobject];//2. Get the position of finger touch via Uitouch object    CgpointStartPoint = [Touch Locationinview:touch. View];//3. Create a path when the user's finger is pressedUibezierpath *path = [Uibezierpath Bezierpath];//3.1 Set the related properties of the path[Path Setlinejoinstyle:kcglinejoinround];    [Path Setlinecapstyle:kcglinecapround]; [Path Setlinewidth:Ten];//4. Set the starting point of the current path[Path Movetopoint:startpoint];//5. Add the path to the array[ Self. PathsAddobject:path];}//Mobile- (void) touchesmoved: (Nsset *) touches withevent: (uievent *) event{//1. Get the finger corresponding to the Uitouch objectUitouch *touch = [touches anyobject];//2. Get the position of finger touch via Uitouch object    CgpointMovepoint = [Touch Locationinview:touch. View];//3. Remove the current pathUibezierpath *currentpaht = [ Self. PathsLastobject];//4. Set the end point of the current path[Currentpaht Addlinetopoint:movepoint];//6. Call the DrawRect method to return to the view[ SelfSetneedsdisplay];}//Leave view (Stop touch)- (void) touchesended: (Nsset *) touches withevent: (uievent *) event{[ SelfTouchesmoved:touches Withevent:event];/*//1. Get finger corresponding to Uitouch object Uitouch *touch = [touches anyobject];     2. Get the position of the finger touch through the Uitouch object cgpoint endPoint = [Touch LocationInView:touch.view];     3. Remove the current path Uibezierpath *currentpaht = [Self.paths lastobject];     4. Set the end point of the current path [Currentpaht Addlinetopoint:endpoint];     6. Call the DrawRect method to return to the view [self setneedsdisplay]; */}//Draw line- (void) DrawRect: (CGRect) rect{[[UicolorRedcolor] set];//Edge group to draw all segments     for(Uibezierpath *path in Self. Paths) {[path stroke]; }}- (void) clearview{[ Self. PathsRemoveallobjects]; [ SelfSetneedsdisplay];} - (void) backview{[ Self. PathsRemovelastobject]; [ SelfSetneedsdisplay];}@end
Nyviewcontroller
////NYVIEWCONTROLLER.M//drawing board////Created by Apple on 15-5-6.//Copyright (c) 2015 Znycat. All rights reserved.//#Import "NYViewController.h"#Import "NYView.h"#Import "Mbprogresshud+nj.h"#Import "Uiimage+captureview.h"@interfaceNyviewcontroller ()/** * Clear screen * *-(ibaction) Clearbtnclick;/** * Fallback * *-(ibaction) Backbtnclick;/** * Save * *-(ibaction) Savebtnclick;@property(Weak, nonatomic) Iboutlet Nyview *customview;@end@implementationnyviewcontroller-(ibaction) Clearbtnclick {[Self.customview clearView];} -(Ibaction) Backbtnclick {[Self.customview backview];} -(Ibaction) Savebtnclick {UIImage *newimage = [UIImage CaptureImageWithView:self.customView];//4. Save to albumUiimagewritetosavedphotosalbum (NewImage, Self,@selector(image:didFinishSavingWithError:contextInfo:), nil);} - (voidImage: (UIImage *) image didfinishsavingwitherror: (nserror *) Error ContextInfo: (void*) contextinfo{if(Error) {[Mbprogresshud showerror:@"Save Failed"]; }Else{[Mbprogresshud showsuccess:@"saved successfully"]; }}@end

Others can be seen in the source code, these two are the most important.

(source code) Cat Learn iOS (34) Implementation of QUARTZ2D drawing board of UI

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.