ios - 如何翻转图像并使其在所有状态下正确显示

标签 ios objective-c ios6 uiimage

我需要翻转并成像,然后将其添加到按钮。我用下面的代码

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/

相关文章:

ios - 在我的 Objective-C 项目中进行 Swift 类集成后,无法在设备上进行编译

ios - Xcode 8.1 自动布局

objective-c - 为什么我必须选派代表?

ios - 如何在 CABasicAnimation 中加速/减慢按钮的旋转

objective-c - Objective C UIColor 到 NSString

ios6 - 在 UITextView 中使用 NSMutableAttributedString 进行 NSRange 搜索很慢

ios - 标签阴影在 swift 中不起作用

ios - Ionic Cordova 应用程序在 iPad 上启动时崩溃

iphone - 在导航栏中添加仅在当前 View 中显示的按钮。如何设置为所有 View ?

ios - Xcode IB Storyboard方向和容器 View