您好,登錄后才能下訂單哦!
類和文件
其中有兩個功能沒有實現(xiàn)
AppDelegate.m
#import "AppDelegate.h" #import "ViewController.h" #import "DoodleView.h" @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; //創(chuàng)建一個ViewController控制器 ViewController *controller = [[ViewController alloc] init]; self.window.rootViewController = controller; [controller release]; // DoodleView *dooleView = [[DoodleView alloc] init]; // self.window.rootViewController = dooleView; // [dooleView release]; // [_window release]; // self.window = nil; //這樣也可以拉啦拉啦 return YES; } - (void)dealloc { [_window release]; [super dealloc]; } - (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } @end
ViewController.h
#import <UIKit/UIKit.h> @interface ViewController : UIViewController @end
ViewController.m
#import "ViewController.h" #import "DoodleView.h" #import "DoodleView1.h" @interface ViewController () @end @implementation ViewController - (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. self.view.backgroundColor = [UIColor cyanColor]; self.view.alpha = 0.2; DoodleView *dooleView = [[DoodleView alloc] initWithFrame:CGRectMake(40, 100, 240, 150)]; // dooleView.layer.cornerRadius = 10; dooleView.backgroundColor = [UIColor whiteColor]; [self.view addSubview:dooleView]; [dooleView release]; // DoodleView1 *view = [[DoodleView1 alloc] initWithFrame:CGRectMake(40, 260, 240, 150)]; // view.backgroundColor = [UIColor whiteColor]; // [self.view addSubview:view]; // [view release]; // // UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 120, 100)]; // label.backgroundColor = [UIColor magentaColor]; // [self.view addSubview:label]; // [label release]; // //如果點擊按鈕沒有反應,那么檢查父視圖以上的圖層,打開響應者鏈 // [label setUserInteractionEnabled:YES]; // // UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; // button.frame = CGRectMake(20, 50, 50, 100); // [button setTitle:@"按鈕" forState:UIControlStateNormal]; // [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; // [button setShowsTouchWhenHighlighted:YES]; // button.backgroundColor = [UIColor yellowColor]; // // [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; // [label addSubview:button]; //將button放到label上面 // //把button的什么是什么關閉 。然后就點擊使用不鳥了 。。。 //// [button setUserInteractionEnabled:NO]; } - (void)buttonClicked:(UIButton *)button { NSLog(@"點擊"); } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //開始觸摸 NSLog(@"觸摸開始"); } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"觸摸移動"); } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"觸摸結束"); } - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"觸摸取消"); } - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { NSLog(@"搖一搖開始"); } - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { NSLog(@"搖一搖結束"); } - (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event { NSLog(@"搖一搖取消"); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
DoodleView.h
#import <UIKit/UIKit.h> @interface DoodleView : UIView @property (nonatomic , retain)NSMutableArray *allLines; //收集所有的線 //@property (nonatomic , copy)NSMutableArray * @end
DoodleView.m
#import "DoodleView.h" @implementation DoodleView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code //只要數(shù)組(字典/集合)作為屬性,都需要在初始化方法進行初始化 self.allLines = [NSMutableArray array]; // // self.allLines = [[NSMutableArray alloc] init]; // [_allLines release]; // // _allLines = [[NSMutableArray alloc] init]; // //release寫在dealloc中 // // _allLines = [NSMutableArray array]; //這樣寫是完全不對的。。。注意啊 } return self; } // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code //所有有關于view重繪的內容,都要在這里寫 //1.獲得當前view的繪制信息 CGContextRef context = UIGraphicsGetCurrentContext(); //2.設置繪制線的顏色和寬度 CGContextSetStrokeColorWithColor(context, [UIColor magentaColor].CGColor);//顏色 CGContextSetLineWidth(context, 3);//寬度 //3.劃線 for (int i = 0; i < self.allLines.count; i++) { //取得每一條線上的所有點的數(shù)組 NSMutableArray *arr = [self.allLines objectAtIndex:i]; if (arr.count == 0) { continue; } for (int j= 0; j < arr.count - 1; j++) { //每次取得兩個點,在兩個點之間連線 NSValue *pointValue1 = [arr objectAtIndex:j]; NSValue *pointValue2 = [arr objectAtIndex:j + 1]; //將NSValue對象轉為CGPoint結構體 CGPoint point1 = [pointValue1 CGPointValue]; CGPoint point2 = [pointValue2 CGPointValue]; //在兩個點之間連線 CGContextMoveToPoint(context, point1.x, point1.y); CGContextAddLineToPoint(context, point2.x, point2.y); } } // //規(guī)定一個起點 // CGContextMoveToPoint(context, 10, 10); // //向終點劃線 // CGContextAddLineToPoint(context, 100, 10); // // //規(guī)定一個起點 // 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.根據(jù)繪制信息,將繪制的內容呈現(xiàn)在view上 CGContextStrokePath(context); UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; [button setTitle:@"橡皮擦" forState:UIControlStateNormal]; button.frame = CGRectMake(40, 100, 50, 30); button.backgroundColor = [UIColor blueColor]; button.alpha = 0.3; button.layer.cornerRadius = 10; [button addTarget:self action:@selector(buttonClicked1:) forControlEvents:UIControlEventTouchUpInside]; [self.window addSubview:button]; UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem]; [button1 setTitle:@"還原" forState:UIControlStateNormal]; button1.frame = CGRectMake(100, 100, 50, 30); button1.backgroundColor = [UIColor blueColor]; button1.alpha = 0.3; button1.layer.cornerRadius = 10; [button1 setShowsTouchWhenHighlighted:YES]; [button1 addTarget:self action:@selector(buttonClicked2:) forControlEvents:UIControlEventTouchUpInside]; [self.window addSubview:button1]; UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem]; [button2 setTitle:@"重繪" forState:UIControlStateNormal]; button2.frame = CGRectMake(160, 100, 50, 30); button2.backgroundColor = [UIColor blueColor]; button2.alpha = 0.3; button2.layer.cornerRadius = 10; [button2 setShowsTouchWhenHighlighted:YES]; [button2 addTarget:self action:@selector(buttonClicked3:) forControlEvents:UIControlEventTouchUpInside]; [self.window addSubview:button2]; UIButton *button3 = [UIButton buttonWithType:UIButtonTypeSystem]; [button3 setTitle:@"畫筆" forState:UIControlStateNormal]; button3.frame = CGRectMake(220, 100, 50, 30); button3.backgroundColor = [UIColor blueColor]; button3.alpha = 0.3; button3.layer.cornerRadius = 10; [button3 setShowsTouchWhenHighlighted:YES]; [button3 addTarget:self action:@selector(buttonClicked4:) forControlEvents:UIControlEventTouchUpInside]; [self.window addSubview:button3]; } //畫筆 - (void)buttonClicked4:(UIButton *)button { CGContextRef context = UIGraphicsGetCurrentContext(); CGFloat color = (arc4random() %256 /255.0 ); CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; CGFloat brightness1 = ( arc4random() % 56 / 256.0 ) + 0.5; CGContextSetStrokeColorWithColor(context, [UIColor colorWithHue:color saturation:saturation brightness:brightness alpha:brightness1].CGColor);//隨機顏色 } //清空 - (void)buttonClicked3:(UIButton *)button { [self.allLines removeAllObjects]; } //還原 - (void)buttonClicked2:(UIButton *)button { [self.allLines removeLastObject]; } //橡皮擦 - (void)buttonClicked1:(UIButton *)button { CGContextRef context = UIGraphicsGetCurrentContext(); //2.設置繪制線的顏色和寬度 CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);//顏色 CGContextSetLineWidth(context, 20);//寬度 //3.劃線 for (int i = 0; i < self.allLines.count; i++) { //取得每一條線上的所有點的數(shù)組 NSMutableArray *arr = [self.allLines objectAtIndex:i]; if (arr.count == 0) { continue; } for (int j= 0; j < arr.count - 1; j++) { //每次取得兩個點,在兩個點之間連線 NSValue *pointValue1 = [arr objectAtIndex:j]; NSValue *pointValue2 = [arr objectAtIndex:j + 1]; //將NSValue對象轉為CGPoint結構體 CGPoint point1 = [pointValue1 CGPointValue]; CGPoint point2 = [pointValue2 CGPointValue]; //在兩個點之間連線 CGContextMoveToPoint(context, point1.x, point1.y); CGContextAddLineToPoint(context, point2.x, point2.y); } } //4.根據(jù)繪制信息,將繪制的內容呈現(xiàn)在view上 CGContextStrokePath(context); } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //創(chuàng)建一個新的數(shù)組。用來裝這條線經(jīng)過的所有點 NSMutableArray *arr = [NSMutableArray array]; [self.allLines addObject:arr]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { //收集手經(jīng)過的所有點 //1.先要取得點要加入的數(shù)組 NSMutableArray *arr = [self.allLines lastObject]; //2.獲取當前的點 NSLog(@"%@", touches);//touches集合中每次只有一個元素,這個元素是UITouch的一個對象 UITouch *touche = [touches anyObject];//結合的元素 //獲取當前的點 CGPoint point = [touche locationInView:self]; // 將點加入到數(shù)組中 NSValue *pointValue = [NSValue valueWithCGPoint:point]; [arr addObject:pointValue]; //收集點完畢,需要在view上將新加入的點繪制出來 [self setNeedsDisplay];//作用:強制當前的view調用自己的drawRect:方法重繪 } @end
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。