ios 三种颜色画笔和橡皮擦的画图板demo

demo功能:三种颜色画笔和橡皮擦的画图板demo 【iphone 6.1 测试通过】

demo说明:项目中PaintView.m 是demo的画板部分,PaintView和三个颜色按钮添加到ViewController的view中。构成程序主界面。

demo截屏:


demo主要代码:PaintView.m 画板view部分

#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

demo下载地址:http://download.csdn.net/detail/donny_zhang/5572237


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值