1.用代码或者storyboard创建5个button(现在一般都是5个星星)
我用的是storyboard
记得一定要设置button的tag值
在.h中
@property (weak, nonatomic) IBOutlet UIButton *btn5;
- (IBAction)btn:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *btn4;
@property (weak, nonatomic) IBOutlet UIButton *btn3;
@property (weak, nonatomic) IBOutlet UIButton *btn2;
@property (weak, nonatomic) IBOutlet UIButton *btn1;
然后在.m中
@interface ViewController ()
{
NSArray * starArray;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
starArray = [NSArray arrayWithObjects:_btn1,_btn2,_btn3,_btn4,_btn5, nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)btn:(id)sender
{
UIButton * allBtn =sender;
for (UIButton *button in starArray)
{
if (button.tag <= allBtn.tag)
{
[button setBackgroundImage:[UIImage imageNamed:@"goldstar1"] forState:UIControlStateNormal];
// goldstar1 和 greystar2 图片名称
}
else
{
[button setBackgroundImage:[UIImage imageNamed:@"greystar2"] forState:UIControlStateNormal];
}
}
}
这样就ok 了
效果如下