一个简易的彩色画板

#import "MainViewController.h"
#import "DoodleView.h"
@interface MainViewController ()

@end

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    DoodleView *doodleView1 = [[DoodleView alloc] initWithFrame:CGRectMake(10, 30, 300, 390)];
    doodleView1.tag = 1;
    [self.view addSubview:doodleView1];
    [doodleView1 release];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setTintColor:[UIColor purpleColor]];
    button.backgroundColor = [UIColor grayColor];
    button.frame = CGRectMake(260, 430, 40, 40);
    [button setTitle:@"橡皮" forState:UIControlStateNormal];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(button1Clicked:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button1 setTintColor:[UIColor purpleColor]];
    button1.backgroundColor = [UIColor blackColor];
    button1.frame = CGRectMake(220, 430, 40, 40);
    [button1 setTitle:@"撤销" forState:UIControlStateNormal];
    [self.view addSubview:button1];
    [button1 addTarget:self action:@selector(button2Clicked:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button2 setTintColor:[UIColor blueColor]];
    [button2 setTitle:@"红色" forState:UIControlStateNormal];
    button2.backgroundColor = [UIColor redColor];
    button2.frame = CGRectMake(180, 430, 40, 40);
    [self.view addSubview:button2];
    [button2 addTarget:self action:@selector(button3Clicked:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button3 setTintColor:[UIColor purpleColor]];
    [button3 setTitle:@"黄色" forState:UIControlStateNormal];
    button3.backgroundColor = [UIColor yellowColor];
    button3.frame = CGRectMake(140, 430, 40, 40);
    [self.view addSubview:button3];
    [button3 addTarget:self action:@selector(button4Clicked:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *button4 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button4 setTintColor:[UIColor purpleColor]];
    [button4 setTitle:@"蓝色" forState:UIControlStateNormal];
    button4.backgroundColor = [UIColor blueColor];
    button4.frame = CGRectMake(100, 430, 40, 40);
    [self.view addSubview:button4];
    [button4 addTarget:self action:@selector(button5Clicked:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *button5 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button5 setTintColor:[UIColor purpleColor]];
    [button5 setTitle:@"绿色" forState:UIControlStateNormal];
    button5.backgroundColor = [UIColor greenColor];
    button5.frame = CGRectMake(60, 430, 40, 40);
    [self.view addSubview:button5];
    [button5 addTarget:self action:@selector(button6Clicked:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *button6 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button6 setTintColor:[UIColor blueColor]];
    [button6 setTitle:@"紫色" forState:UIControlStateNormal];
    button6.backgroundColor = [UIColor purpleColor];
    button6.frame = CGRectMake(60, 430, 40, 40);
    [self.view addSubview:button6];
    [button6 addTarget:self action:@selector(button7Clicked:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *button7 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button7 setTintColor:[UIColor blackColor]];
    [button7 setTitle:@"粉色" forState:UIControlStateNormal];
    button7.backgroundColor = [UIColor magentaColor];
    button7.frame = CGRectMake(20, 430, 40, 40);
    [self.view addSubview:button7];
    [button7 addTarget:self action:@selector(button8Clicked:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(120, 40, 80, 30)];
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor grayColor];
    label.textColor = [UIColor whiteColor];
    label.text = @"彩色画板";
    [self.view addSubview:label];
    
}
- (void)button1Clicked:(UIButton *)button
{
    DoodleView *doodleView = (DoodleView *)[self.view viewWithTag:1];
    doodleView.color = [UIColor brownColor];
}

- (void)button2Clicked:(UIButton *)button1
{
    DoodleView *doodleView1 = (DoodleView *)[self.view viewWithTag:1];
    [doodleView1.lines removeLastObject];
    [doodleView1 setNeedsDisplay];
}

- (void)button3Clicked:(UIButton *)button2
{
    DoodleView *doodleView2 = (DoodleView *)[self.view viewWithTag:1];
    doodleView2.color = [UIColor redColor];
}

- (void)button4Clicked:(UIButton *)button3
{
    DoodleView *doodleView = (DoodleView *)[self.view viewWithTag:1];
    doodleView.color = [UIColor yellowColor];
}

- (void)button5Clicked:(UIButton *)button4
{
    DoodleView *doodleView = (DoodleView *)[self.view viewWithTag:1];
    doodleView.color = [UIColor blueColor];
}

- (void)button6Clicked:(UIButton *)button5
{
    DoodleView *doodleView = (DoodleView *)[self.view viewWithTag:1];
    doodleView.color = [UIColor greenColor];
}

- (void)button7Clicked:(UIButton *)button6
{
    DoodleView *doodleView = (DoodleView *)[self.view viewWithTag:1];
    doodleView.color = [UIColor purpleColor];
}
- (void)button8Clicked:(UIButton *)button7
{
    DoodleView *doodleView = (DoodleView *)[self.view viewWithTag:1];
    doodleView.color = [UIColor magentaColor];
}

#import "DoodleView.h"
#import "MainViewController.h"
@implementation DoodleView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.lines = [NSMutableArray array];
        self.backgroundColor = [UIColor brownColor];
        self.color = [UIColor blackColor];
        
        
    }
    return self;
}

- (void)dealloc
{
    [_lines release];
    [super dealloc];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 在触摸到屏幕的时候 就创建一个数组
    NSMutableArray *line = [NSMutableArray array];
    
    NSMutableDictionary *dicColor = [NSMutableDictionary dictionaryWithObjectsAndKeys:self.color,@"color",
                                     line,@"point", nil];
    
    // 添加到大的数组中
    [self.lines addObject:dicColor];
    
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 当手指移动的时候 把获取到的每一个点放入所在的数组中
    NSMutableArray *line = [[self.lines lastObject] objectForKey:@"point"];
    
    // 获取点
    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self];
    
    // 把结构体类型 转换为 对象类型
    NSValue *point = [NSValue valueWithCGPoint:p];
    
    // 将点放到数组中
    [line addObject:point];
    
    // 强制view调用自己的drawRect:方法
    [self setNeedsDisplay];
    
}



// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    // 获得view的绘制信息
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetLineWidth(context, 20);
    
    for (NSDictionary *lineDic in self.lines) {
        
        NSMutableArray * line = [lineDic objectForKey:@"point"];
        
        UIColor *color = [lineDic objectForKey:@"color"];
        
        CGContextSetStrokeColorWithColor(context, color.CGColor);
        
        if (0 == [line count]) {
            continue;
        }
        for (int i = 0; i < line.count - 1; i++) {
            // 按顺序取一个点 以及 之后的一个点
            NSValue *point1 = [line objectAtIndex:i];
            CGPoint p1 = [point1 CGPointValue];
            
            NSValue *point2 = [line objectAtIndex:i + 1];
            CGPoint p2 = [point2 CGPointValue];
            
            CGContextMoveToPoint(context, p1.x, p1.y);
            CGContextAddLineToPoint(context, p2.x, p2.y);   
        }
        CGContextStrokePath(context); // 绘制
    }    
}



@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【优质项目推荐】 1、项目代码均经过严格本地测试,运行OK,确保功能稳定后才上传平台。可放心下载并立即投入使用,若遇到任何使用问题,随时欢迎私信反馈与沟通,博主会第一时间回复。 2、项目适用于计算机相关专业(如计科、信息安全、数据科学、人工智能、通信、物联网、自动化、电子信息等)的在校学生、专业教师,或企业员工,小白入门等都适用。 3、该项目不仅具有很高的学习借鉴价值,对于初学者来说,也是入门进阶的绝佳选择;当然也可以直接用于 毕设、课设、期末大作业或项目初期立项演示等。 3、开放创新:如果您有一定基础,且热爱探索钻研,可以在此代码基础上二次开发,进行修改、扩展,创造出属于自己的独特应用。 欢迎下载使用优质资源!欢迎借鉴使用,并欢迎学习交流,共同探索编程的无穷魅力! 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值