1

我需要翻转和图像,然后将其添加到按钮。我使用了以下代码

UIButton *playButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
playButton.frame = CGRectMake(xing, ying, dx,dy);
[playButton setTitle:text forState:UIControlStateNormal];
[playButton addTarget:self action:function forControlEvents:UIControlEventTouchUpInside];



UIImage *buttonImageNormal = [UIImage imageNamed:@"Begin-Set-Button.png"];
UIImage * other = [UIImage imageWithCGImage:buttonImageNormal.CGImage
                    scale:1.0 orientation: UIImageOrientationUpMirrored];
UIImage * awesome = [other stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[playButton setBackgroundImage:awesome forState:UIControlStateNormal];

它正确显示图像,但是当我单击它时,按钮显示未翻转的图像。

在我尝试解决这个问题时,我添加了以下代码行

[playButton setBackgroundImage:awesome forState:UIControlStateHighlighted];

然而,当我单击按钮时,它不会像使用未翻转图像创建的按钮那样使颜色变暗。

如何对其进行编码,以便当我使用翻转的图像时,它显示翻转的图像在按下时变暗?我知道如何手动使图像变暗,但我想知道是否有一种方法可以通过简单的函数调用自动完成?

4

2 回答 2

3

而不是翻转图像。您是否尝试过翻转 imageView?

UIImage *img = [UIImage imageNamed:@"path/to-image.png"];

UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.imageView.transform = CGAffineTransformMakeRotation(M_PI); // flip the image view
[button setImage:img forState:UIControlStateNormal];

这是我为保持选择状态突出显示而不必处理图像翻转所做的。

于 2013-04-16T18:32:30.633 回答
1

我也遇到了同样的问题,所以我搜索了一下这个帖子。我怀疑也许图像没有“固化”,因此镜像操作不是永久性的。

查看“imageWithCGImage:”的文档显示以下内容:

讨论 此方法不缓存图像对象。您可以使用 Core Graphics 框架的方法来创建 Quartz 图像参考。

因此,除非在新上下文中创建新图像,否则图像的镜像不适用于事件。我也一直在研究一种在任意大小的画布上绘制图像以创建具有相同背景的按钮的方法。您可以使用此方法创建维护CG操作的图像,例如镜像:

+ (UIImage *)imageWithCanvasSize:(CGSize)canvasSize withImage:(UIImage *)anImage
{
    if (anImage.size.width > canvasSize.width ||
        anImage.size.height > canvasSize.height)
    {
        // scale image first
        anImage = [anImage scaleWithAspectToSize:canvasSize];
    }

    CGRect targetRect = CGRectZero;
    CGFloat xOrigin = (canvasSize.width - anImage.size.width) / 2;
    CGFloat yOrigin = (canvasSize.height - anImage.size.height) / 2;
    targetRect.origin = CGPointMake(xOrigin, yOrigin);
    targetRect.size = anImage.size;

    UIGraphicsBeginImageContextWithOptions(canvasSize, NO, anImage.scale);
    [anImage drawInRect:targetRect];

    UIImage *canvasedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return canvasedImage;    
}

- (UIImage*)scaleWithAspectToSize:(CGSize)newSize
{
    CGSize scaledSize = newSize;

    float scaleFactor = 1.0;

    if (self.size.width > self.size.height)
    {
        scaleFactor = self.size.width / self.size.height;
        scaledSize.width = newSize.width;
        scaledSize.height = newSize.height / scaleFactor;
    }
    else
    {
        scaleFactor = self.size.height / self.size.width;
        scaledSize.height = newSize.height;
        scaledSize.width = newSize.width / scaleFactor;
    }

    UIGraphicsBeginImageContextWithOptions(scaledSize, NO, 0.0);
    CGRect scaledImageRect = CGRectMake(0.0, 0.0, scaledSize.width, scaledSize.height);
    [self drawInRect:scaledImageRect];

    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();    

    return scaledImage;
}
于 2013-02-03T02:00:31.977 回答