iPhone SDK从屏幕上删除UIImageView使用触摸?

Ale*_*lec 10 iphone core-graphics quartz-graphics uitouch ios

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

我想成像与Quartz有关吗?如果是这样的话,我对此并不是很好.:(

我想最好的例子是彩票.一旦你抓住机票的一部分,它下面的那个区域就会显露出来.有谁知道怎么做到这一点?

谢谢!

更新:

以下代码是诀窍.谢谢!

- (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 {

}
Run Code Online (Sandbox Code Playgroud)

Eth*_*ser 10

您绝对可以使用a 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();
Run Code Online (Sandbox Code Playgroud)

在这段代码中canvasView是一个UIImageView.这种绘图有很多教程.您想要的重点是将混合模式设置为kCGBlendModeClear.就是这条线:

CGContextSetBlendMode(context, kCGBlendModeClear);
Run Code Online (Sandbox Code Playgroud)