Как нарисовать CALayer с пути линии, как имитировать эффект ластика?
Я хочу смоделировать эффект ластика с сенсорным событием, чтобы показать изображение, которое за чем-то блокируется сверху, например, серым цветом;
Что-то вроде того:
Я долго находил решение, но не могу сделать это хорошо.
Ниже приведен мой код пользовательского представления: CustomView.m:
-(id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
[self setup];
}
return self;
}
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
-(void)setup
{
[self setMultipleTouchEnabled:NO];
[self setBackgroundColor:[UIColor darkGrayColor]];
self.drawingPath = [UIBezierPath bezierPath];
[self.drawingPath moveToPoint:CGPointZero];
[self.drawingPath setLineWidth:5.0];
self.image = [UIImage imageNamed:@"transformers.jpg"];
self.shapeLayer = [CAShapeLayer layer];
self.caLayer = [CALayer layer];
self.caLayer.frame = self.bounds;
self.caLayer.contents = (id)(self.image.CGImage);
[self.layer addSublayer:self.caLayer];
}
- (void)drawRect:(CGRect)rect
{
self.shapeLayer.path = [self.drawingPath CGPath];
self.caLayer.mask = self.shapeLayer;
self.shapeLayer.lineWidth = 5.0;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
[self.drawingPath moveToPoint:location];
lastPt = location;
[self setNeedsDisplay];
NSLog(@"Touch Began");
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
[self.drawingPath addLineToPoint:location];
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesMoved:touches withEvent:event];
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesMoved:touches withEvent:event];
}
Однако он просто имитирует заднее изображение, заполняющее путь. Я хочу, чтобы был эффект ластика, как на картинке. Пожалуйста помоги.