繁体   English   中英

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

[英]iPhone SDK Erase A UIImageView from the Screen Using Touches?

我正在寻找一种能够从屏幕上擦除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 { } 

您绝对可以使用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();

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

CGContextSetBlendMode(context, kCGBlendModeClear);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM