views:

224

answers:

1

http://img16.imageshack.us/img16/225/ladibig.png

I want to have a button on a screen with this image. this image is transparent from its corners as you can see here.

UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(xCo, yCo, kImageSizeWidth, kImageSizeHeight)];
[btn setImage:[UIImage imageNamed:@"aboveImg.png"] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnTapped:) forControlEvents:UIControlEventTouchDown];

But the button tap is recognized out side image & i don't want that kind of functionality.

I mean tap must be recognized only when it is tapped within image not outside image.

How is that possible ?

Thanks in advance for sharing your great knowledge.

Sagar.

+1  A: 

You can subclass that UIButton, then override its -pointInside:withEvent: function to something similar to

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event {
   if (point is in that rhombus)
     return YES;
   else
     return NO;
}
KennyTM
Isn't there is an option to have skewed button ?
sugar
@sugar: You could use some combination of `.transform`, but this method is much more flexible.
KennyTM
Yep ! Done With Transform. I am adding my answer - so, you can understand what exactly I Did.
sugar