我们在用UIButton的时候,想要两张图片,一张时正常显示的,一张是在点击时显示的,这个该怎么实现呢?
下面时我的实现方式,如有别的方式,请大家一起分享出来,共同学习....
NSString *path = nil; UIImage *image = nil; path = [[NSBundle mainBundle] pathForResource:@"menubtn1" ofType:@"png"]; image = [UIImage imageWithContentsOfFile:path]; _gameBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _gameBtn.frame = CGRectMake(180, 670, image.size.width, image.size.height); [_gameBtn setImage:image forState:UIControlStateNormal];//这个图片是在布点击时响应 path = nil; image = nil; path = [[NSBundle mainBundle] pathForResource:@"menubtn1_1" ofType:@"png"]; image = [UIImage imageWithContentsOfFile:path]; [_gameBtn setImage:image forState:UIControlStateHighlighted];//图片在点击的时候响应 [_gameBtn addTarget:self action:@selector(ButtonClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_gameBtn]; path = nil; image = nil;下面还有响应事件的代码:
-(IBAction)ButtonClick:(id)sender{ UIButton *whichOne = (UIButton *)sender; if (_gameBtn == whichOne) { NSLog(@"You Clicked _gameBtn"); } }
本文介绍如何使用UIButton在iOS应用中实现两种状态的图片切换:一种为默认状态下的图片,另一种为按钮被按下时显示的图片。通过加载不同资源文件并设置对应的状态,实现了按钮状态改变时的视觉反馈。
287

被折叠的 条评论
为什么被折叠?



