【问题标题】:iPhone SDK Erase A UIImageView from the Screen Using Touches?iPhone SDK 使用触摸从屏幕上擦除 UIImageView?
【发布时间】:2012-07-14 01:17:57
【问题描述】:

我正在寻找一种能够从屏幕上擦除 UIImageView 的方法。当我说擦除时,我的意思不是[imageView removeFromSuperview];,我的意思是通过在屏幕上涂抹手指来擦除部分图像。无论你的手指在哪里,那都是图像中被擦除的部分。我只是找不到任何帮助。

我会认为图像与石英有关吗?如果是这样,那我真的不擅长。 :(

我想最好的例子是彩票。一旦你刮开票的一部分,它下面的那个区域就会显露出来。有谁知道如何做到这一点?

谢谢!

更新:

下面的代码就是诀窍。谢谢!
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    lastTouch = [touch locationInView:canvasView];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    currentTouch = [touch locationInView:canvasView];

    CGFloat brushSize = 35;
    CGColorRef strokeColor = [UIColor whiteColor].CGColor;

    UIGraphicsBeginImageContext(scratchView.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [canvasView.image drawInRect:CGRectMake(0, 0, canvasView.frame.size.width, canvasView.frame.size.height)];
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, brushSize);
    CGContextSetStrokeColorWithColor(context, strokeColor);
    CGContextSetBlendMode(context, kCGBlendModeClear);
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, lastTouch.x, lastTouch.y);
    CGContextAddLineToPoint(context, currentTouch.x, currentTouch.y);
    CGContextStrokePath(context);
    canvasView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    lastTouch = [touch locationInView:canvasView];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

}

【问题讨论】:

  • 我想象一个更简单的方法,如果它在一个简单的背景上不会擦除 UIImage 而是使用 Quartz 简单地在它上面绘制背景颜色:)
  • @RyanPoolos 否,因为它下面有一个 UIView 和多个图像。我想擦除顶部图像以显示其下方的 UIView。
  • 那你根本不能使用 UIImageView 。您将不得不将其绘制到自定义 Quartz 层中并以艰难的方式完成。
  • @Alec 感谢我需要的代码,非常感谢 Alec
  • @Spynet 我很高兴这对其他人有所帮助。有什么暗示你用这个做的吗?哈哈

标签: iphone ios core-graphics quartz-graphics uitouch


【解决方案1】:

您绝对可以使用UIImageView 来完成此操作,无需自定义 Quartz 层。你熟悉 iOS 中的任何形式的绘图吗?基本上,您只需要使用touchesBegan:touchesMoved:touchesEnded 跟踪当前和以前的触摸位置。

然后你需要在当前触摸位置和之前触摸位置之间绘制一条“线”(在这种情况下会擦除其下方的内容),使用类似以下内容的内容,该内容直接取自我开发的实际应用程序,该应用程序做了一些事情相当相似:

UIGraphicsBeginImageContext(canvasView.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[canvasView.image drawInRect:CGRectMake(0, 0, canvasView.frame.size.width, canvasView.frame.size.height)];
CGContextSetLineCap(context, lineCapType);
CGContextSetLineWidth(context, brushSize);
CGContextSetStrokeColorWithColor(context, strokeColor);
CGContextSetBlendMode(context, kCGBlendModeClear);
CGContextBeginPath(context);
CGContextMoveToPoint(context, lastTouch.x, lastTouch.y);
CGContextAddLineToPoint(context, currentTouch.x, currentTouch.y);
CGContextStrokePath(context);
canvasView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

在此代码中,canvasViewUIImageView。这种绘图有很多教程。您想要的重要是将混合模式设置为 kCGBlendModeClear。就是这一行:

CGContextSetBlendMode(context, kCGBlendModeClear);

【讨论】:

  • 在 touchesBegan 中,你会设置 lastTouch,然后在 Moved 中,你会在发布所有代码之后设置 lastTouch,在 touchesEnded 中你会设置 lastTouch = nil 吗?
  • 关闭。在 touchesBegan 中,您将设置 lastTouch。在 touchesMoved 中,您将在绘图代码之前设置 currentTouch,在最后设置 lastTouch。在 touchesEnded 中,您将在开头设置 currentTouch。任何时候都不需要设置 lastTouch = nil。
  • 这很奇怪,我刮了它,它可以工作,但它向下擦除了大约 150 个像素。除了我需要的东西之外,我没有更改任何代码。
  • 您如何获得触摸的位置?确保您使用与您要擦除的 UIImageView 相同的 UIImageView 调用 locationInView:
  • 您需要使用lastTouch = [touch locationInView:canvasView]; 而不是lastTouch = [touch locationInView:self.view];。当然,currentTouch 也是如此。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-29
  • 1970-01-01
  • 2015-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-08
相关资源
最近更新 更多