如何实现
1.经常在很多APP上可以看到各种评分五角星显示,那么该如何去做呢?很简单,只需要提供三种UI的星星ICON,就可以完成。首先我们去自定义一个UIView的实体类。@implementation SXStarView
/**
* 星星评分
*/
@implementation SXStarView
具体实现
/**
* 初始化
*
* @param frame 坐标
* @param statNum 数字
*
* @return UIView
*/
- (instancetype)initWithFrame:(CGRect)frame andStarNum: (float)statNum
{
if ([super initWithFrame:frame])
{
CGFloat width = self.frame.size.width/5;
for (NSInteger i = 0; i<5; i++)
{
UIImageView * starImg = [[UIImageView alloc]initWithFrame:CGRectMake(width *i, 0, width, width)];
//灰色星星
starImg.image = [UIImage imageNamed:@"ico_star"];
[self addSubview:starImg];
UIImageView * starImg1 = [[UIImageView alloc]initWithFrame:CGRectMake(width *i, 0, width, width)];
//实心星星
starImg1.image = [UIImage imageNamed:@"ico_star1"];
starImg1.hidden = YES;
[self addSubview:starImg1];
UIImageView * starImg2 = [[UIImageView alloc]initWithFrame:CGRectMake(width *i, 0, width, width)];
//半星星星
starImg2.image = [UIImage imageNamed:@"ico_star3"];
starImg2.hidden = YES;
[self addSubview:starImg2];
if (i<statNum)
{
if(statNum == i+0.5)
{
starImg2.hidden = NO;
}
else
{
starImg1.hidden = NO;
}
}
}
}
return self;
}