您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
类和文件
其中有两个功能没有实现
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]; //创建一个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 //只要数组(字典/集合)作为属性,都需要在初始化方法进行初始化 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++) { //取得每一条线上的所有点的数组 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); } } // //规定一个起点 // 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 = [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++) { //取得每一条线上的所有点的数组 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.根据绘制信息,将绘制的内容呈现在view上 CGContextStrokePath(context); } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //创建一个新的数组。用来装这条线经过的所有点 NSMutableArray *arr = [NSMutableArray array]; [self.allLines addObject:arr]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { //收集手经过的所有点 //1.先要取得点要加入的数组 NSMutableArray *arr = [self.allLines lastObject]; //2.获取当前的点 NSLog(@"%@", touches);//touches集合中每次只有一个元素,这个元素是UITouch的一个对象 UITouch *touche = [touches anyObject];//结合的元素 //获取当前的点 CGPoint point = [touche locationInView:self]; // 将点加入到数组中 NSValue *pointValue = [NSValue valueWithCGPoint:point]; [arr addObject:pointValue]; //收集点完毕,需要在view上将新加入的点绘制出来 [self setNeedsDisplay];//作用:强制当前的view调用自己的drawRect:方法重绘 } @end
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。