【iOS】怎么做一個五角星等級顯示

Simulator Screen Shot 2016年3月18日 上午10.34.27.png

如何實現(xiàn)

1.經(jīng)常在很多APP上可以看到各種評分五角星顯示,那么該如何去做呢?很簡單,只需要提供三種UI的星星ICON,就可以完成。首先我們?nèi)プ远x一個UIView的實體類。@implementation SXStarView

/**
 *  星星評分
 */
@implementation SXStarView

具體實現(xiàn)

/**
 *  初始化
 *
 *  @param frame   坐標
 *  @param statNum 數(shù)字
 *
 *  @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;
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容