代码改变世界

简易涂鸦板

2015-10-19 13:58  lzrrey  阅读(93)  评论(0编辑  收藏  举报

delegate.h

@property (retain, nonatomic) UIWindow *window;

delegate.m

-(void)dealloc{

    [_window release];

    [super dealloc];

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    MainViewController *mainVC = [[[MainViewController alloc]init]autorelease];

    self.window.rootViewController = mainVC;

    return YES;

}

创建LineMode类

LineMode.h

@interface LineModel : NSObject

@property(nonatomic,retain)UIBezierPath *path; //使用贝塞尔曲线记录触摸轨迹

@property(nonatomic,retain)UIColor *color ; //线条颜色

@property(nonatomic,assign)CGFloat width; //线条宽度

@end

LineMode.m

-(void)dealloc{

    [_path release];

    [_color release];

    [super dealloc];

}

 创建属于UIView的PaintView

PaintView.h

@property(nonatomic,retain)NSMutableArray *allLines; //用于保存画板上所有线条对象.

@property(nonatomic,retain)UIColor *lineColor; //当前线条的颜色

@property(nonatomic,assign)CGFloat lineWidth; //当前线条的宽度

-(void)undoLastDrawing; //撤销上次绘制

-(void)allClean; //清空画板

 PaintView.m

#import "PaintView.h"

#import "LineModel.h"

@implementation PaintView

 -(void)dealloc{

    [_allLines release];

    [_lineColor release];

    [super dealloc];

}

 

-(NSMutableArray *)allLines{

    if (!_allLines) {

        self.allLines = [NSMutableArray array];

    }

    return _allLines;

}

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

    UITouch *aTouch = touches.anyObject;

    CGPoint startPoint = [aTouch locationInView:self.superview];

        //创建贝塞尔曲线对象,并设置曲线的起始点.

    UIBezierPath*bezierPath=[UIBezierPath bezierPath];

    [bezierPath moveToPoint:startPoint];

    //  创建线条模型对象 并保存线条的三个数据

    LineModel*line=[[[LineModel alloc]init]autorelease];

    line.path=bezierPath;

    line.color=self.lineColor;

    line.width=self.lineWidth;

    

    [self.allLines addObject:line];

    

}

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

    UITouch *aTouch = touches.anyObject;

    CGPoint currentPoint =[aTouch locationInView:self.superview];

    //获取对应的线条模型(在触摸移动的过程中,数组的最后一个对象对应当前正在绘制的曲线)

    LineModel *aLine = self.allLines.lastObject;

    [aLine.path addLineToPoint:currentPoint];

    [self setNeedsDisplay]; //调用此方法是通知视图,绘制界面,一旦调用此方法,系统就会自动调用drawRect:方法来绘制界面.

}

 

 

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

//     Drawing code

    for (LineModel *aLine in self.allLines) {

        //设置填充颜色

        [aLine.color setStroke];

        //设置绘制时的线条宽度

        [aLine.path setLineWidth:aLine.width];

        [aLine.path stroke]; //开始绘制曲线

    }

 

}

-(void)undoLastDrawing{

    [self.allLines removeLastObject];

    [self setNeedsDisplay]; //移除数组中的最后一条曲线对象,并重绘界面.

}

-(void)allClean{

    [self.allLines removeAllObjects];

    [self setNeedsDisplay];

}

@end

创建的MainViewController

MainViewController.m

-(void)loadView{

    PaintView *painView = [[PaintView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];

    painView.backgroundColor = [UIColor whiteColor];

    painView.lineWidth = 5;

    painView.lineColor = [UIColor blackColor];

    self.view = painView;

    [painView release];

}

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    //创建菜单按钮

    UIButton *menuButton = [UIButton buttonWithType:UIButtonTypeSystem];

    menuButton.frame = CGRectMake(CGRectGetWidth(self.view.bounds)-80, 40, 60, 40);

    [menuButton setTitle:@"菜单" forState:UIControlStateNormal];

    [menuButton addTarget:self action:@selector(handleMenuButtonAction:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:menuButton];

    

    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectZero];

    textField.tag = 123;

    [self.view addSubview:textField];

    [textField release];

    UIView *inputView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 60)];

    inputView.backgroundColor = [UIColor whiteColor];

    textField.inputView = inputView;

    [inputView release];

    

    NSArray *titles = @[@"减小",@"增大",@"撤销",@"清空",@"颜色"];

    CGFloat width = (CGRectGetWidth(self.view.bounds)-20*6)/5;

    CGFloat height = width;

    CGFloat originY = (60 - height) / 2;

    CGFloat originX = 20;

    for (int i = 0; i<titles.count; i++) {

        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

        button.tag = 100+i;

        button.frame = CGRectMake(originX+(width+originX)*i, originY, width, height);

        [button setTitle:titles[i] forState:UIControlStateNormal];

        button.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:0.6];

        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

        //设置按钮的角半径,为按钮宽度的一半

        button.layer.cornerRadius = width / 2.0;

        button.layer.masksToBounds = YES; //按指定的半径裁剪视图.

        [button addTarget:self action:@selector(handleButtonAction:) forControlEvents:UIControlEventTouchUpInside];

        [inputView addSubview:button];

    }

    

}

 

-(void)handleMenuButtonAction:(UIButton *)sender{

    //获取textField

    UITextField *textField = (UITextField *)[self.view viewWithTag:123];

    //判断如果textField当前是第一响应者,则撤销其第一响应者权限,否则让其成为第一响应者.

    if (textField.isFirstResponder) {

        [textField resignFirstResponder];

    }else{

        [textField becomeFirstResponder];//成为第一响应者

    }

}

-(void)handleButtonAction:(UIButton *)sender{

    PaintView *paintView = (PaintView *)self.view; //获取画板视图

    switch (sender.tag) {

        case 100:

            if (paintView.lineWidth > 2) {

                paintView.lineWidth -=1;

            }

            break;

        case 101:

            paintView.lineWidth +=1;

            break;

        case 102:

            [paintView undoLastDrawing];

            break;

        case 103:

            [paintView allClean];

            break;

        case 104:

            sender.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];

            //设置画笔颜色

            paintView.lineColor = sender.backgroundColor;

            break;

            

        default:

            break;

    }

}