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

我用這個來在視圖的背景插入梯度層。