我需要翻转并成像,然后将其添加到按钮。我用下面的代码
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];
但是,当我单击按钮时,它不会像我用未翻转的图像创建的按钮那样使颜色变暗。
如何编码,以便在使用翻转图像时,按下后翻转图像变暗?我知道如何手动使图像变暗,但是我想知道是否有一种方法可以通过简单的函数调用来自动使图像变暗?
最佳答案
而不是翻转图像。您是否尝试过翻转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];
这是我保持选择状态突出显示而不必处理图像翻转的方法。
关于ios - 如何翻转图像并使其在所有状态下正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14573546/