用UITouchView创建简单画板.点击颜色改变画笔颜色,有撤销键(UIButton简化后)

本文深入探讨了在iOS平台上利用Swift编程语言实现触摸事件处理与动态视觉反馈的创新设计策略。通过构建一个交互式触摸视图组件,展示如何在应用程序中引入用户交互,并通过改变颜色和线条来实时反映用户操作轨迹。文章不仅涵盖了基本的UI元素和事件响应机制,还涉及了色彩变化和动画效果的实现,旨在提升用户体验和增强应用的互动性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MainViewControl.m

#import "MainViewController.h"
#import "TouchView.h"

@interface MainViewController ()

@end

@implementation MainViewController

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

- (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.
    
    TouchView * aView = [[TouchView alloc] initWithFrame:CGRectMake(20, 20, 280, 440)];
    [aView setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:aView];
    [aView release];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

TouchView.h

#import <UIKit/UIKit.h>

@interface TouchView : UIView

{
    //所有在.h里声明的容器类变量,只能在.m方法里初始化
    
    NSMutableArray * _pointArray;
    //建立可变数组,存数第一笔所有点组成的线
    NSMutableArray * _lineArray;
    //存储 改变颜色
    UIColor * _color;
}

@end


#import "TouchView.h"

@implementation TouchView

- (void) dealloc
{
    [_pointArray release];
    _pointArray= nil;
    [_lineArray release];
    _lineArray = nil;
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _pointArray = [[NSMutableArray alloc] init];
        _lineArray = [[NSMutableArray alloc] init];
        _color = [UIColor blackColor];
    }
    
   
    NSArray * arr = [NSArray arrayWithObjects:[UIColor redColor],[UIColor magentaColor],[UIColor yellowColor],[UIColor orangeColor],[UIColor greenColor],[UIColor cyanColor],[UIColor blueColor],[UIColor purpleColor],[UIColor brownColor],[UIColor grayColor],[UIColor blackColor],[UIColor clearColor], nil];
    
    int i = 0;
    for (UIColor * color in arr) {
        UIButton * abutton = [[UIButton alloc] initWithFrame:CGRectMake(23*i+2, 417, 23, 23)];
        [abutton setBackgroundColor:color];
        [abutton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        if (color == [UIColor clearColor]) {
            [abutton setTitle:@"c" forState:UIControlStateNormal];
            [abutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            //Button不能设置边框 ,所以要从底层调用,用.layer
            [abutton.layer setBorderWidth:1];
            //UIColor是对象类型,边框颜色需要CGColorRef类型的结构体,(只读的属性).所以后面要.CGColor
            [abutton.layer setBorderColor:[UIColor redColor].CGColor];
        }
        
        [self addSubview:abutton];
        [abutton release];
        
        i++;
    }


     return self;
}
    
    if ([button.currentTitle isEqualToString:@"c"]) {
        
        [_lineArray removeLastObject];
    }else{
        _color = button.backgroundColor;
    }
    [self setNeedsDisplay];
    
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //清空
//    [_pointArray removeAllObjects];
    //获得集合里的元素
    UITouch * touch = [touches anyObject];
    //获得起始点坐标
    CGPoint point = [touch locationInView:self];
    //结构体转换为对象,全部用NSValue   CGPoint是结构体  转换为 对象
    NSValue * value = [NSValue valueWithCGPoint:point];
    //把元素存入数组
//    [_pointArray addObject:value];
    //局部变量要用便利构造器  不用释放 系统自动release
    NSMutableArray * points = [NSMutableArray array];//创建火车头,也就是数组线
    //放入第一个椅子
    [points addObject:value];
    //把车厢挂到火车头上
//    [_lineArray addObject:points];
    //其他的在move中添加
    
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:_color,@"color",points,@"point", nil];
    //把字典传入数组里
    [_lineArray addObject:dic];
    
    
    
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch * touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    NSValue * value = [NSValue valueWithCGPoint:point];
//    [_pointArray addObject:value];
    //通过最后一个元素,取数组
//    NSMutableArray * points = [_lineArray lastObject];
    //把移动的坐标放到数组中
//    [points addObject:value];
    
    NSMutableDictionary * dic = [_lineArray lastObject];
    NSMutableArray * points = [dic objectForKey:@"point"];
    //把对象添加到数组里
    [points addObject:value];
    
    //调用下面绘画的方法
    [self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    
}


// 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 (NSMutableDictionary * dic in _lineArray) {
        //获得起始点坐标
        NSValue * firstValue = [[dic objectForKey:@"point"] firstObject];
        //转换
        CGPoint firstPoint = [firstValue CGPointValue];
        UIColor * c = [dic objectForKey:@"color"];
        CGContextSetStrokeColorWithColor(context, c.CGColor);
        //起始点
        CGContextMoveToPoint(context, firstPoint.x, firstPoint.y);
        
        for (NSValue * value in [dic objectForKey:@"point"]) {
            
            CGPoint currentPoint = [value CGPointValue];
            //添加到线上
            CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y);
            
        }
        
        CGContextStrokePath(context);
    }
    
}



@end




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值