Drawing board, drawing board what brand is good

Source: Internet
Author: User

Drawing board, drawing board what brand is good

Knowledge Point: Save the information on the View to the album.

Drawing Graphics and routes

**************************************** **

# Import <UIKit/UIKit. h>

@ Interface FFFPaintView: UIView

@ Property (nonatomic, strong) UIColor * lineColor;

@ Property (nonatomic, assign) CGFloat lineWithed;

@ Property (nonatomic, copy) CGFloat (^ lineWithBlock )();

// Clear screen

-(Void) clearScreen;

// Rollback

-(Void) recede;

// Eraser

-(Void) eraser;

@ End

**************************************** **

# Import "FFFPaintView. h"

@ Interface FFFBezierPath: UIBezierPath

@ Property (nonatomic, strong) UIColor * lineColor;

@ End

@ Implementation FFFBezierPath

@ End

 

@ Interface FFFPaintView ()

@ Property (nonatomic, strong) NSMutableArray * paths;

@ End

@ Implementation FFFPaintView

 

-(NSMutableArray *) paths {

 

If (_ paths = nil ){

_ Paths = [NSMutableArray array];

}

Return _ paths;

}

// Clear screen

-(Void) clearScreen {

// Note that sometimes an error occurs when you remove all of them here. To prevent this, we recommend that you = nil, and then re-paint them.

[Self. paths removeAllObjects];

Self. paths = nil;

// Redraw

[Self setNeedsDisplay];

}

// Rollback

-(Void) recede {

[Self. paths removeLastObject];

// Redraw

[Self setNeedsDisplay];

}

// Eraser

-(Void) eraser {

Self. lineColor = self. backgroundColor;

// Redraw

[Self setNeedsDisplay];

}

 

-(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {

// Obtain the touch object

UITouch * touch = [touches anyObject];

// Obtain the finger position by touching the object

CGPoint point = [touch locationInView: touch. view];

// Create a path object

FFFBezierPath * path = [FFFBezierPath alloc] init];

[Path moveToPoint: point];

// Set the line color

Path. lineColor = self. lineColor;

 

If (self. lineWithBlock ){

Path. lineWidth = self. lineWithBlock ();

}

// The following method cannot obtain the value.

// Path. lineWidth = self. lineWithed;

// NSLog (@ "% f", self. lineWithed );

// Add path to the array

[Self. paths addObject: path];

}

 

-(Void) touchesMoved :( NSSet *) touches withEvent :( UIEvent *) event {

 

// Obtain the touch object

UITouch * touch = [touches anyObject];

CGPoint point = [touch locationInView: touch. view];

// Use the last path in the array to connect

[[Self. paths lastObject] addLineToPoint: point];

// Redraw

[Self setNeedsDisplay];

}

 

-(Void) drawRect :( CGRect) rect {

// Traverse all path Rendering

For (FFFBezierPath * path in self. paths ){

[Path. lineColor set];

// Set the style

[Path setLineCapStyle: kCGLineCapRound];

[Path setLineJoinStyle: kCGLineJoinRound];

// Rendering

[Path stroke];

// NSLog (@ "% @", path );

}

}

 

@ End

 

**************************************** **************************************** ****

 

# Import "ViewController. h"

# Import "FFFPaintView. h"

@ Interface ViewController ()

 

@ Property (weak, nonatomic) IBOutlet FFFPaintView * paintView;

 

@ Property (weak, nonatomic) IBOutlet UISlider * slider;

 

@ Property (weak, nonatomic) IBOutlet UIButton * firstButton;

 

@ End

 

@ Implementation ViewController

//-(IBAction) slider :( UISlider *) sender {

//

// Self. paintView. lineWithed = sender. value;

//

//}

 

-(IBAction) savePaint {

// Enable the image Context

Uigraphicsbeginimagecontextwitexceptions (self. paintView. bounds. size, NO, 0 );

// Obtain the current context

CGContextRef ctx = UIGraphicsGetCurrentContext ();

// Draw the style of the current view to the context

[Self. paintView. layer renderInContext: ctx];

// Save the image to the album and obtain the image from the context

UIImageWriteToSavedPhotosAlbum (UIGraphicsGetImageFromCurrentImageContext (), nil );

// Close the context

UIGraphicsEndImageContext ();

}

 

// Clear screen

-(IBAction) clearScreen {

[Self. paintView clearScreen];

}

// Rollback

-(IBAction) recede {

[Self. paintView recede];

}

// Eraser

-(IBAction) eraser {

[Self. paintView eraser];

}

 

-(Void) viewDidLoad {

[Super viewDidLoad];

[Self. paintView setLineWithBlock: ^ CGFloat {

Return self. slider. value;

}];

Self. paintView. lineColor = self. firstButton. backgroundColor;

}

 

-(IBAction) setColorSelected :( UIButton *) sender {

Self. paintView. lineColor = sender. backgroundColor;

}

@ End

 

 

**************************************** **************************************** ****

 

 

 

**************************************** **************************************** ****

 

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.