UI 画图四(画板画图)作业

来源:互联网 发布:北极熊团 知乎 编辑:程序博客网 时间:2024/05/21 17:19
画图四(画板画图)作业

//phd

#define Screen_width [UIScreen mainScreen].bounds.size.width

#define Screen_height self.view.frame.size.height


#define MY_COLOR(RED,GREEN,BLUE,ALPHA) [UIColor colorWithRed:RED/255.0 green:GREEN/255.0 blue:BLUE/255.0 alpha:ALPHA]


#define Mycolor [UIColor colorWithRed:148/255.0 green:0/255.0 blue:170/255.0 alpha:1]


#define TOP_CONSTRAINT(ITEM,TOITEM,CONSTANT) [NSLayoutConstraint constraintWithItem:ITEM attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:TOITEM attribute:NSLayoutAttributeTop multiplier:1 constant:CONSTANT]


#define LEFT_CONSTRAINT(ITEM,TOITEM,CONSTANT) [NSLayoutConstraint constraintWithItem:ITEM attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:TOITEM attribute:NSLayoutAttributeLeading multiplier:1 constant:CONSTANT]


#define WIDTH_CONSTRAINT(ITEM,CONSTANT) [NSLayoutConstraint constraintWithItem:ITEM attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1 constant:CONSTANT]


#define HEIGHT_CONSTRAINT(ITEM,CONSTANT) [NSLayoutConstraint constraintWithItem:ITEM attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1 constant:CONSTANT]


#define ChangeColor @"changeColor"



//viewController.h

//

//  ViewController.h

//  UIdraw画图

//

//  Created by ibokan on 16/1/18.

//  Copyright © 2016谭其伟. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface ViewController : UIViewController


@property(nonatomic,strong)UILabel *label;



@end



//ViewController.m

//

//  ViewController.m

//  UIdraw画图

//

//  Created by ibokan on 16/1/18.

//  Copyright © 2016谭其伟. All rights reserved.

//


#import "ViewController.h"

#import "AppDraw.h"

#import "DrawRef.h"


@interface ViewController ()<textprotocol>

{

    

    UISlider *slider1;

    DrawRef *drawRef;

    UILabel *_label1;

    UILabel *_label2;

    

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    

