Randomize Everything

Icon

And create the perfect world

Hit Testing A Non-Transparent Area In Objective C

Coding hit tests has always been a hassle when transparent .png files come into the game. That’s why Jeff Lamarche has extended the standard UIButton and added the hit testing of transparent .png buttons. This new class will only return a valid hit when you are touching a non-transparent part of the .png image.

More information and the downloadable source can be found here.

Some example code to get you going:

-(void)viewDidLoad
{
	UIImage *image = [UIImage imageNamed:@"image-in-resources.png"];
	IrregularShapedButton *irrButton = [[IrregularShapedButton buttonWithType:UIButtonTypeCustom] retain];
	[irrButton setImage:image forState:UIControlStateNormal];
	irrButton.frame = CGRectMake(0, 0, image.size.width, image.size.height);
 
	[self.view addSubview:irrButton];
	[super viewDidLoad];
}
 
- (IBAction)buttonClick:(id)sender
{
    NSLog(@"Clicked:%@", sender);
}

Category: Objective-C, iPad, iPhone

Tagged: , , , , , ,

Leave a Reply