类似京东淘宝的五星评级,需要的看一下,找找思路
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self setButton];
}
-(void)setButton
{
buttonArray=[[NSMutableArray alloc]init];//一个全局变量 自行设置
for (NSInteger i=0; i<5; i++) {
UIButton *star=[[UIButton alloc]initWithFrame:CGRectMake(52*i, 100, 50, 50)];
star.tag=i;
star.backgroundColor=[UIColor whiteColor];
star.clipsToBounds=YES;
//大家用的时候删掉下面三行 换成设置默认图片(未选定状态的)
star.layer.cornerRadius=star.frame.size.width/2;
star.layer.borderColor=[UIColor redColor].CGColor;
star.layer.borderWidth=1;
[star addTarget:self action:@selector(starClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:star];
[buttonArray addObject:star];
}
}
-(void)starClicked:(UIButton *)sender
{
//我们只是 设置了背景色,可以换成是梗概背景图片>>>[button setBackgroundImage:<#(UIImage *)#> forState:<#(UIControlState)#>];
for (NSInteger i=0; i<=sender.tag; i++) {
[(UIButton*)buttonArray[i] setBackgroundColor:[UIColor redColor]];
}
for (NSInteger j=4; j>sender.tag; j--) {
[(UIButton*)buttonArray[j] setBackgroundColor:[UIColor whiteColor]];
}
}