    AppDraw *appDrawView = [[AppDraw alloc]initWithFrame:CGRectMake(0,0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];

    

//    [self.view addSubview:appDrawView];

    

    NSLog(@"%f,%f",[UIScreen mainScreen].bounds.size.width,self.view.frame.size.height);

    

    //设置一个view,view里边放上图片,再设置view的大小=屏幕大小,再添加到视图里边(画板下面).

    UIImageView *backView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"shu"]];

    backView.frame = CGRectMake(0, 20, Screen_width, Screen_height-(Screen_height*11/184)-20);

    [self.view addSubview:backView];

    

    //布置画板

    drawRef = [[DrawRef alloc]initWithFrame:CGRectMake(0,20, Screen_width, Screen_height-(Screen_height*11/184)-20)];

    drawRef.delegate = self;

    [self.view addSubview:drawRef];

    

    

    NSArray *array = @[@"画笔大小",@"颜色设置",@"后退",@"清空",@"分享"];

    //button布局

    for (int i =0; i <5; i++) {

        

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

        button.backgroundColor = [UIColor grayColor];

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

        //设置字体大小

        button.titleLabel.font = [UIFont systemFontOfSize:16];

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

        //添加按下时颜色变化

        [button addTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchDown];

        

        button.translatesAutoresizingMaskIntoConstraints = NO;

        [self.view addSubview:button];

        

        //距离顶部都一样

        NSLayoutConstraint *constraint1 = TOP_CONSTRAINT(button, self.view, Screen_height-(Screen_height*11/184));

        

        

        if (i < 3) {

            

            NSLayoutConstraint *constraint2 = LEFT_CONSTRAINT(button, self.view, ((Screen_width+1)/4)*i);

            [self.view addConstraint:constraint2];

            


        }

        else

        {

            NSLayoutConstraint *constraint2 = LEFT_CONSTRAINT(button, self.view, ((Screen_width+1)/6)*(i+1));

            [self.view addConstraint:constraint2];


        }

        

        //前面两块宽度

        if (i < 2) {

            NSLayoutConstraint *constraint3 = WIDTH_CONSTRAINT(button, (Screen_width+1)/4-1);

            [self.view addConstraint:constraint3];

        }

        else   //后面几块的宽度

        {

            NSLayoutConstraint *constraint3 = WIDTH_CONSTRAINT(button, (Screen_width+1)/6-1);

            [self.view addConstraint:constraint3];

            

        }


        

        //高度都一样

        NSLayoutConstraint *constraint4 = HEIGHT_CONSTRAINT(button, Screen_height*11/184);

        

        [self.view addConstraint:constraint1];

        

        [self.view addConstraint:constraint4];

        

        if ( i == 0) {

            button.tag = 101;

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

            

            //添加一个label2

            _label2 = [[UILabelalloc]init];

            _label2.font = [UIFontsystemFontOfSize:10];

            _label2.textAlignment =2;

            

            

            _label2.layer.cornerRadius =6;

            _label2.layer.masksToBounds =YES;

            _label2.translatesAutoresizingMaskIntoConstraints =NO;

            [button addSubview:_label2];

            

            NSLayoutConstraint *constraint1 = TOP_CONSTRAINT(_label2, button, 2);

            NSLayoutConstraint *constraint2 = LEFT_CONSTRAINT(_label2, button, (Screen_width+1)/4-24);

            NSLayoutConstraint *constraint3 = WIDTH_CONSTRAINT(_label2, 22);

            NSLayoutConstraint *constraint4 = HEIGHT_CONSTRAINT(_label2, 12);

            [button addConstraint:constraint1];

            [button addConstraint:constraint2];

            [button addConstraint:constraint3];

            [button addConstraint:constraint4];

            

            

        }

        if (i == 1) {

            button.tag = 102;

            [button addTarget:selfaction:@selector(setColorAction:)forControlEvents:UIControlEventTouchUpInside];

        }

        if (i == 2) {

            button.tag = 103;

            [button addTarget:selfaction:@selector(backAction:)forControlEvents:UIControlEventTouchUpInside];

            

            //添加一个小label

            _label1 = [[UILabelalloc]init];


            NSLog(@"drawRef.count1 = %f",drawRef.count1);

            

            _label1.font = [UIFontsystemFontOfSize:10];

            _label1.textAlignment =NSTextAlignmentCenter;

            _label1.layer.cornerRadius =6;

            _label1.layer.masksToBounds =YES;

            _label1.translatesAutoresizingMaskIntoConstraints =NO;

            [button addSubview:_label1];

            

            NSLayoutConstraint *constraint1 = TOP_CONSTRAINT(_label1, button, 2);

            NSLayoutConstraint *constraint2 = LEFT_CONSTRAINT(_label1, button, (Screen_width+1)/6-17);

            NSLayoutConstraint *constraint3 = WIDTH_CONSTRAINT(_label1, 14);

            NSLayoutConstraint *constraint4 = HEIGHT_CONSTRAINT(_label1, 12);

            [button addConstraint:constraint1];

            [button addConstraint:constraint2];

            [button addConstraint:constraint3];

            [button addConstraint:constraint4];

            

            

        }

        if (i == 3) {

            button.tag = 104;

            [button addTarget:selfaction:@selector(deleteAllAction:)forControlEvents:UIControlEventTouchUpInside];

        }

        if (i == 4) {

            button.tag = 105;

            [button addTarget:selfaction:@selector(shareButtonAction:)forControlEvents:UIControlEventTouchUpInside];

        }

        

        

    }

    

    

    slider1 = [[UISlideralloc]initWithFrame:CGRectMake(10,Screen_height-(Screen_height*11/184)-32,Screen_width-20,31)];

    [self.viewaddSubview:slider1];

    //设置最大值

    slider1.maximumValue =20;

    //设置最小值

    slider1.minimumValue =1;

    //设置一开始时的值

    slider1.value =5;

    //设置拨块条的颜色

    slider1.minimumTrackTintColor = [UIColorcyanColor];

    //设置拨块的颜色

    slider1.maximumTrackTintColor = [UIColorwhiteColor];

    //添加方法

    [slider1addTarget:selfaction:@selector(slider1Action:)forControlEvents:UIControlEventValueChanged]; //值变的时候

    

    slider1.hidden =YES;

    

    _label2.text = [NSStringstringWithFormat:@"%.1f",slider1.value];

    _label2.textColor = [UIColorwhiteColor];

    


    

    for ( int j =0; j < 9; j++) {

        

        UIButton *button1 = [UIButtonbuttonWithType:UIButtonTypeCustom];

        button1.layer.borderWidth =1;

        button1.layer.borderColor = [UIColorwhiteColor].CGColor;

        button1.translatesAutoresizingMaskIntoConstraints =NO;

        [self.viewaddSubview:button1];

        button1.tag = 106 + j;

        button1.hidden = YES;

        

        NSLayoutConstraint *constraint1 =TOP_CONSTRAINT(button1, self.view,Screen_height-(Screen_height*11/184)-(Screen_width*2/25+3));

        NSLayoutConstraint *constraint2 = LEFT_CONSTRAINT(button1, self.view, (Screen_width*2/25+5)*j+Screen_width*2/25+2);

        NSLayoutConstraint *constraint3 = WIDTH_CONSTRAINT(button1, Screen_width*2/25);

        NSLayoutConstraint *constraint4 = HEIGHT_CONSTRAINT(button1, Screen_width*2/25);

        

        [self.viewaddConstraint:constraint1];

        [self.viewaddConstraint:constraint2];

        [self.viewaddConstraint:constraint3];

        [self.viewaddConstraint:constraint4];

        

        //看看能不能循环方法(不能)

//        [button1 addTarget:self action:@selector(button1Action:) forControlEvents:UIControlEventTouchUpInside];

        

        

        if (j == 0) {

            button1.backgroundColor = [UIColorredColor];

            button1.tag = 106;

            [button1 addTarget:selfaction:@selector(button1Action106:)forControlEvents:UIControlEventTouchUpInside];

        }

        if (j == 1) {

            button1.backgroundColor = [UIColororangeColor];

            button1.tag = 107;

            [button1 addTarget:selfaction:@selector(button1Action107:)forControlEvents:UIControlEventTouchUpInside];

        }

        if (j == 2) {

            button1.backgroundColor = [UIColoryellowColor];

            button1.tag = 108;

            [button1 addTarget:selfaction:@selector(button1Action108:)forControlEvents:UIControlEventTouchUpInside];

        }

        if (j == 3) {

            button1.backgroundColor = [UIColorgreenColor];

            button1.tag = 109;

            [button1 addTarget:selfaction:@selector(button1Action109:)forControlEvents:UIControlEventTouchUpInside];

        }

        if (j == 4) {

            button1.backgroundColor = [UIColorblueColor];

            button1.tag = 110;

            [button1 addTarget:selfaction:@selector(button1Action110:)forControlEvents:UIControlEventTouchUpInside];

        }

        if (j == 5) {

            button1.backgroundColor = [UIColorcyanColor];

            button1.tag = 111;

            [button1 addTarget:selfaction:@selector(button1Action111:)forControlEvents:UIControlEventTouchUpInside];

        }

        if (j == 6) {

            button1.backgroundColor = [UIColorpurpleColor];

            button1.tag = 112;

            [button1 addTarget:selfaction:@selector(button1Action112:)forControlEvents:UIControlEventTouchUpInside];

        }

        if (j == 7) {

            button1.backgroundColor = [UIColorblackColor];

            button1.tag = 113;

            [button1 addTarget:selfaction:@selector(button1Action113:)forControlEvents:UIControlEventTouchUpInside];

        }

        if (j == 8) {

            button1.backgroundColor = [UIColorwhiteColor];

            button1.tag = 114;

            [button1 addTarget:selfaction:@selector(button1Action114:)forControlEvents:UIControlEventTouchUpInside];

        }

    }

    

    

    

    

    

}


