类似于QQ截屏以后可以使用画笔在所截取的视图上涂鸦,本章的内容是简单实现UI里面在画布上进行绘图的功能,功能简单单一,主要是传递一种编程思想。
重点:绘图的过程其实是将手指在屏幕中划过的每个点保存下来,然后保存到数组中,再用drawRect方法从数组中取出这些点,进行对画布的描绘。首先,至少要有两个数组,一个用来保存一个笔画里面有多少个点,另一个用来保存总共有多少笔画。好吧,话不多说,上代码:
====================AppDelegate.m=================
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColorwhiteColor];
//================程序运行,进入该事件,“跳转”到根视图==========
RootViewController *rootView=[[RootViewControlleralloc]init];//创建一个视图控制器的对象
self.window.rootViewController=rootView;//设置window的根视图为该试图控制器的对象
[rootView release];//内存管理
[self.windowmakeKeyAndVisible];
return YES;
}
==================RootViewController.m================
- (void)viewDidLoad
{
MyView *mView=[[MyViewalloc]initWithFrame:CGRectMake(0,0, 320,460)];
[self.viewaddSubview:mView];
mView.backgroundColor=[UIColorblackColor];
[mView release];
[superviewDidLoad];
// Do any additional setup after loading the view.
}
================MyView.h=======================
@interface MyView : UIView
{
NSMutableArray *_lineArray;//存放笔画的数组
UITouch *_touch;
UIColor *_changColor;//获取button的背景颜色
NSMutableArray *_colorChangeArray;//存放颜色变化的数组
NSMutableArray *_lineWidthChangeArray;//存放画笔大小的数组
CGFloat dd;//获取画笔大小的值
}
@end
===================MyView.m=======================
#pragma -mark装载事件
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// ===================给全局变量初始化并赋值==============================
_lineArray=[[NSMutableArrayalloc]init];//存放笔画的数组必须先初始化,后面才能使用
_colorChangeArray =[[NSMutableArrayalloc]init];//同上
_lineWidthChangeArray=[[NSMutableArrayalloc]init];
// NSLog(@"dd=%@",[_lineWidthChangeArray objectAtIndex:0]);
_changColor=[[UIColoralloc]init];//同上
_changColor=[UIColorredColor];//先赋个初始值给_changeColor,让程序运行开始默认的画笔颜色为红色
// =================把红橙黄绿青蓝紫七种颜色装载到数组里面======================
UIColor *redColor=[UIColorredColor];
UIColor *orangeColor=[UIColororangeColor];
UIColor *yellowColor=[UIColoryellowColor];
UIColor *greenColor=[UIColorgreenColor];
UIColor *cyanColor=[UIColorcyanColor];
UIColor *blueColor=[UIColorblueColor];
UIColor *purpleColor=[UIColorpurpleColor];
NSArray *colorArray=@[redColor,orangeColor,yellowColor,greenColor,cyanColor,blueColor,purpleColor];
// ===============添加红橙黄绿青蓝紫7个按钮并给他们加载changeColor:事件=============
for (int i=0; i<7; i++)
{
UIButton *button=[UIButtonbuttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(25+40*i,20, 30, 30);
[button setBackgroundColor:[colorArrayobjectAtIndex:i]];
[button addTarget:selfaction:@selector(changColor:)forControlEvents:UIControlEventTouchDown];
[self addSubview:button];
}
// ==============单独添加撤销和清屏按钮===================================
//撤销按钮
UIButton *backButton=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];
backButton.frame=CGRectMake(170,405, 100, 30);
[backButton setTitle:@"撤销"forState:UIControlStateNormal];
[backButton addTarget:selfaction:@selector(backButton)forControlEvents:UIControlEventTouchDown];
[self addSubview:backButton];
//清屏按钮
UIButton *clearButton=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];
clearButton.frame=CGRectMake(60,405, 100, 30);
[clearButton setTitle:@"清屏"forState:UIControlStateNormal];
[clearButton addTarget:selfaction:@selector(clearButton)forControlEvents:UIControlEventTouchDown];
[self addSubview:clearButton];
//画笔label
UILabel *huabiLabel=[[UILabelalloc]initWithFrame:CGRectMake(5,65, 70, 25)];
huabiLabel.text=@"画笔大小:";
huabiLabel.textColor=[UIColorwhiteColor];
UIFont *la=[UIFontfontWithName:@"Didot" size:12];
[huabiLabel setFont:la];
[huabiLabel setBackgroundColor:[UIColorblackColor]];
[self addSubview:huabiLabel];
[huabiLabel release];
//滑块控件(调整画笔大小)
UISlider *huabiSlider=[[UISlideralloc]initWithFrame:CGRectMake(75,65, 220, 25)];
[huabiSlider setBackgroundColor:[UIColorclearColor]];
huabiSlider.minimumValue=1;
huabiSlider.maximumValue=20;
[huabiSlider setValue:5];
dd=huabiSlider.value;
[huabiSlider addTarget:selfaction:@selector(chageLineWidth:)forControlEvents:UIControlEventValueChanged];
[self addSubview:huabiSlider];
}
return self;
}
#pragma -mark滑块控件的valueChange事件
-(void)chageLineWidth:(UISlider *)sender
{
dd=sender.value;
NSLog(@"dd=%f",dd);
[selfsetNeedsDisplay];
}
#pragma -mark撤销按钮的TouchDown事件
-(void)backButton
{
[_lineArrayremoveLastObject];//移除笔画数组中最后一笔,实现撤销功能
[_colorChangeArrayremoveLastObject];//移除颜色数组中最后一色,为了实现颜色与笔画一一对应的关系,重绘时才不会出错
[_lineWidthChangeArrayremoveLastObject];
[selfsetNeedsDisplay];//系统调用drawRect事件,进行重绘
}
#pragma -mark清屏按钮的TouchDown事件
-(void)clearButton
{
[_lineArrayremoveAllObjects];
[_colorChangeArray removeAllObjects];
[_lineWidthChangeArrayremoveAllObjects];
[selfsetNeedsDisplay];
}
#pragma -mark七个颜色按钮的TouchDown事件
-(void)changColor:(id)sender
{
UIButton *button=sender;
_changColor=[button backgroundColor];
// NSLog(@"%@",_changColor);
}
#pragma -mark画布重绘事件
- (void)drawRect:(CGRect)rect
{
// NSLog(@"%s",__FUNCTION__);
CGContextRef currentContext=UIGraphicsGetCurrentContext();
static int i=0;
for ( i=0; i<[_lineArraycount]; i++) {
NSArray *array=[_lineArrayobjectAtIndex:i];
UIColor *colorNow=[_colorChangeArrayobjectAtIndex:i];
NSNumber *ff=[_lineWidthChangeArrayobjectAtIndex:i];
float kk=[ff floatValue];
CGContextSetStrokeColorWithColor(currentContext, colorNow.CGColor);
CGContextSetLineWidth(currentContext, kk);
for (int j=0; j<[arraycount]-1; j++) {
NSValue *a=[array objectAtIndex:j];
CGPoint _a=[a CGPointValue];
NSValue *b=[array objectAtIndex:j+1];
CGPoint _b=[b CGPointValue];
CGContextMoveToPoint(currentContext, _a.x, _a.y);
CGContextAddLineToPoint(currentContext, _b.x, _b.y);
CGContextStrokePath(currentContext);
}
}
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// NSLog(@"%s",__FUNCTION__);
[_colorChangeArray addObject:_changColor];
[_lineWidthChangeArrayaddObject:[NSNumbernumberWithFloat:dd]];
UITouch *touch=[touches anyObject];
CGPoint currentPoint=[touch locationInView:self];
NSValue *firstPosition=[NSValuevalueWithCGPoint:currentPoint];
NSMutableArray *array=[[NSMutableArrayalloc]init];
[array addObject:firstPosition];
[_lineArray addObject:array];
[array release];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// NSLog(@"%s",__FUNCTION__);
_touch=[touches anyObject];
CGPoint currentPoint=[_touchlocationInView:self];
NSValue *valuePosition=[NSValuevalueWithCGPoint:currentPoint];
NSMutableArray *array=[_lineArraylastObject];
[array addObject:valuePosition];
[selfsetNeedsDisplay];
[selfsetNeedsDisplay];
}