首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

不规则形态的uibutton

2012-07-15 
不规则形状的uibutton有的时候,我们需要使用非规则形状的按钮。UIButton允许你选择带有alpha通道的图像。比

不规则形状的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

热点排行