-(void)touchDown:(UIButton *)sender

{

    sender.alpha = 0.5;

}


-(void)setLingWidthAction:(UIButton *)sender

{

    //透明度回1

    sender.alpha = 1;

    

    slider1.hidden = !slider1.hidden;

    

    for (int j =0; j < 9; j++) {  //重新定义j,然后取到上面的button.tag

        UIButton *button = (UIButton *)[self.viewviewWithTag:106+j];

        button.hidden = YES;   //当点画笔大小按键时,颜色块收回

        //点其他按键时,色块按键收回的同时边框变回初始状态

        button.layer.borderWidth =1.0;

        button.layer.borderColor = [UIColorwhiteColor].CGColor;

    }


    

    

    

    

}



-(void)setColorAction:(UIButton *)sender

{

    //透明度回1

    sender.alpha = 1;

    //当点颜色按钮时,slider1收回

    slider1.hidden =YES;

    

    for (int j =0; j < 9; j++) {  //重新定义j,然后取到上面的button.tag

        UIButton *button = (UIButton *)[self.viewviewWithTag:106+j];

        button.hidden = !button.hidden;

        button.layer.borderWidth =1.0;

        button.layer.borderColor = [UIColorwhiteColor].CGColor;

    }

    

    

    

    

    

}




-(void)backAction:(UIButton *)sender

