如何使用触摸擦除图像 [英] How To Erase Image With Touch

查看:46
本文介绍了如何使用触摸擦除图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我能做的就是用 touchesMoved 来画画.

Right now what I can do is draw with touch, using touchesMoved.

  • 我可以通过将 CGContextSetLineWidth 分配给我分配给滑块值的浮点数来更改绘图的厚度.

  • I can change the thickness of the drawing by assigning the CGContextSetLineWidth to a float that I assigned to a sliders value.

我可以更改绘图的颜色.我通过将 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.这会生成绿色.好的!

I can change the drawing's colors. I do this by assigning to CGContextSetStrokeColor in the touchesMoved void to 4 different floats. I assign the floats different float values according to the color I want. In my viewDidLoad, the floats from 1 to 4 respectively are 1.0, 0.0, 0.0 and 1.0. That generates a red color. Using IBActions I assign different values for the floats according to the color value (RGB then Alpha) So by pressing the green button I can change to floats to 0.0, 1.0, 0.0, 1.0. This generates a green color. Ok!

所以现在我真正想做的是创建一个橡皮擦按钮.我检查了 UIColor 类参考网页,我查看了清晰的颜色值,但他们无法删除我所做的我能理解的事情.清晰的颜色不会擦除,它只是绘制清晰的图画.我现在知道我必须制作一个 CGPoint 并将其分配给触摸的 locationInView ,然后说如果该位置有绘图,那么绘图应该消失.我不知道这是否是一个正确的概念,但对我来说似乎是正确的.如果不正确,请告诉我.

So now what I want to do is actually create an eraser button. I checked the UIColor class reference webpage and I looked at the clear color values but they could not erase what I had done which I could understand. The clear color doesn't erase, it just draws a clear drawing. I now know that I have to make a CGPoint and assign that to a touch's locationInView and then say that if there is drawing in the location, then the drawing should be gone. I don't know if that is a correct concept but it seems correct to me. If it is not correct please tell me.

我会提供我必要的代码,但我也要求有人分享他们的知识和他们的代码,因为这真的让我卡住了!老实说,我不是那些唠叨代码然后执行 command-c 然后 command-v 进入 XCode 的廉价人之一.总是欢迎解释并有益于我的学习,因为这仍然是我 12 岁时正在经历的事情!

I will provide my necessary code, but I ALSO ASK FOR SOMEONE to share their knowledge and their code because this has really got me stuck! Too be honest, Im not one of those cheap people who nag for code then do command-c and then command-v into XCode. Explanations are always welcome and benefit my learning as that is still something I am undergoing at the age of 12!

这是我所有与问题相关的代码:

Here is all my code that is related to the question:

.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

推荐答案

正如您正确观察到的那样,当您使用默认混合模式进行绘制时,使用清晰颜色进行绘制没有任何效果.但是,当您将图形上下文的混合模式设置为复制"时,您绘制的所有内容都将替换其下方的内容,而不是在其上方进行绘制.用清晰的颜色画画就会有橡皮擦的效果.

As you correctly observed, drawing with the clear color doesn't have any effect when you draw with the default blend mode. When you set your graphics context's blend mode to "copy" however, everything that you draw will replace what's underneath it instead of painting over it. Drawing with the clear color will then have the effect of an eraser.

CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);

这篇关于如何使用触摸擦除图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