iOS星級(jí)評(píng)分效果實(shí)現(xiàn)

在開(kāi)發(fā)中我們經(jīng)常需要實(shí)現(xiàn)評(píng)價(jià)星級(jí)的功能,這里分享一個(gè)評(píng)星效果的實(shí)現(xiàn)過(guò)程。

效果:

Demo地址:https://github.com/JerryLMJ/LMJGradeStarsControl
如果此demo幫助到你,請(qǐng)賜給一顆star,你的鼓勵(lì)是我coding的動(dòng)力

具體實(shí)現(xiàn):

@implementation LMJGradeStarsControl
{
    NSInteger _defaultIndex;
    NSInteger _totalStars;
    CGFloat   _size;
    
    NSMutableArray * _starsBtnArr;
}

- (id)initWithFrame:(CGRect)frame totalStars:(NSInteger)totalStars starSize:(CGFloat)size{
    self = [self initWithFrame:frame defaultSelectedStatIndex:(LMJGradeStarsControlStartIndex-1) totalStars:totalStars starSize:size];
    return self;
}

- (id)initWithFrame:(CGRect)frame defaultSelectedStatIndex:(NSInteger)index totalStars:(NSInteger)totalStars starSize:(CGFloat)size{
    self = [super initWithFrame:frame];
    _defaultIndex = index;
    _totalStars   = totalStars;
    _size         = size;
    
    if (self) {
        _starsBtnArr = [NSMutableArray array];
        [self buildStars];
    }
    return self;
}


- (void)buildStars{
    CGFloat starDistance = self.frame.size.width / (_totalStars +1);
    
    int i = 0;
    
    for (int index = LMJGradeStarsControlStartIndex; index < (LMJGradeStarsControlStartIndex +_totalStars); index++) {
        
        UIButton * starBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [starBtn setFrame:CGRectMake(0, 0, _size +10, _size +10)];
        [starBtn setCenter:CGPointMake(starDistance * (i+1), self.frame.size.height/2)];
        [starBtn setBackgroundColor:[UIColor clearColor]];
        if (index <= _defaultIndex) {
            [starBtn setImage:[UIImage imageNamed:@"star_on"] forState:UIControlStateNormal];
            [starBtn setImage:[UIImage imageNamed:@"star_on"] forState:UIControlStateHighlighted];
        }else{
            [starBtn setImage:[UIImage imageNamed:@"star_off"] forState:UIControlStateNormal];
            [starBtn setImage:[UIImage imageNamed:@"star_off"] forState:UIControlStateHighlighted];
        }
        
        [starBtn setImageEdgeInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
        [starBtn setTag:index];
        [starBtn setHighlighted:NO];
        [starBtn addTarget:self action:@selector(clickStarBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:starBtn];
        
        [_starsBtnArr addObject:starBtn];
        
        i++;
    }
}


- (void)clickStarBtn:(UIButton *)button{
    [self clickStarBtnActionWithBtnTag:button.tag];
}

- (void)clickStarBtnActionWithBtnTag:(NSInteger)tag{
    for (int i = 0; i < _starsBtnArr.count; i++) {
        UIButton * btn = _starsBtnArr[i];
        
        if (btn.tag <= tag) {
            [btn setImage:[UIImage imageNamed:@"star_on"] forState:UIControlStateNormal];
            [btn setImage:[UIImage imageNamed:@"star_on"] forState:UIControlStateHighlighted];;
        }else{
            [btn setImage:[UIImage imageNamed:@"star_off"] forState:UIControlStateNormal];
            [btn setImage:[UIImage imageNamed:@"star_off"] forState:UIControlStateHighlighted];
        }
    }
    
    if ([self.delegate respondsToSelector:@selector(gradeStarsControl:selectedStars:)]) {
        [self.delegate gradeStarsControl:self selectedStars:(tag)];
    }
}

- (void)setSelectedStarIndex:(NSUInteger)index{
    [self clickStarBtnActionWithBtnTag:index];
}

Demo地址:
https://github.com/JerryLMJ/LMJGradeStarsControl
如果此demo幫助到你,請(qǐng)賜給一顆star,你的鼓勵(lì)是我coding的動(dòng)力

版權(quán)聲明:出自MajorLMJ技術(shù)博客的原創(chuàng)作品 ,轉(zhuǎn)載時(shí)必須注明出處及相應(yīng)鏈接!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,063評(píng)論 25 709
  • 太長(zhǎng)了,還是轉(zhuǎn)載吧...今天在看博客的時(shí)候,無(wú)意中發(fā)現(xiàn)了@Trinea在GitHub上的一個(gè)項(xiàng)目Android開(kāi)源...
    龐哈哈哈12138閱讀 20,394評(píng)論 3 283
  • 第一次翻開(kāi)的時(shí)候,看了眼開(kāi)頭幾頁(yè),又合上了,覺(jué)得太過(guò)枯燥。 今天再把這本書(shū)翻出來(lái),看到數(shù)據(jù)推動(dòng)人工智能發(fā)展這一節(jié),...
    心里住了一只羊閱讀 730評(píng)論 0 3
  • 央視國(guó)際中文頻道《真功夫》節(jié)目,展示了太極拳以柔克剛、出神入化的搏擊功夫,提到一個(gè)關(guān)鍵詞:得意忘形。留得...
    王進(jìn)會(huì)閱讀 823評(píng)論 0 0
  • 昨天看班主任在群里發(fā)的要穿長(zhǎng)袖衣服,心里還想這冷天的不會(huì)有穿短袖校服的了。昨晚看到孩子書(shū)包鼓鼓的,拉開(kāi)拉鏈...
    李璽辰媽媽閱讀 198評(píng)論 0 2

友情鏈接更多精彩內(nèi)容