{

    //透明度回1

    sender.alpha = 1;

    

    //当点颜色按钮时,slider1收回

    slider1.hidden =YES;

    

    for (int j =0; j < 9; j++) {  //重新定义j,然后取到上面的button.tag

        UIButton *button = (UIButton *)[self.viewviewWithTag:106+j];

        button.hidden = YES;   //当点画笔大小按键时,颜色块收回

        button.layer.borderWidth =1.0;

        button.layer.borderColor = [UIColorwhiteColor].CGColor;

    }

    

    //当点击@"后退"键时发出一个通知,通知内容为@"deleteLastOne",发出者是自己,传送值为空(否则为字典)

    [[NSNotificationCenterdefaultCenter] postNotificationName:@"deleteLastOne"object:selfuserInfo:nil];

    

    

    

    

}


-(void)deleteAllAction:(UIButton *)sender

{

    //透明度回1

    sender.alpha = 1;

    

    //当点颜色按钮时,slider1收回

    slider1.hidden = YES;

    

    for (int j =0; j < 9; j++) {  //重新定义j,然后取到上面的button.tag

        UIButton *button = (UIButton *)[self.view viewWithTag:106+j];

        button.hidden = YES;   //当点画笔大小按键时,颜色块收回

        button.layer.borderWidth = 1.0;

        button.layer.borderColor = [UIColor whiteColor].CGColor;

    }

    

    //当点击@"后退"键时发出一个通知,通知内容为@"deleteLastOne",发出者是自己,传送值为空(否则为字典)

    [[NSNotificationCenter defaultCenter] postNotificationName:@"deleteAll" object:self userInfo:nil];

    

}




-(void)shareButtonAction:(UIButton *)sender

{

    //透明度回1

    sender.alpha = 1;

    

    //当点颜色按钮时,slider1收回

    slider1.hidden = YES;

    

    for (int j =0; j < 9; j++) {  //重新定义j,然后取到上面的button.tag

        UIButton *button = (UIButton *)[self.view viewWithTag:106+j];

        button.hidden = YES;   //当点画笔大小按键时,颜色块收回

        button.layer.borderWidth = 1.0;

        button.layer.borderColor = [UIColor whiteColor].CGColor;

    }

    

    

}



-(void)slider1Action:(UISlider *)sender

{

    NSLog(@"slider1.value = %f",slider1.value);

    

    //通知的方法

//    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeLineWidth" object:self userInfo:@{@"lineWith":[NSNumber numberWithFloat:slider1.value]}];

    

    

    //属性传值法(drawRef1对象对应类里的属性linewidth= slider1.value)

//    然后到touchbegin里初始化并写到字典里,然后在画板里读取字典

    //属性传值时注意drawRef这个对象创建的画板与传值的画板是同一个的,这里不要用到又新建一个画板,这样不行的

    drawRef.linewidth = slider1.value;

    

    _label2.text = [NSString stringWithFormat:@"%.1f",slider1.value];

    _label2.textColor = [UIColor blackColor];

    _label2.backgroundColor = [UIColor whiteColor];

    

    

    

}



//颜色块方法

/*

-(void)button1Action:(UIButton *)sender

{

    int j;

    UIButton *button = (UIButton *)[self.view viewWithTag:105 + j];

    switch (button.tag) {

        case 105:

            //发出通知ChangeColor

            [[NSNotificationCenter defaultCenter] postNotificationName:ChangeColor object:self userInfo:@{@"color":[UIColor redColor]}];

                

            break;

        case 106:

            //发出通知

            [[NSNotificationCenter defaultCenter] postNotificationName:ChangeColor object:self userInfo:@{@"color":[UIColor orangeColor]}];

            break;

        case 107:

            //发出通知

            [[NSNotificationCenter defaultCenter] postNotificationName:ChangeColor object:self userInfo:@{@"color":[UIColor yellowColor]}];

            break;

        case 108:

            //发出通知

            [[NSNotificationCenter defaultCenter] postNotificationName:ChangeColor object:self userInfo:@{@"color":[UIColor greenColor]}];

            break;

        case 109:

            //发出通知

            [[NSNotificationCenter defaultCenter] postNotificationName:ChangeColor object:self userInfo:@{@"color":[UIColor blueColor]}];

            break;

        case 110:

            //发出通知

            [[NSNotificationCenter defaultCenter] postNotificationName:ChangeColor object:self userInfo:@{@"color":[UIColor cyanColor]}];

            break;

        case 111:

            //发出通知

            [[NSNotificationCenter defaultCenter] postNotificationName:ChangeColor object:self userInfo:@{@"color":[UIColor purpleColor]}];

            break;

        case 112:

            //发出通知

            [[NSNotificationCenter defaultCenter] postNotificationName:ChangeColor object:self userInfo:@{@"color":[UIColor blackColor]}];

            break;

        case 113:

            //发出通知

            [[NSNotificationCenter defaultCenter] postNotificationName:ChangeColor object:self userInfo:@{@"color":[UIColor whiteColor]}];

            break;

                

        default:

            break;

    }

 

}

*/



