【发布时间】:2011-06-29 14:46:27
【问题描述】:
现在我能做的就是使用 touchesMoved 进行触摸绘图。
我可以通过将 CGContextSetLineWidth 分配给我分配给滑块值的浮点数来更改绘图的粗细。
我可以更改绘图的颜色。我通过将 touchesMoved void 中的 CGContextSetStrokeColor 分配给 4 个不同的浮点数来做到这一点。我根据我想要的颜色为浮点数分配不同的浮点值。在我的 viewDidLoad 中,从 1 到 4 的浮点数分别是 1.0、0.0、0.0 和 1.0。这会产生红色。使用 IBActions,我根据颜色值(RGB 然后 Alpha)为浮点数分配不同的值,因此通过按下绿色按钮,我可以将浮点数更改为 0.0、1.0、0.0、1.0。这会生成绿色。行!
所以现在我要做的实际上是创建一个橡皮擦按钮。我检查了 UIColor 类参考网页,并查看了清晰的颜色值,但它们无法抹去我所做的我能理解的事情。清晰的颜色不会抹去,它只是画出清晰的图画。我现在知道我必须创建一个 CGPoint 并将其分配给触摸的 locationInView,然后说如果该位置有绘图,那么绘图应该消失。我不知道这是否是一个正确的概念,但对我来说似乎是正确的。如果不正确,请告诉我。
我将提供我必要的代码,但我也要求有人分享他们的知识和代码,因为这真的让我陷入困境!老实说,我不是那些唠叨代码然后执行 command-c 然后 command-v 进入 XCode 的廉价人之一。解释总是受欢迎的,并且有益于我的学习,因为这仍然是我 12 岁时正在经历的事情!
这是我与问题相关的所有代码:
.h:
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
@interface GameScreen : UIViewController {
BOOL mouseSwiped;
float lineWidth;
CGPoint lastPoint;
IBOutlet UIImageView *drawImage;
CGFloat f1;
CGFloat f2;
CGFloat f3;
CGFloat f4;
}
-(IBAction)cyan;
-(IBAction)blue;
-(void)checktouch;
-(IBAction)eraser;
-(void)checkgreen;
-(IBAction)orange;
-(IBAction)purple;
-(IBAction)pink;
-(IBAction)black;
-(void)mouseswiped;
-(IBAction)clear;
-(IBAction)dismiss;
-(IBAction)green;
-(IBAction)yellow;
-(IBAction)brown;
-(IBAction)grey;
-(IBAction)white;
-(IBAction)newdp;
-(IBAction)menu;
-(void)remove;
-(IBAction)red;
@end
.m:
#import "GameScreen.h"
#import "DoodlePicsViewController.h"
#import "Settings.h"
@implementation GameScreen
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
lineWidth = thickness.value;
lastPoint = [touch locationInView:self.view];
lastPoint.y -= 20;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
currentPoint.y -= 20;
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), lineWidth);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),f1, f2, f3, f4);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
}
-(IBAction)yellow {
f1 = 1.0;
f2 = 1.0;
f3 = 0.0;
f4 = 1.0;
}
-(IBAction)brown {
f1 = 0.6;
f2 = 0.4;
f3 = 0.2;
f4 = 1.0;
}
-(IBAction)white {
f1 = 1.0;
f2 = 1.0;
f3 = 1.0;
f4 = 1.0;
}
-(IBAction)grey {
f1 = 0.5;
f2 = 0.5;
f3 = 0.5;
f4 = 1.0;
}
-(IBAction)red {
f1 = 1.0;
f2 = 0.0;
f3 = 0.0;
f4 = 1.0;
}
-(IBAction)cyan {
f1 = 0.0;
f2 = 1.0;
f3 = 1.0;
f4 = 1.0;
}
-(IBAction)blue {
f1 = 0.0;
f2 = 0.0;
f3 = 1.0;
f4 = 1.0;
}
-(IBAction)black {
f1 = 0.0;
f2 = 0.0;
f3 = 0.0;
f4 = 1.0;
}
-(IBAction)green {
f1 = 0.0;
f2 = 1.0;
f3 = 0.0;
f4 = 1.0;
}
-(IBAction)orange {
f1 = 1.0;
f2 = 0.5;
f3 = 0.0;
f4 = 1.0;
}
-(IBAction)pink {
f1 = 1.0;
f2 = 0.0;
f3 = 1.0;
f4 = 1.0;
}
-(IBAction)purple {
f1 = 0.5;
f2 = 0.0;
f3 = 0.5;
f4 = 1.0;
}
-(IBAction)eraser {
//I'm stuck right here LOL!
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if(!mouseSwiped) {
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), lineWidth);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), f1, f2, f3, f4);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}}
-(void)viewDidLoad {
f1 = 1.0;
f2 = 0.0;
f3 = 0.0;
f4 = 1.0;
}
@end
【问题讨论】:
标签: objective-c xcode