1

I am referring to this code - Create masking effect over a view to produce a scratch card effect. Now, I want to show a particular animation when a particular area lets say (100, 100, 70, 50) is scratched.

Or for that case, I just want to find out if the user has scratched the whole upper layer to show the animation.

Can we know what area is scratched and what is not?

Though I have provided the link, I will also include the code hoping for a prompt reply that way. :)

#import "ScratchableView.h"


@implementation ScratchableView


- (id)initWithFrame:(CGRect)frame {

self = [super initWithFrame:frame];
if (self) {
    scratchable = [UIImage imageNamed:@"screen1.png"].CGImage;
    width = CGImageGetWidth(scratchable);
    height = CGImageGetHeight(scratchable);
    self.opaque = NO;
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();

    CFMutableDataRef pixels = CFDataCreateMutable( NULL , width * height );
    alphaPixels = CGBitmapContextCreate( CFDataGetMutableBytePtr( pixels ) , width , height , 8 , width , colorspace , kCGImageAlphaNone );
    provider = CGDataProviderCreateWithCFData(pixels);


    CGContextSetFillColorWithColor(alphaPixels, [UIColor blackColor].CGColor);
    CGContextFillRect(alphaPixels, frame);

    CGContextSetStrokeColorWithColor(alphaPixels, [UIColor whiteColor].CGColor);
    CGContextSetLineWidth(alphaPixels, 50.0);
    CGContextSetLineCap(alphaPixels, kCGLineCapRound);

    CGImageRef mask = CGImageMaskCreate(width, height, 8, 8, width, provider, nil, NO);
    scratched = CGImageCreateWithMask(scratchable, mask);

    CGImageRelease(mask);
    CGColorSpaceRelease(colorspace);
}
return self;
}

- (void)drawRect:(CGRect)rect {
CGContextDrawImage(UIGraphicsGetCurrentContext() , [self bounds] , scratched);
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{ 
UITouch *touch = [[event touchesForView:self] anyObject];
firstTouch = YES;
location = [touch locationInView:self];
}

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

if (firstTouch) {
    firstTouch = NO;
    previousLocation = [touch previousLocationInView:self];
} else {
    location = [touch locationInView:self];
    previousLocation = [touch previousLocationInView:self];
}

// Render the stroke
[self renderLineFromPoint:previousLocation toPoint:location];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{  
UITouch *touch = [[event touchesForView:self] anyObject];
if (firstTouch) {
    firstTouch = NO;
    previousLocation = [touch previousLocationInView:self];


    [self renderLineFromPoint:previousLocation toPoint:location];
}


}

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

- (void) renderLineFromPoint:(CGPoint)start toPoint:(CGPoint)end {

CGContextMoveToPoint(alphaPixels, start.x, start.y);
CGContextAddLineToPoint(alphaPixels, end.x, end.y);
CGContextStrokePath(alphaPixels);
[self setNeedsDisplay];
}

- (void)dealloc {
CGContextRelease(alphaPixels);
CGImageRelease(scratchable);
CGDataProviderRelease(provider);
    [super dealloc];
}
Community
  • 1
  • 1
ScarletWitch
  • 522
  • 6
  • 23
  • CGContext is just a drawing area--it doesn't have bounds and it can't interact with user input. One way to do what you want is to subclass UIView and override the touch handling methods. (defined in UIView's superclass UIResponder. – nielsbot Sep 05 '13 at 08:18
  • possible duplicate of [How to erase finger paint on Custom UIView in iPhone](http://stackoverflow.com/questions/11132546/how-to-erase-finger-paint-on-custom-uiview-in-iphone) – nielsbot Sep 05 '13 at 08:18

0 Answers0