不规则形状的uibutton
有的时候,我们需要使用非规则形状的按钮。UIButton允许你选择带有alpha通道的图像。比如,我使用下面四个图像:
#import "IrregularShapedButton.h"
#import "UIImage-Alpha.h"
@implementation IrregularShapedButton
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
? ? if (!CGRectContainsPoint([self bounds], point))
? ? ? ??return nil;
? ? else
? ? {
? ? ? ? UIImage *displayedImage = [self imageForState:[self state]];
? ? ? ??if (displayedImage == nil) // No image found, try for background image
? ? ? ? displayedImage = [self backgroundImageForState:[self state]];
? ? ? ? if (displayedImage == nil) // No image could be found, fall back to
? ? ? ? ? ? return self; ? ? ? ?
? ? ? ? BOOL isTransparent = [displayedImage isPointTransparent:point];
? ? ? ??if (isTransparent)
? ? ? ? ? ??return nil;
? ? }
? ? return self;
}
@end
将Interface Builder中的四个图像按钮改为IrregularShapedButton,它们将正常工作了。原文见:Irregularly Shaped UIButton