Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

davejas69

macrumors newbie
Original poster
Feb 19, 2009
25
0
I have a view that allows you to "erase" it or scratch it off like a lottery
ticket and reveal the view below it.

I am trying to "reset" the process with a shake event. The only way I can
think to do it is to reload the awakeFromNib and reload the view, however it
remembers where I touched last and does not erase properly.

What would be the best way to reset this view as if I have never used it?

I have tried [self setNeedsDisplay]; and it does nothing


I've tried [self awakeFromNib]; and it resets the view but only allows me to erase in
the areas I had previously erased.

I tried [self removeFromSuperview]; but then I cannot get the view back.

At a real loss...

Code:
- (void)awakeFromNib {
	
	[[NSNotificationCenter defaultCenter] addObserver: self
											 selector: @selector(handleShakeEvent)
												 name: K_SHAKE_EVENT
											   object: nil];
	
	
	ScratchableView *scratchableView = [[ScratchableView alloc] initWithFrame:self.frame];
	[self addSubview:scratchableView];
		[self bringSubviewToFront:scratchableView];
}




-(void) handleShakeEvent
{
	//Do whatever you need to handle shake events.
	//[ScratchableView setNeedsDisplay];
	
	[self awakeFromNib];
}




- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
		scratchable = [UIImage imageNamed:@"scratchable.jpg"].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, 20.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];
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.