用UITouchView创建简单画板.点击颜色改变画笔颜色,有撤销键(UIButton简化后)
2014-04-12 15:00
525 查看
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
相关文章推荐
- TextView 点击、获得焦点等 改变颜色
- tableview 自定义cell 点击cell改变cell中的label.text的字体颜色,cell复用出现问题
- tableview 自定义cell 点击cell改变cell中的label.text的字体颜色,cell复用出现问题
- 关于TextView中,设置指定部分文字改变颜色,和指定部分文字点击事件
- 点击LinearLayout使用selector改变TextView字体颜色
- RecyclerView实现滚动滑动以及点击改变控件颜色
- TextView部分字体颜色改变,并且点击进行跳转
- android textview 给指定字改变颜色和添加点击事件
- view的clickable属性和点击background颜色改变
- 自定义View---圆形进度条&&点击改变颜色
- ios UIButton来回点击切换改变字体颜色
- 点击LinearLayout使用selector改变TextView字体颜色
- android中为TextView动态改变字体颜色,如点击时更改
- 点击LinearLayout使用selector改变TextView字体颜色
- 点击某个按钮切换图片、文字描述(按钮颜色跟着改变)(简化代码)
- Android 关于TextView中,设置指定部分文字改变颜色,和指定部分文字点击事件
- 微信小程序如何实现通过点击view改变背景颜色
- 点击LinearLayout使用selector改变TextView字体颜色
- 如何通过点击使TextView文字颜色改变
- Android 如何点击改变ListView行布局的TextView内容和颜色背景