最近产品大大要求个功能是要计算显示用户的等级 在网上找了找好多都感觉好复杂 然后我们大神整出了一个种方案身为渣渣的我顿时茅舍顿开 下面我说说大致的思路哈 不对的请大家指出来哈(大神请绕道)
先把效果图整出来
创建一个数组来接收名称
_LevelNameList = @[@"5块",@"10块",@"15块",@"20块",@"25块",@"30块",@"35块",@"40块",@"45块"];
接下来处理下简单的逻辑
创建一个变量来控制等级
int level = 0;
if (level < 1) {
level = 1;
}
if (level > 9) {
level = 9;
}
在创建一个int变量来控制按钮的变化 我以3个为一个等级
int index = 0;
if (level < 3) {
index = 0;
}else if (level>7){
index = level - (5 - (9 - level));
}else{
index = level - 2 - 1;
}
然后遍历所需要的按钮
NSArray * buttonArray = @[_button1,_button2,_button3,_button4,_button5];
for (int i = index; i<index+5; i++) {
NSString *title = _LevelNameList[i];
UIButton * button = buttonArray[i - index];
[button setTitle:title forState:UIControlStateNormal];
if (i < level) {
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
}
//获取出需要变换的btn的宽
double width = 33.5f + 71.5 * (level - 1 - index);
if (level == 9) {
width = 33.5f + 71.5 * (level - 1 - index) + 33.5f;
}
UIImage *image = [UIImage imageNamed:@"userlevel_levelline_yellow"];
image = [self ct_imageFromImage:image inRect:CGRectMake(0, 0, width, image.size.height)];
_imageView_Line.image = image;
if (level == 9) {
width = kScreenWidth / 375 * 345;
} else {
width = 33.5f * (kScreenWidth / 375) + 71.5 * (level - 1 - index) * (kScreenWidth / 375);
}
_constraint_LineWidth.constant = width;
打完收工