2010-04-15 131 views
1

我知道如何更改常规按钮的状态,以及更改常规按钮的背景颜色。但是,当我做UIButton:setBackgroundImage(自定义按钮),那么我不能改变状态了。我想,当用户点击它时,会发生一些事情让用户知道该按钮被选中,并且当用户再次点击它时,它会回到常规状态。我试图改变按钮的背景,但它不起作用。这是我如何创建我的自定义按钮更改自定义按钮的状态(带图像的按钮)?

UIButton *b = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
b.frame = CGRectMake(X, Y, H, W); 
UIImage *iv = [UIImage imageWithContentsOfFile:picFilePath]; 
[b setBackgroundImage:iv forState:UIControlStateNormal]; 

的按钮获取图像由iPhone的摄像头,因此不能使用不同的图片为选中状态

[button setAlpha:0.8] 

这将减仓的按钮,是有什么会使按钮变暗?

回答

4

您需要在位图上下文中创建图像的副本,通过绘制覆盖图或以某种方式处理它来修改副本,然后将修改后的图像分配给按钮的UIControlStateSelected状态。

下面是创建的上下文,并绘制一个50%的黑色覆盖在图像上的按钮的选择状态的一些示例代码,其中将其调暗:

//assume UIImage* image exists 

//get the size of the image 
CGFloat pixelsHigh = image.size.height; 
CGFloat pixelsWide = image.size.width; 

//create a new context the same size as the image 
CGContextRef cx = NULL; 
CGColorSpaceRef colorSpace; 
void *   bitmapData; 
int    bitmapByteCount; 
int    bitmapBytesPerRow; 

bitmapBytesPerRow = (pixelsWide * 4); 
bitmapByteCount  = (bitmapBytesPerRow * pixelsHigh); 

colorSpace = CGColorSpaceCreateDeviceRGB(); 
bitmapData = malloc(bitmapByteCount); 
if (bitmapData == NULL) 
{ 
    fprintf (stderr, "Memory not allocated!"); 
    return; 
} 
cx = CGBitmapContextCreate (bitmapData, 
           pixelsWide, 
           pixelsHigh, 
           8,  // bits per component 
           bitmapBytesPerRow, 
           colorSpace, 
           kCGImageAlphaPremultipliedLast); 
if (cx == NULL) 
{ 
    free (bitmapData); 
    fprintf (stderr, "Context not created!"); 
    return; 
} 
CGColorSpaceRelease(colorSpace); 
CGRect imageRect = CGRectMake(0, 0, pixelsWide, pixelsHigh); 

//draw the existing image in the context 
CGContextDrawImage(cx, imageRect, image.CGImage); 

//draw a 50% black overlay 
CGContextSetRGBFillColor(cx, 0, 0, 0, 0.5); 
CGContextFillRect(cx, imageRect); 

//create a new image from the context 
CGImageRef newImage = CGBitmapContextCreateImage(cx); 
UIImage* secondaryImage = [UIImage imageWithCGImage:newImage]; 
CGImageRelease(newImage); 
CGContextRelease(cx); 

//assign the images to the button 
[button setBackgroundImage:image forState:UIControlStateNormal]; 
[button setBackgroundImage:secondaryImage forState:UIControlStateSelected]; 
+0

super ...非常感谢 – 2010-04-21 15:38:11

1

为什么你让图像覆盖按钮而不是将按钮的背景设置为图像?

编辑: 你想要做的是让你突出显示和/或选定状态按钮有不同的背景,像这样。

UIImage* highlightImage = [UIImage imageNamed:@"highlightImage.png"]; 
[b setBackgroundImage:highlightImage forState:UIControlStateSelected]; 

然后在事件处理

- (void)buttonOnclick 
{ 
    b.selected = !b.selected; 
} 
+0

我的确在做,我只是转发一些代码,请看看他们 – 2010-04-15 22:04:20

+0

按钮的图像是由iphone相机拍摄的。所以我不能做你建议的 – 2010-04-20 02:28:07

+0

你需要发布更多的代码或更完整地解释自己。我不确定你的问题是什么。虽然如果我认为我正确理解了你,我会做的是将一个UIView放在你想要突出显示的按钮的顶部,将此UIView的userInteractionEnabled = NO,背景Alpha设置为0,并将颜色设置为所需的高亮颜色。然后当用户选择你的按钮时,将alpha改为说.5,这将突出显示按钮。 如果您只需要突出显示某些区域而不是整个矩形,请使用Quartz并覆盖drawRect,或者使用透明度制作png。 – DevDevDev 2010-04-20 18:07:38

0

你可能需要设置的背景图像的每个合适的状态。

1
UIButton *b = [UIButton buttonWithType:UIButtonRoundedRect]; 
b.frame = CGRectMake(X,Y,H,W); 

// set a different background image for each state 
[b setBackgroundImage:myNormalBackgroundImage forState:UIControlStateNormal]; 
[b setBackgroundImage:mySelectedBackgroundImage forState:UIControlStateSelected]; 
+0

是的,我已经想到了......其实我在其他应用程序中通过photoshop图像实现了类似的东西,并在它上面有一个复选标记,但问题是,按钮的图像是从相机中获取的,因此无法使用photoshop他们在飞行 – 2010-04-20 02:20:53

-1

我在我的应用程序中使用setImage而不是setBackgroundImage来做到这一点,它对我来说工作正常。代码如下所示:

[button setImage:[self eraserImageForSize:radius selected:selected] 
     forState:UIControlStateNormal]; 

...其中eraserImageForSize:选择:返回正确的形象,为“选中”状态(在你的情况,你只需在操作事件触发)。

+0

什么是'eraserImageForSize'?什么是“半径”? – 2010-04-20 02:32:00

+0

eraserImageForSize是我的代码中的一种方法,它生成的图像可用作给定半径的橡皮擦 - 这在我自己的代码之外并不重要。重要的部分是setImage调用。如果你愿意,这里是一个简化的例子: [button setImage:someImage forState:UIControlStateNormal] – 2010-05-04 14:18:08

1

要变暗按钮,假设背景包含图像,您可以添加一个子图层到视图(按钮)。子图层将覆盖背景图像。您可以使用该图层为背景图像提供“色调”。

您可以创建一个黑色或灰色图层,其alpha值为0.3左右。您可以创建一个带有白色图层和适当字母的减轻层。

CALayer Class Reference学习如何创建图层,然后将子到按钮的看法是这样的:

[self.layer insertSublayer:darkeningLayer atIndex:0]; 

我用这个来在视图的背景插入梯度层。