【问题标题】:How To Erase Image With Touch如何通过触摸擦除图像
【发布时间】: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


    【解决方案1】:

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

    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
    

    【讨论】:

      【解决方案2】:

      您可以尝试将橡皮擦设为普通的 touchesMoved,并将颜色设置为您使用的任何背景颜色。因此,当它们着色时,它看起来像是在擦除,但实际上只是用与背景相同的颜色着色。您似乎已经有了代码,所以我认为我不需要发布它。希望这会有所帮助。

      【讨论】:

        【解决方案3】:

        我已经将两个图像视图放在一起,我在一个中加载背景并在另一个中绘制,我在绘制图像视图上将背景设置为不透明度 0%,我可以这样做:

        -(IBAction) eraseLine {
        
        drawImage.image = nil;
        
        }
        

        删除所有行而不删除我在背景视图中的图像。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-07-21
          • 1970-01-01
          • 2017-11-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多