-(void)button1Action106:(UIButton *)sender

{

    for (int j =0; j <9; j++) {

        UIButton *button1 = (UIButton *)[self.view viewWithTag:106+j];

        if (j == 0 ) {

            button1.layer.borderWidth = 2;

            button1.layer.borderColor = [UIColor yellowColor].CGColor;

        }

        else

        {

            button1.layer.borderWidth = 1;

            button1.layer.borderColor = [UIColor whiteColor].CGColor;

        }

    }

    //发送一个通知(ChangeColor),发送者是自己,使用字典::color,:redColor;

    [[NSNotificationCenter defaultCenter] postNotificationName:ChangeColor object:self userInfo:@{@"color":[UIColor redColor]}];

}


-(void)button1Action107:(UIButton *)sender

{

    for (int j =0; j <9; j++) {

        UIButton *button1 = (UIButton *)[self.view viewWithTag:106+j];

        if (j == 1 ) {

            button1.layer.borderWidth = 2;

            button1.layer.borderColor = [UIColor yellowColor].CGColor;

        }

        else

        {

            button1.layer.borderWidth = 1;

            button1.layer.borderColor = [UIColor whiteColor].CGColor;

        }

    }


    

    [[NSNotificationCenter defaultCenter] postNotificationName:ChangeColor object:self userInfo:@{@"color":[UIColor orangeColor]}];

}


-(void)button1Action108:(UIButton *)sender

{

    for (int j =0; j <9; j++) {

        UIButton *button1 = (UIButton *)[self.view viewWithTag:106+j];

        if (j == 2 ) {

            button1.layer.borderWidth = 2;

            button1.layer.borderColor = [UIColor redColor].CGColor;

        }

        else

        {

            button1.layer.borderWidth = 1;

            button1.layer.borderColor = [UIColor whiteColor].CGColor;

        }

    }



    

    [[NSNotificationCenter defaultCenter] postNotificationName:ChangeColor object:self userInfo:@{@"color":[UIColor yellowColor]}];

}


-(void)button1Action109:(UIButton *)sender

{

    for (int j =0; j <9; j++) {

        UIButton *button1 = (UIButton *)[self.view viewWithTag:106+j];

        if (j == 3 ) {

            button1.layer.borderWidth = 2;

            button1.layer.borderColor = Mycolor.CGColor;

        }

        else

        {

            button1.layer.borderWidth = 1;

            button1.layer.borderColor = [UIColor whiteColor].CGColor;

        }

    }


    

    [[NSNotificationCenter defaultCenter] postNotificationName:ChangeColor object:self userInfo:@{@"color":[UIColor greenColor]}];

}


-(void)button1Action110:(UIButton *)sender

{

    for (int j =0; j <9; j++) {

        UIButton *button1 = (UIButton *)[self.view viewWithTag:106+j];

        if (j == 4 ) {

            button1.layer.borderWidth = 2;

            button1.layer.borderColor = [UIColor yellowColor].CGColor;

        }

        else

        {

            button1.layer.borderWidth = 1;

            button1.layer.borderColor = [UIColor whiteColor].CGColor;

        }

    }



    

    [[NSNotificationCenter defaultCenter] postNotificationName:ChangeColor object:self userInfo:@{@"color":[UIColor blueColor]}];

}



-(void)button1Action111:(UIButton *)sender

{

    for (int j =0; j <9; j++) {

        UIButton *button1 = (UIButton *)[self.view viewWithTag:106+j];

        if (j == 5 ) {

            button1.layer.borderWidth = 2;

            button1.layer.borderColor = Mycolor.CGColor;

        }

        else

        {

            button1.layer.borderWidth = 1;

            button1.layer.borderColor = [UIColor whiteColor].CGColor;

        }

    }



    

    [[NSNotificationCenter defaultCenter] postNotificationName:ChangeColor object:self userInfo:@{@"color":[UIColor cyanColor]}];

}


-(void)button1Action112:(UIButton *)sender

{

    for (int j =0; j <9; j++) {

        UIButton *button1 = (UIButton *)[self.view viewWithTag:106+j];

        if (j == 6 ) {

            button1.layer.borderWidth = 2;

            button1.layer.borderColor = [UIColor yellowColor].CGColor;

        }

        else

        {

            button1.layer.borderWidth = 1;

            button1.layer.borderColor = [UIColor whiteColor].CGColor;

        }

    }



    

    [[NSNotificationCenter defaultCenter] postNotificationName:ChangeColor object:self userInfo:@{@"color":[UIColor purpleColor]}];

}


