iPhone中带有偏斜区域的自定义UIButton

问题描述:

我想在屏幕上显示带有此图像的按钮.如您在此处看到的,此图像从各个角落都是透明的.

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 and I don't want that kind of functionality.

我的意思是,只有在点击了图片而不是图片以外的内容时,才可以识别该点击.

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

那怎么可能?

您可以将UIButton子类化,然后覆盖其

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