iOS 画图-写字(代码笔记)

绘画视图

<span style="font-family: Arial, Helvetica, sans-serif;">//</span>
//  PaintView.h
//  DrawTest
//
//  Created by mac on 13-3-14.
//  Copyright (c) 2013年 dazhuan. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface PaintView : UIView
{
    NSMutableArray *lines;
    NSMutableArray *removedLines;
    NSMutableArray *currentLine;
    
    CGPoint mLastPoint;
}

/**
 *  撤销
 */
- (void) undo;
/**
 *  恢复
 */
- (void) restore;
/**
 *  画图
 *
 *  @param points 点
 */
- (void) drawLineWithPoints:(NSArray *)points;

@end

========================================

//
//  PaintView.m
//  DrawTest
//
//  Created by mac on 13-3-14.
//  Copyright (c) 2013年 dazhuan. All rights reserved.
//

#import "PaintView.h"

#define kNotationLineWidth  5.0
#define get

@implementation PaintView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        mLastPoint = CGPointMake(-1, -1);
        lines = [[NSMutableArray alloc] init];
        removedLines = [[NSMutableArray alloc] init];
    }
    return self;
}

//core drawing method
- (void)drawLine:(NSArray *)line inContext:(CGContextRef)ctx {
    
    if (line.count < 2) return;
    
    CGContextBeginPath(ctx);
    NSArray * point =  [line objectAtIndex:0];
    CGFloat startPointX = [[point objectAtIndex:0] floatValue];
    CGFloat startPointY = [[point objectAtIndex:1] floatValue];
    
    CGContextMoveToPoint(ctx, startPointX, startPointY);
    for (int i=1; i<line.count; ++i) {
        point = [line objectAtIndex:i];
        CGFloat px = [[point objectAtIndex:0] floatValue];
        CGFloat py = [[point objectAtIndex:1] floatValue];
        CGContextAddLineToPoint(ctx, px, py);
    }
    CGContextStrokePath(ctx);
}

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetAlpha(ctx, 1);
    CGContextSetRGBStrokeColor(ctx, 1, 0/255.0, 0, 0.5);
    CGContextSetLineWidth(ctx, kNotationLineWidth);
    CGContextSetLineJoin(ctx, kCGLineJoinRound);
    CGContextSetLineCap(ctx, kCGLineCapRound);
    
    for (NSArray *line in lines) {
        [self drawLine:line inContext:ctx];
    }
}

- (void) drawLineWithPoints:(NSArray *)points
{
    [self beginLine];
    for(NSArray *point in points) {
        float x = [[point objectAtIndex:0] floatValue];
        float y = [[point objectAtIndex:1] floatValue];
        [self addToCurrentLineWithPoint:x y:y];
    }
    [self setNeedsDisplay];
    [self endLine];
}

- (void)beginLine {
    currentLine = [[NSMutableArray alloc] init];
    [lines addObject:currentLine];
}

- (void)endLine {
    currentLine = nil;
}


- (void)addToCurrentLineWithPoint:(CGFloat)x y:(CGFloat)y {
    NSNumber * nx = [NSNumber numberWithFloat:x];
    NSNumber * ny = [NSNumber numberWithFloat:y];
    NSArray * point = [NSArray arrayWithObjects:nx, ny, nil];
    [currentLine addObject:point];
}

- (CGRect)rectRoundPoint:(CGPoint)point withSize:(CGFloat)size {
    return CGRectMake(point.x - size / 2, point.y - size / 2, size, size);
}

- (CGRect)rectNeedRefreshFromThisPoint:(CGPoint) point {
    if (mLastPoint.x == -1 || mLastPoint.y == -1) {
        return CGRectZero;
    }
    
    CGRect rc1 = [self rectRoundPoint:mLastPoint withSize:kNotationLineWidth];
    CGRect rc2 = [self rectRoundPoint:point withSize:kNotationLineWidth];
    
    return CGRectUnion(rc1, rc2);//Returns the smallest rectangle
}