-(void)button1Action113:(UIButton *)sender

{

    for (int j =0; j <9; j++) {

        UIButton *button1 = (UIButton *)[self.view viewWithTag:106+j];

        if (j == 7 ) {

            button1.layer.borderWidth = 2;

            button1.layer.borderColor = [UIColor whiteColor].CGColor;

        }

        else

        {

            button1.layer.borderWidth = 1;

            button1.layer.borderColor = [UIColor whiteColor].CGColor;

        }

    }



    

    [[NSNotificationCenter defaultCenter] postNotificationName:ChangeColor object:self userInfo:@{@"color":[UIColor blackColor]}];

}


-(void)button1Action114:(UIButton *)sender

{

    for (int j =0; j <9; j++) {

        UIButton *button1 = (UIButton *)[self.view viewWithTag:106+j];

        if (j == 8 ) {

            button1.layer.borderWidth = 2;

            button1.layer.borderColor = Mycolor.CGColor;

        }

        else

        {

            button1.layer.borderWidth = 1;

            button1.layer.borderColor = [UIColor whiteColor].CGColor;

        }

    }



    

    [[NSNotificationCenter defaultCenter] postNotificationName:ChangeColor object:self userInfo:@{@"color":[UIColor whiteColor]}];

}


-(void)transferString:(NSString *)string

{

    _label1.text = string;

    if ([_label1.text isEqualToString:@"0"]) {

        _label1.backgroundColor = [UIColor grayColor];

        _label1.textColor = [UIColor whiteColor];

    }

    else

    {

        _label1.backgroundColor = [UIColor whiteColor];

        _label1.textColor = [UIColor blackColor];

    }

    

    

    

}





- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end



//类AppDraw:UIView

//

//  AppDraw.m

//  UIdraw画图

//

//  Created by ibokan on 16/1/18.

//  Copyright © 2016谭其伟. All rights reserved.

//


#import "AppDraw.h"


@implementation AppDraw



// 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,1);

    //设置画笔填充颜色

    CGContextSetRGBFillColor(context,1, 0,0, 1);

    //设置画笔描边颜色

    CGContextSetRGBStrokeColor(context,1, 1,1, 1);

    

    //开始画画

    //画圆

    //'画圆',先定义一个文字属性字典

    NSDictionary *dic = [NSDictionarydictionaryWithObjectsAndKeys:

                         [UIFontsystemFontOfSize:16.0],NSFontAttributeName,[UIColorwhiteColor],NSForegroundColorAttributeName,nil];

    

    [@"画圆:"drawInRect:CGRectMake(20,30, 80, 20)withAttributes:dic];//用这种方法在画布上画文字,这是最新的

    

    //声明要画什么

    CGContextAddArc(context, 100, 40, 30,0, 2*M_PI,0);

    //开始画圆(描边)

    CGContextDrawPath(context,kCGPathStroke);

    //fill

    CGContextSetRGBFillColor(context,1, 0,0, 1);

    CGContextAddArc(context, 200, 40, 30,0, 2*M_PI,0);

    CGContextDrawPath(context,kCGPathFill);

    //fill $ stroke

    CGContextAddArc(context, 300, 40, 30,0, 2*M_PI,0);

    CGContextDrawPath(context,kCGPathFillStroke);

    

    [@"画线及弧线"drawInRect:CGRectMake(20,110, 100, 20)withAttributes:dic];

    

    CGContextMoveToPoint(context,130, 115);

    CGContextAddLineToPoint(context,280, 120);

    CGContextDrawPath(context,kCGPathStroke);

    

    CGContextMoveToPoint(context,285, 115);

    CGContextAddArcToPoint(context, 345, 115, 400,130, 150);

    CGContextDrawPath(context,kCGPathStroke);

    

 

    

}



@end


//类DrawRef.h

//

//  DrawRef.h

//  UIdraw画图

//

//  Created by ibokan on 16/1/22.

//  Copyright © 2016谭其伟. All rights reserved.

//


#import <UIKit/UIKit.h>


//声明一个自己类的协议,带有一个协议方法,这个协议是让自己的代理遵循的,这个方法也是让遵循这个协议的代理实现的,这里的代理是ViewController.m

@protocol textprotocol <NSObject>


-(void)transferString:(NSString *)string;




@end

@interface DrawRef : UIView

{

    NSMutableArray *points;

    NSMutableArray *linesArray;

    

    

    //创建一个颜色

    UIColor *penColor;

    

    

}

//画笔大小属性

@property(nonatomic,assign)float linewidth;

@property(nonatomic,assign)float count1;


