UIButton在HighLighted时不会改变图片,只会将图片设置为灰色
Posted
技术标签:
【中文标题】UIButton在HighLighted时不会改变图片,只会将图片设置为灰色【英文标题】:UIButton won't change picture when HighLighted, will only set picture to grey 【发布时间】:2013-05-07 09:02:29 【问题描述】:我希望图片在用户触摸它的那一刻变成黄色。现在第一次点击时图像会变灰,释放时会变黄。 第二次点击图片会立即变回红色。我该如何解决这个问题?
-(void)bMethod:(UIButton*)sender
sender.selected = !sender.selected;
- (void)viewDidLoad
[super viewDidLoad];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:@"red.png"];
UIImage *buttonImageSelected = [UIImage imageNamed:@"yellow.png"];
myButton.frame = CGRectMake(200, 200, 40, 40);
[myButton addTarget:self action:@selector(bMethod:) forControlEvents:UIControlEventTouchDown];
[myButton setImage:buttonImage forState:UIControlStateNormal];
[myButton setImage:buttonImageSelected forState:UIControlStateSelected];
[myButton setImage:buttonImageSelected forState:UIControlStateHighlighted];
[self.view addSubview:myButton];
【问题讨论】:
使用UIControlEventTouchDown
而不是UIControlEventTouchUpInside
的任何特殊原因?
没有。认为它会更即时,不会等待用户松开手指。
你试过设置myButton.adjustsImageWhenHighlighted = NO;
吗?
是的,它会去除灰色,但仍然不会在高亮显示黄色图片
黄色图片没有显示?
【参考方案1】:
我运行了你的代码,它运行良好,伙计。只需仔细检查图像名称!
编辑 1:
将您的代码替换为以下内容:
-(void)bMethod:(UIButton*)sender
if (sender.imageView.image != [UIImage imageNamed:@"red.png"])
[sender setImage:[UIImage imageNamed:@"yellow.png"] forState:UIControlStateHighlighted];
[sender setImage:[UIImage imageNamed:@"yellow.png"] forState:UIControlStateNormal];
else
[sender setImage:[UIImage imageNamed:@"red.png"] forState:UIControlStateHighlighted];
[sender setImage:[UIImage imageNamed:@"red.png"] forState:UIControlStateNormal];
- (void)viewDidLoad
[super viewDidLoad];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:@"red.png"];
myButton.frame = CGRectMake(200, 200, 40, 40);
[myButton addTarget:self action:@selector(bMethod:) forControlEvents:UIControlEventTouchDown];
[myButton setImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:myButton];
编辑 2:
-(void)bMethod:(UIButton*)sender
sender.selected = !sender.selected;
sender.highlighted = !sender.highlighted;
- (void)viewDidLoad
[super viewDidLoad];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:@"red.png"];
UIImage *buttonImageSelected = [UIImage imageNamed:@"yellow.png"];
myButton.frame = CGRectMake(200, 200, 40, 40);
[myButton addTarget:self action:@selector(bMethod:) forControlEvents:UIControlEventTouchUpInside];
myButton.adjustsImageWhenHighlighted = NO;
[myButton setImage:buttonImage forState:UIControlStateNormal];
[myButton setImage:buttonImageSelected forState:UIControlStateSelected];
[myButton setImage:buttonImageSelected forState:UIControlStateHighlighted];
[self.view addSubview:myButton];
【讨论】:
当您运行我的代码并点击按钮并保持您的手指时,图像是否会变为黄色?我刚刚开始了一个新项目,当我点击并保持我的手指时,图像不会变成黄色。 没有。我确实拿掉了手指,它确实变黄了。当用户将手指放在按钮上时,您是否希望更改它? 是的。我希望图像在用户触摸按钮时立即变为黄色,并在用户再次触摸时立即变为红色。 将(sender.imageView.image != [UIImage imageNamed:@"red.png"])
更改为 (sender.imageView.image == [UIImage imageNamed:@"red.png"])
以使其工作。为什么编写一个简单的高亮按钮如此复杂(如果我从 Interface Builder 中设置高亮图像就可以了?
我做到了。我正在使用代码,您的回答解决了我的问题。我只是在问为什么我需要编写这么多行代码来完成一个简单的高亮工作。请修正我上次评论中的错误,以便我接受。以上是关于UIButton在HighLighted时不会改变图片,只会将图片设置为灰色的主要内容,如果未能解决你的问题,请参考以下文章