Find out if CGContext contains a specific Area - iPhone

197 views Asked by At

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];
}
0

There are 0 answers