@property(nonatomic,strong)NSString *text;

//声明自己有一个代理,一个遵循协议的代理

@property(nonatomic,assign)id<textprotocol>delegate;


@end


//

//  DrawRef.m

//  UIdraw画图

//

//  Created by ibokan on 16/1/22.

//  Copyright © 2016谭其伟. All rights reserved.

//


#import "DrawRef.h"




@implementation DrawRef


-(instancetype)initWithFrame:(CGRect)frame  //初始化

{

    if (self = [superinitWithFrame:frame]) {

        //设置背景颜色

//        self.backgroundColor = [UIColor whiteColor];

        //设置背景图片,但默认是平铺,图片不能缩放到屏幕大小

//        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"shu"]];

        //设置背景颜色为白色,透明度为0,然后再设置一个view,把图片放view里边,设置view的大小=屏幕的大小.

        self.backgroundColor = [UIColorcolorWithWhite:0.2alpha:0];

        self.userInteractionEnabled =YES; //可触摸

        self.multipleTouchEnabled =YES;

        linesArray = [NSMutableArrayarray];  //初始化

        

        self.linewidth =5;

        

        self.count1 =0;

        

        //颜色变化的监听

        //让他一出生就成为监听者(注册监听者),ChangeColor是通知名称

        [[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(changeColorAction:)name:ChangeColorobject:nil];

        

        //后退一步的监听

        [[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(deleteLastOne:)name:@"deleteLastOne"object:nil];

        

        //画笔大小的监听

//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeLineWidth:) name:@"changeLineWidth" object:nil];

        

        //清除all的通知监听

        [[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(deleteAll:)name:@"deleteAll"object:nil];

        

        

    }

    return self;

    

}


// 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();  //拿到一个画板

    

    //传值所需,需要把大小及颜色放到for里面

//    //设置画笔大小

//    CGContextSetLineWidth(context, _linewidth);

//    //设置画笔颜色

//    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);

    

    //当打开程序时,未开始画时,开始判断代理,如果条件符合则传值.

    if (self.delegate && [self.delegateconformsToProtocol:@protocol(textprotocol)]) {

        if (self.count1 ==0) {

            _text = @"0";

        }

        else

        {

            _text = [NSStringstringWithFormat:@"%.f",self.count1];

        }

        

        [self.delegatetransferString:_text];

    }

    

    

    //线

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

        

        

        

        //创建一个字典,用来存放线数组

        NSDictionary *dictionary = linesArray[i];

        //创建一个Color,它是由字典的key@"penColor"对应,一会传值时就传到@"penColor"就会自行改变color;

        UIColor *color = [dictionary objectForKey:@"penColor"];

        

        //读取字典里的值

        //为什么要先存进字典再读取字典呢?因为直接传值过来的话,一会画时每改变一次画笔大小,就会使得之前画的所有线都变成最后那条的大小.

        float line = [[dictionary objectForKey:@"lineWith"] floatValue];

        //设置画笔大小

        CGContextSetLineWidth(context, line);

        //设置画笔颜色(设置成上面声明的color)

        CGContextSetStrokeColorWithColor(context, color.CGColor);

        

        

        NSArray *pointss = [dictionary objectForKey:@"points"];

        

//        NSArray *pointsArray = linesArray[i];

        //第一点位置

        CGContextMoveToPoint(context, [pointss[0]CGPointValue].x, [pointss[0]CGPointValue].y);

        

        //连线

        for (int j =1; j < pointss.count; j++) {

            CGContextAddLineToPoint(context, [pointss[j]CGPointValue].x, [pointss[j]CGPointValue].y);

        }

        //添加到画板上

        CGContextStrokePath(context);

        

    }

    

    

}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

//    NSLog(@"手指放在上面");

    //初始化points这个数组

    points = [NSMutableArrayarray];

    //拿到手指并拿到点

    UITouch *touch = touches.anyObject;

    CGPoint point = [touch locationInView:self];

    //把点放到数组里

    [pointsaddObject:[NSValuevalueWithCGPoint:point]];

    

    //初始化一个字典

    NSMutableDictionary *mDitionary = [NSMutableDictionarydictionary];

    //points这个数组放在字典里面(对应的key)

    [mDitionary setObject:pointsforKey:@"points"];

    

    //把颜色存放到字典里

    if (penColor ==nil) {

        penColor = MY_COLOR(255, 200, 156, 1);

        //接收

        [mDitionary setObject:penColorforKey:@"penColor"];

    }

    else

    {

        [mDitionary setObject:penColorforKey:@"penColor"];

    }

    

    

    //通知/属性都要这一步初始化并写到字典里(这里的lineWith是在这里自定义的,就是这一步写到字典里时定义它的键,然后在上面读取字典时也用这个键读取就可以了)

    if (self.linewidth ==5) {

        [mDitionary setObject:[NSNumbernumberWithFloat:self.linewidth]forKey:@"lineWith"];

    }

    else

    {

        [mDitionary setObject:[NSNumbernumberWithFloat:self.linewidth]forKey:@"lineWith"];

    }

    

    

    

    

    

    //把这个存放有points数组及颜色的字典存放到linesArray里面

    [linesArray addObject:mDitionary];

    

    

}


