Demo of three-color paint brushes and erasers for ios

Source: Internet
Author: User

Demo function: three-color paint brush and eraser board demo [tested on iphone 6.1]

Demo Description: In the project, PaintView. m is the drawing board of the demo. PaintView and three color buttons are added to the view of ViewController. The main interface of the program.

Demo screenshot:

 

 


Demo main code: PaintView. m canvas view


[Csharp]
# Import "PaintView. h"
# Import <QuartzCore/QuartzCore. h>
 
@ Implementation PaintView
@ Synthesize paintColor = _ paintColor;
@ Synthesize erase;
-(Id) initWithFrame :( CGRect) frame
{
Self = [super initWithFrame: frame];
If (self ){
Self. layer. shadowColor = [UIColor blackColor]. CGColor;
Self. layer. shadowOpacity = 0.8;
Self. layer. shadowOffset = CGSizeMake (5, 5 );
Self. backgroundColor = [UIColor whiteColor];
Self. paintColor = [UIColor blackColor];
// Initialization code
LinesArray = [[NSMutableArray alloc] init];
UIPanGestureRecognizer * panGesture = [[UIPanGestureRecognizer alloc] initWithTarget: self
Action: @ selector (panGesture :)];
[Self addGestureRecognizer: panGesture];
[PanGesture release];
}
Return self;
}
 
-(Void) dealloc {
[LinesArray release];
[_ PaintColor release];
[Super dealloc];
}
 
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
-(Void) drawRect :( CGRect) rect
{
// Drawing code
CGContextRef context = UIGraphicsGetCurrentContext ();
CGContextSetLineWidth (context, 20 );
// NSLog (@ "color: % @", _ paintColor );
 
For (NSDictionary * lineDic in linesArray ){
UIColor * lineColor = [lineDic objectForKey: @ "color"];
CGContextSetStrokeColorWithColor (context, lineColor. CGColor );
CGMutablePathRef paintPath = CGPathCreateMutable ();
NSArray * linePointArray = [lineDic objectForKey: @ "line"];
For (NSInteger I = 0; I <linePointArray. count; I ++ ){
CGPoint point = [[linePointArray objectAtIndex: I] CGPointValue];
If (I = 0 ){
// CGContextMoveToPoint (context, point. x, point. y );
CGPathMoveToPoint (paintPath, NULL, point. x, point. y );
} Else {
// CGContextAddLineToPoint (context, point. x, point. y );
CGPathAddLineToPoint (paintPath, NULL, point. x, point. y );
}
}
CGContextAddPath (context, paintPath );
CGContextStrokePath (context );

If ([lineDic objectForKey: @ "eraseArray"]) {
// NSLog (@ "color: % @", lineColor );
NSMutableArray * eraseArray = [lineDic objectForKey: @ "eraseArray"];
CGContextSetStrokeColorWithColor (context, [UIColor whiteColor]. CGColor );
CGMutablePathRef paintPath = CGPathCreateMutable ();
For (NSInteger I = 0; I <eraseArray. count; I ++ ){
CGPoint point = [[eraseArray objectAtIndex: I] CGPointValue];
// NSLog (@ "erase point: % @", NSStringFromCGPoint (point ));
If (I = 0 ){
// CGContextMoveToPoint (context, point. x, point. y );
CGPathMoveToPoint (paintPath, NULL, point. x, point. y );
} Else {
// CGContextAddLineToPoint (context, point. x, point. y );
CGPathAddLineToPoint (paintPath, NULL, point. x, point. y );
}
}
CGContextAddPath (context, paintPath );
CGContextStrokePath (context );
}
}
}
 
 
-(Void) panGesture :( UIPanGestureRecognizer *) thePan {
CGPoint touchPoint = [thePan locationInView: self];
If (self. erase ){
If (thePan. state = UIGestureRecognizerStateChanged ){
For (NSMutableDictionary * lineDic in linesArray ){
NSMutableArray * linePointArray = [lineDic objectForKey: @ "line"];
For (NSInteger I = 0; I <linePointArray. count; I ++ ){
CGPoint point = [[linePointArray objectAtIndex: I] CGPointValue];
CGFloat distance = powf (point. x-touchPoint.x, point. y-touchPoint.y );
If (distance <20 ){
NSMutableArray * eraseArray;
If ([lineDic objectForKey: @ "eraseArray"]) {
EraseArray = [lineDic objectForKey: @ "eraseArray"];
} Else {
EraseArray = [NSMutableArray array];
}
[EraseArray addObject: [NSValue valueWithCGPoint: touchPoint];
[LineDic setObject: eraseArray forKey: @ "eraseArray"];
CGRect paintRect = CGRectMake (touchPoint. x-50, touchPoint. y-50, 100,100 );
[Self setNeedsDisplayInRect: paintRect];
// [Self setNeedsDisplay];
Continue;
}
}
}
}
// [Self eraseLine: currentLineDic erase: [thePan locationInView: self];
} Else {
If (thePan. state = UIGestureRecognizerStateBegan ){
NSMutableArray * currentLineArray = [NSMutableArray arrayWithObject: [NSValue valueWithCGPoint: touchPoint];
NSMutableDictionary * lineDic = [NSMutableDictionary dictionaryWithObjectsAndKeys: currentLineArray, @ "line", _ paintColor, @ "color", nil];
[LinesArray addObject: lineDic];
} Else if (thePan. state = UIGestureRecognizerStateChanged ){
NSMutableDictionary * lineDic = [linesArray lastObject];
NSMutableArray * currentLineArray = [lineDic objectForKey: @ "line"];
[CurrentLineArray addObject: [NSValue valueWithCGPoint: touchPoint];
CGRect paintRect = CGRectMake (touchPoint. x-50, touchPoint. y-50, 100,100 );
[Self setNeedsDisplayInRect: paintRect];
} Else if (thePan. state = UIGestureRecognizerStateEnded ){
 
}
}
}
@ 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.