现在我可以做的是使用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并将其分配给touch的locationInView,然后说如果该位置有绘图,则绘图应该消失。我不知道这是否是一个正确的概念,但对我来说似乎是正确的。如果不正确请告诉我。
我将提供必要的代码,但我也要求某人分享他们的知识和代码,因为这确实让我陷入困境!老实说,我不是那些唠叨代码然后执行命令-c然后命令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
答案 0 :(得分:2)
正如您所看到的那样,使用默认混合模式绘制时,使用清晰颜色绘制没有任何效果。但是,当您将图形上下文的混合模式设置为“复制”时,您绘制的所有内容都将替换下面的内容而不是绘制它。使用清晰的颜色绘制将产生橡皮擦的效果。
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
答案 1 :(得分:0)
您可以尝试使橡皮擦只是普通的touchesMoved并将颜色设置为您正在使用的任何背景颜色。因此,当它们着色时,它看起来就像是擦除但实际上只是着色与背景颜色相同。你似乎已经有了代码,所以我认为我不需要发布它。希望这会有所帮助。
答案 2 :(得分:0)
我把两个图像视图放在一起,然后我将背景加载到另一个中并在另一个中绘制,我在绘制图像视图上将背景设置为不透明度0%,我可以这样做:
-(IBAction) eraseLine {
drawImage.image = nil;
}
删除所有行而不删除我在后台视图中的图像。