码迷,mamicode.com
首页 > 其他好文 > 详细

涂鸦-每次调setNeedsDisplay以后就会重新调用一次drawRect方法,每次调drawRect方法就会把之前画好的东西删掉

时间:2014-09-11 20:47:32      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:io   ar   strong   for   2014   sp   on   c   line   

//
//  WJView.m
//  zwj涂鸦
//
//  Created by zwj on 14-9-9.
//  Copyright (c) 2014年 zwj. All rights reserved.
//

#import "WJView.h"

@interface WJView()
@property(nonatomic,strong) NSMutableArray *allPaths;
@end

@implementation WJView

- (void)backto{
    [self.allPaths removeLastObject];
    [self setNeedsDisplay];
}
- (void)clearScreen{
    [self.allPaths removeAllObjects];
    [self setNeedsDisplay];
}

-(NSMutableArray *)allPaths{
    if (_allPaths == nil) {
        _allPaths = [NSMutableArray array];
    }
    return _allPaths;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:touch.view];
    NSMutableArray *aryCurrentP = [NSMutableArray array];
    [aryCurrentP addObject:[NSValue valueWithCGPoint:currentPoint]];
    [self.allPaths addObject:aryCurrentP];
    [self setNeedsDisplay];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint currentP = [touch locationInView:touch.view];
    NSMutableArray *ary = [self.allPaths lastObject];
    [ary addObject:[NSValue valueWithCGPoint:currentP]];
    [self setNeedsDisplay];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint currentP = [touch locationInView:touch.view];
    NSMutableArray *ary = [self.allPaths lastObject];
    [ary addObject:[NSValue valueWithCGPoint:currentP]];
    [self setNeedsDisplay];
}

/**
 * 绘制图形
 */
-(void)drawRect:(CGRect)rect{
    CGContextRef ref = UIGraphicsGetCurrentContext();
    for (NSMutableArray *ary in self.allPaths) {
        for (int i = 0; i < ary.count; i++) {
            if (i == 0) {
                CGPoint firstPoint = [ary[i] CGPointValue];
                CGContextMoveToPoint(ref, firstPoint.x, firstPoint.y);
            } else {
                CGPoint movePoint = [ary[i] CGPointValue];
                CGContextAddLineToPoint(ref, movePoint.x, movePoint.y);
            }
        }
    }
    CGContextSetLineJoin(ref, kCGLineJoinRound);
    CGContextSetLineWidth(ref, 5);
    CGContextStrokePath(ref);
}

@end

涂鸦-每次调setNeedsDisplay以后就会重新调用一次drawRect方法,每次调drawRect方法就会把之前画好的东西删掉

标签:io   ar   strong   for   2014   sp   on   c   line   

原文地址:http://www.cnblogs.com/xiaokanfengyu/p/3967129.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!