更改自定义按钮的状态(带图像的按钮)? [英] Change the state of a customized button (button with an image)?

查看:127
本文介绍了更改自定义按钮的状态(带图像的按钮)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何更改常规按钮的状态,以及更改常规按钮的背景颜色。但是,当我执行 UIButton:setBackgroundImage (自定义按钮)时,我不能再改变状态了。我想,当用户点击它时,会在视觉上发生让用户知道该按钮被选中的事情,当用户再次点击它时,它会回到常规状态。我尝试更改按钮的背景,但它不起作用。以下是我创建自定义按钮的方法

I know how to change the state of the regular button, as well as change the background color of a regular button. However, when I do UIButton:setBackgroundImage (customized button), then I cant change the state anymore. I want, when the user click on it, something visually would happen to let the user know that button is selected, and when the user click again it go back to the regular state. I try to change the background of the button, but it doesnt work. Here is how I create my customized button

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

按钮的图像由iPhone相机拍摄,因此无法使用不同的图片选择状态

The images for the buttons are taken by the iPhone camera, therefore cant use different picture for selected state

[button setAlpha:0.8]

这会减轻按钮的压力,是否会使按钮变暗?

That would lighten up the button, is there something that would darken the button?

推荐答案

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

You need to create a copy of the image in a bitmap context, modify the copy by drawing an overlay or processing it in some way, and then assign the modified image to the button's UIControlStateSelected state.

下面是一些示例代码,用于创建上下文并在图像上为按钮的选定状态绘制50%的黑色覆盖图,使其变暗:

Here is some sample code that creates the context and draws a 50% black overlay on the image for the selected state of the button, which darkens it:

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

这篇关于更改自定义按钮的状态(带图像的按钮)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