DoodleView做一个画板和Touche的方法
类和文件
其中有两个功能没有实现
AppDelegate.m
#import"AppDelegate.h"#import"ViewController.h"#import"DoodleView.h"@implementationAppDelegate-(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions{self.window=[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];//Overridepointforcustomizationafterapplicationlaunch.self.window.backgroundColor=[UIColorwhiteColor];[self.windowmakeKeyAndVisible];//创建一个ViewController控制器ViewController*controller=[[ViewControlleralloc]init];self.window.rootViewController=controller;[controllerrelease];//DoodleView*dooleView=[[DoodleViewalloc]init];//self.window.rootViewController=dooleView;//[dooleViewrelease];//[_windowrelease];//self.window=nil;//这样也可以拉啦拉啦returnYES;}-(void)dealloc{[_windowrelease];[superdealloc];}-(void)applicationWillResignActive:(UIApplication*)application{//Sentwhentheapplicationisabouttomovefromactivetoinactivestate.Thiscanoccurforcertaintypesoftemporaryinterruptions(suchasanincomingphonecallorSMSmessage)orwhentheuserquitstheapplicationanditbeginsthetransitiontothebackgroundstate.//Usethismethodtopauseongoingtasks,disabletimers,andthrottledownOpenGLESframerates.Gamesshouldusethismethodtopausethegame.}-(void)applicationDidEnterBackground:(UIApplication*)application{//Usethismethodtoreleasesharedresources,saveuserdata,invalidatetimers,andstoreenoughapplicationstateinformationtorestoreyourapplicationtoitscurrentstateincaseitisterminatedlater.//Ifyourapplicationsupportsbackgroundexecution,thismethodiscalledinsteadofapplicationWillTerminate:whentheuserquits.}-(void)applicationWillEnterForeground:(UIApplication*)application{//Calledaspartofthetransitionfromthebackgroundtotheinactivestate;hereyoucanundomanyofthechangesmadeonenteringthebackground.}-(void)applicationDidBecomeActive:(UIApplication*)application{//Restartanytasksthatwerepaused(ornotyetstarted)whiletheapplicationwasinactive.Iftheapplicationwaspreviouslyinthebackground,optionallyrefreshtheuserinterface.}-(void)applicationWillTerminate:(UIApplication*)application{//Calledwhentheapplicationisabouttoterminate.Savedataifappropriate.SeealsoapplicationDidEnterBackground:.}@end
ViewController.h
#import<UIKit/UIKit.h>@interfaceViewController:UIViewController@end
ViewController.m
#import"ViewController.h"#import"DoodleView.h"#import"DoodleView1.h"@interfaceViewController()@end@implementationViewController-(id)initWithNibName:(NSString*)nibNameOrNilbundle:(NSBundle*)nibBundleOrNil{self=[superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];if(self){//Custominitialization}returnself;}-(void)viewDidLoad{[superviewDidLoad];//Doanyadditionalsetupafterloadingtheview.self.view.backgroundColor=[UIColorcyanColor];self.view.alpha=0.2;DoodleView*dooleView=[[DoodleViewalloc]initWithFrame:CGRectMake(40,100,240,150)];//dooleView.layer.cornerRadius=10;dooleView.backgroundColor=[UIColorwhiteColor];[self.viewaddSubview:dooleView];[dooleViewrelease];//DoodleView1*view=[[DoodleView1alloc]initWithFrame:CGRectMake(40,260,240,150)];//view.backgroundColor=[UIColorwhiteColor];//[self.viewaddSubview:view];//[viewrelease];////UILabel*label=[[UILabelalloc]initWithFrame:CGRectMake(50,100,120,100)];//label.backgroundColor=[UIColormagentaColor];//[self.viewaddSubview:label];//[labelrelease];////如果点击按钮没有反应,那么检查父视图以上的图层,打开响应者链//[labelsetUserInteractionEnabled:YES];////UIButton*button=[UIButtonbuttonWithType:UIButtonTypeSystem];//button.frame=CGRectMake(20,50,50,100);//[buttonsetTitle:@"按钮"forState:UIControlStateNormal];//[buttonsetTitleColor:[UIColorblackColor]forState:UIControlStateNormal];//[buttonsetShowsTouchWhenHighlighted:YES];//button.backgroundColor=[UIColoryellowColor];////[buttonaddTarget:selfaction:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];//[labeladdSubview:button];//将button放到label上面////把button的什么是什么关闭。然后就点击使用不鸟了。。。////[buttonsetUserInteractionEnabled:NO];}-(void)buttonClicked:(UIButton*)button{NSLog(@"点击");}-(void)touchesBegan:(NSSet*)toucheswithEvent:(UIEvent*)event{//开始触摸NSLog(@"触摸开始");}-(void)touchesMoved:(NSSet*)toucheswithEvent:(UIEvent*)event{NSLog(@"触摸移动");}-(void)touchesEnded:(NSSet*)toucheswithEvent:(UIEvent*)event{NSLog(@"触摸结束");}-(void)touchesCancelled:(NSSet*)toucheswithEvent:(UIEvent*)event{NSLog(@"触摸取消");}-(void)motionBegan:(UIEventSubtype)motionwithEvent:(UIEvent*)event{NSLog(@"摇一摇开始");}-(void)motionEnded:(UIEventSubtype)motionwithEvent:(UIEvent*)event{NSLog(@"摇一摇结束");}-(void)motionCancelled:(UIEventSubtype)motionwithEvent:(UIEvent*)event{NSLog(@"摇一摇取消");}-(void)didReceiveMemoryWarning{[superdidReceiveMemoryWarning];//Disposeofanyresourcesthatcanberecreated.}/*#pragmamark-Navigation//Inastoryboard-basedapplication,youwilloftenwanttodoalittlepreparationbeforenavigation-(void)prepareForSegue:(UIStoryboardSegue*)seguesender:(id)sender{//Getthenewviewcontrollerusing[seguedestinationViewController].//Passtheselectedobjecttothenewviewcontroller.}*/@end
DoodleView.h
#import<UIKit/UIKit.h>@interfaceDoodleView:UIView@property(nonatomic,retain)NSMutableArray*allLines;//收集所有的线//@property(nonatomic,copy)NSMutableArray*@end
DoodleView.m
#import"DoodleView.h"@implementationDoodleView-(id)initWithFrame:(CGRect)frame{self=[superinitWithFrame:frame];if(self){//Initializationcode//只要数组(字典/集合)作为属性,都需要在初始化方法进行初始化self.allLines=[NSMutableArrayarray];////self.allLines=[[NSMutableArrayalloc]init];//[_allLinesrelease];////_allLines=[[NSMutableArrayalloc]init];////release写在dealloc中////_allLines=[NSMutableArrayarray];//这样写是完全不对的。。。注意啊}returnself;}//OnlyoverridedrawRect:ifyouperformcustomdrawing.//Anemptyimplementationadverselyaffectsperformanceduringanimation.-(void)drawRect:(CGRect)rect{//Drawingcode//所有有关于view重绘的内容,都要在这里写//1.获得当前view的绘制信息CGContextRefcontext=UIGraphicsGetCurrentContext();//2.设置绘制线的颜色和宽度CGContextSetStrokeColorWithColor(context,[UIColormagentaColor].CGColor);//颜色CGContextSetLineWidth(context,3);//宽度//3.划线for(inti=0;i<self.allLines.count;i++){//取得每一条线上的所有点的数组NSMutableArray*arr=[self.allLinesobjectAtIndex:i];if(arr.count==0){continue;}for(intj=0;j<arr.count-1;j++){//每次取得两个点,在两个点之间连线NSValue*pointValue1=[arrobjectAtIndex:j];NSValue*pointValue2=[arrobjectAtIndex:j+1];//将NSValue对象转为CGPoint结构体CGPointpoint1=[pointValue1CGPointValue];CGPointpoint2=[pointValue2CGPointValue];//在两个点之间连线CGContextMoveToPoint(context,point1.x,point1.y);CGContextAddLineToPoint(context,point2.x,point2.y);}}////规定一个起点//CGContextMoveToPoint(context,10,10);////向终点划线//CGContextAddLineToPoint(context,100,10);//////规定一个起点//CGContextMoveToPoint(context,10,10);////向终点划线//CGContextAddLineToPoint(context,100,50);//////画正方形//CGContextAddRect(context,CGRectMake(50,50,50,50));//////CGContextClearRect(context,CGRectMake(70,70,60,60));////画圆形//CGContextAddEllipseInRect(context,CGRectMake(100,50,50,50));////画曲线//CGContextMoveToPoint(context,10,200);//起点//CGContextAddCurveToPoint(context,0,0,100,0,200,200);//4.根据绘制信息,将绘制的内容呈现在view上CGContextStrokePath(context);UIButton*button=[UIButtonbuttonWithType:UIButtonTypeSystem];[buttonsetTitle:@"橡皮擦"forState:UIControlStateNormal];button.frame=CGRectMake(40,100,50,30);button.backgroundColor=[UIColorblueColor];button.alpha=0.3;button.layer.cornerRadius=10;[buttonaddTarget:selfaction:@selector(buttonClicked1:)forControlEvents:UIControlEventTouchUpInside];[self.windowaddSubview:button];UIButton*button1=[UIButtonbuttonWithType:UIButtonTypeSystem];[button1setTitle:@"还原"forState:UIControlStateNormal];button1.frame=CGRectMake(100,100,50,30);button1.backgroundColor=[UIColorblueColor];button1.alpha=0.3;button1.layer.cornerRadius=10;[button1setShowsTouchWhenHighlighted:YES];[button1addTarget:selfaction:@selector(buttonClicked2:)forControlEvents:UIControlEventTouchUpInside];[self.windowaddSubview:button1];UIButton*button2=[UIButtonbuttonWithType:UIButtonTypeSystem];[button2setTitle:@"重绘"forState:UIControlStateNormal];button2.frame=CGRectMake(160,100,50,30);button2.backgroundColor=[UIColorblueColor];button2.alpha=0.3;button2.layer.cornerRadius=10;[button2setShowsTouchWhenHighlighted:YES];[button2addTarget:selfaction:@selector(buttonClicked3:)forControlEvents:UIControlEventTouchUpInside];[self.windowaddSubview:button2];UIButton*button3=[UIButtonbuttonWithType:UIButtonTypeSystem];[button3setTitle:@"画笔"forState:UIControlStateNormal];button3.frame=CGRectMake(220,100,50,30);button3.backgroundColor=[UIColorblueColor];button3.alpha=0.3;button3.layer.cornerRadius=10;[button3setShowsTouchWhenHighlighted:YES];[button3addTarget:selfaction:@selector(buttonClicked4:)forControlEvents:UIControlEventTouchUpInside];[self.windowaddSubview:button3];}//画笔-(void)buttonClicked4:(UIButton*)button{CGContextRefcontext=UIGraphicsGetCurrentContext();CGFloatcolor=(arc4random()%256/255.0);CGFloatsaturation=(arc4random()%128/256.0)+0.5;CGFloatbrightness=(arc4random()%128/256.0)+0.5;CGFloatbrightness1=(arc4random()%56/256.0)+0.5;CGContextSetStrokeColorWithColor(context,[UIColorcolorWithHue:colorsaturation:saturationbrightness:brightnessalpha:brightness1].CGColor);//随机颜色}//清空-(void)buttonClicked3:(UIButton*)button{[self.allLinesremoveAllObjects];}//还原-(void)buttonClicked2:(UIButton*)button{[self.allLinesremoveLastObject];}//橡皮擦-(void)buttonClicked1:(UIButton*)button{CGContextRefcontext=UIGraphicsGetCurrentContext();//2.设置绘制线的颜色和宽度CGContextSetStrokeColorWithColor(context,[UIColorwhiteColor].CGColor);//颜色CGContextSetLineWidth(context,20);//宽度//3.划线for(inti=0;i<self.allLines.count;i++){//取得每一条线上的所有点的数组NSMutableArray*arr=[self.allLinesobjectAtIndex:i];if(arr.count==0){continue;}for(intj=0;j<arr.count-1;j++){//每次取得两个点,在两个点之间连线NSValue*pointValue1=[arrobjectAtIndex:j];NSValue*pointValue2=[arrobjectAtIndex:j+1];//将NSValue对象转为CGPoint结构体CGPointpoint1=[pointValue1CGPointValue];CGPointpoint2=[pointValue2CGPointValue];//在两个点之间连线CGContextMoveToPoint(context,point1.x,point1.y);CGContextAddLineToPoint(context,point2.x,point2.y);}}//4.根据绘制信息,将绘制的内容呈现在view上CGContextStrokePath(context);}-(void)touchesBegan:(NSSet*)toucheswithEvent:(UIEvent*)event{//创建一个新的数组。用来装这条线经过的所有点NSMutableArray*arr=[NSMutableArrayarray];[self.allLinesaddObject:arr];}-(void)touchesMoved:(NSSet*)toucheswithEvent:(UIEvent*)event{//收集手经过的所有点//1.先要取得点要加入的数组NSMutableArray*arr=[self.allLineslastObject];//2.获取当前的点NSLog(@"%@",touches);//touches集合中每次只有一个元素,这个元素是UITouch的一个对象UITouch*touche=[touchesanyObject];//结合的元素//获取当前的点CGPointpoint=[touchelocationInView:self];//将点加入到数组中NSValue*pointValue=[NSValuevalueWithCGPoint:point];[arraddObject:pointValue];//收集点完毕,需要在view上将新加入的点绘制出来[selfsetNeedsDisplay];//作用:强制当前的view调用自己的drawRect:方法重绘}@end
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。