【发布时间】: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