- (void) undo {
    
    int lineCount = lines.count;
    if(lineCount == 0) return;
    
    int index = lineCount - 1;
    NSArray *lastLine = [lines objectAtIndex:index];
    [removedLines addObject:lastLine];
    [lines removeObjectAtIndex:index];
    
    [self setNeedsDisplay];
}

- (void) restore {
    
    int removedLinesCount = removedLines.count;
    if(removedLinesCount == 0) return;
    
    int index = removedLinesCount - 1;
    NSArray *lastLine = [removedLines objectAtIndex:index];
    [lines addObject:lastLine];
    [removedLines removeObjectAtIndex:index];
    
    [self setNeedsDisplay];
}


#pragma mark - Touch event handling

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch * touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    mLastPoint = point;
    
    [self beginLine];
    [self addToCurrentLineWithPoint:point.x y:point.y];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch * touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    CGRect roundRect = [self rectNeedRefreshFromThisPoint:point];
    mLastPoint = point;
    
    [self addToCurrentLineWithPoint:point.x y:point.y];
    [self setNeedsDisplayInRect:roundRect];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch * touch     = [touches anyObject];
    CGPoint point       = [touch locationInView:self];
    CGRect roundRect = [self rectNeedRefreshFromThisPoint:point];
    mLastPoint = CGPointMake(-1, -1);
    
    [self addToCurrentLineWithPoint:point.x y:point.y];
    
    if ([currentLine count] < 3)
    {
        [lines removeObject:currentLine];
    }
    
    [self endLine];
    [self setNeedsDisplayInRect:roundRect];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self touchesEnded:touches withEvent:event];
}

@end

===================================

使用

//
//  ViewController.h
//  DrawTest
//
//  Created by mac on 13-3-14.
//  Copyright (c) 2013年 dazhuan. All rights reserved.
//

#import <UIKit/UIKit.h>
@class PaintView;

@interface ViewController : UIViewController
{
    PaintView *paintView;
}
@end

//
//  ViewController.m
//  DrawTest
//
//  Created by mac on 13-3-14.
//  Copyright (c) 2013年 dazhuan. All rights reserved.
//

#import "ViewController.h"
#import "PaintView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    paintView = [[PaintView alloc] initWithFrame:self.view.frame];
    paintView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:paintView];
    
    UIButton *undo = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    undo.frame = CGRectMake(10, 10, 90, 35);
    [undo setTitle:@"undo" forState:UIControlStateNormal];
    [undo addTarget:self action:@selector(undo) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:undo];
    
    UIButton *restore = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    restore.frame = CGRectMake(10, 50, 90, 35);
    [restore setTitle:@"restore" forState:UIControlStateNormal];
    [restore addTarget:self action:@selector(restore) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:restore];
    
    UIButton *draw = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    draw.frame = CGRectMake(10, 90, 90, 35);
    [draw setTitle:@"draw" forState:UIControlStateNormal];
    [draw addTarget:self action:@selector(draw) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:draw];
    
}

- (void) draw {
    
    NSArray *point1 = [NSArray arrayWithObjects:[NSNumber numberWithFloat:150], [NSNumber numberWithFloat:150], nil];
    NSArray *point2 = [NSArray arrayWithObjects:[NSNumber numberWithFloat:170], [NSNumber numberWithFloat:180], nil];
    NSArray *point3 = [NSArray arrayWithObjects:[NSNumber numberWithFloat:220], [NSNumber numberWithFloat:190], nil];
    NSArray *point4 = [NSArray arrayWithObjects:[NSNumber numberWithFloat:240], [NSNumber numberWithFloat:250], nil];
    NSArray *points = [NSArray arrayWithObjects:point1, point2, point3, point4, nil];
    
    [paintView drawLineWithPoints:points];
}

- (void) undo {
    [paintView undo];
}

- (void) restore {
    [paintView restore];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
}

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值