-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

//    NSLog(@"手指移动");

    

    //把点存到数组里

    NSMutableArray *pointss = [[linesArraylastObject] objectForKey:@"points"];

    //拿到手指

    UITouch *touch = touches.anyObject;

    CGPoint point = [touch locationInView:self];

    //把拿到的点存放到数组里面

    [pointss addObject:[NSValuevalueWithCGPoint:point]];

    

    //调用drawRect方法(写到画板上)

    [selfsetNeedsDisplay];

    

    

    

}



-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

//    NSLog(@"手指离开屏幕");

    

    self.count1 =linesArray.count;

    NSLog(@"self.count1 = %f",self.count1);

    

    //当手指离开屏幕时,调用此方法,然后进行代理判断,如果存在代理且代理遵循协议那么就进行传值,使用自己的代理将_text值传出,然后让代理中实现的协议方法里接收这个值,进而把这个值传给对应的label.text.

    if (self.delegate && [self.delegateconformsToProtocol:@protocol(textprotocol)]) {

        if (self.count1 ==0) {

            _text = @"0";

        }

        else

        {

            _text = [NSStringstringWithFormat:@"%.f",self.count1];

        }

        

        [self.delegatetransferString:_text];

    }

    

    

    

    

}



//实现通知的回调方法

-(void)changeColorAction:(NSNotification *)sender

{

    //在这个回调方法里改变penColor

    penColor = [sender.userInfoobjectForKey:@"color"];

    

    

    

    

}



//实现通知的回调方法

-(void)deleteLastOne:(NSNotification *)sender

{

    //撤消下一步

    [linesArrayremoveLastObject];

    

    [selfsetNeedsDisplay];

    

    //第删除一条线就输出数组还剩几条线

    self.count1 =linesArray.count;

    NSLog(@"self.count1 = %f",self.count1);

    

    //当点击后退时,调用此方法,然后进行代理判断,如果存在代理且代理遵循协议那么就进行传值,使用自己的代理将_text值传出,然后让代理中实现的协议方法里接收这个值,进而把这个值传给对应的label.text.

    if (self.delegate && [self.delegateconformsToProtocol:@protocol(textprotocol)]) {

        if (self.count1 ==0) {

            _text = @"0";

        }

        else

        {

            _text = [NSStringstringWithFormat:@"%.f",self.count1];

        }

        

        [self.delegatetransferString:_text];

    }

    

    

}


//实现第三个通知的回调方法

-(void)changeLineWidth:(NSNotification *)sender

{


    self.linewidth = [[sender.userInfoobjectForKey:@"lineWith"]floatValue];


}



//实现清除所有的通知回调

-(void)deleteAll:(NSNotification *)sender

{

    //清空all

        [linesArrayremoveAllObjects];

    //调用画板 drawRect:写到画板上

    [selfsetNeedsDisplay];

    

    //清空时输出数组中有几条线,应该为0

    self.count1 =linesArray.count;

    NSLog(@"self.count1 = %f",self.count1);

    

    //当点击清除时,调用此方法,然后进行代理判断,如果存在代理且代理遵循协议那么就进行传值,使用自己的代理将_text值传出,然后让代理中实现的协议方法里接收这个值,进而把这个值传给对应的label.text.

    if (self.delegate && [self.delegateconformsToProtocol:@protocol(textprotocol)]) {

        if (self.count1 ==0) {

            _text = @"0";

        }

        else

        {

            _text = [NSStringstringWithFormat:@"%.f",self.count1];

        }

        

        [self.delegatetransferString:_text];

    }

    

    

}



//统一移除监听者

-(void)dealloc

{

    [[NSNotificationCenterdefaultCenter]removeObserver:selfname:ChangeColorobject:nil];

    

    [[NSNotificationCenterdefaultCenter]removeObserver:selfname:@"deleteLastOne"object:nil];

    

//    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"changeLineWidth" object:nil];

    

    [[NSNotificationCenterdefaultCenter]removeObserver:selfname:@"deleteAll"object:nil];

}


@end













0 0
原创粉丝点击