iOS星星評分兩種方式

樣式1

星星樣式1.png
  • 我們要做的就是圖片上的5顆星星
  1. 根據分數判斷我們該添加什么樣式的星星
 - (void)setLevel:(CGFloat)level{
 _level = level;
    //1.滿星  
    NSInteger fullStarCount = (NSInteger)level;
    for (NSInteger i = 0; i < fullStarCount; i++) {
        //添加滿星
        [self makeStarViewWithImageName:@"full_star" startPosition:i];
        
    }
    //2.半星   
    if (level - fullStarCount) {
        
        [self makeStarViewWithImageName:@"half_star" startPosition:fullStarCount];
        //統計星星目前個數(滿星+半星)
        fullStarCount ++;
    }
     //3.空星
     for (NSInteger i = fullStarCount; i < kStartCount ; i++) {
        [self makeStarViewWithImageName:@"empty_star"  startPosition:i];
    }
 } 
 


2.根據位置設置設置圖片

- (void)makeStarViewWithImageName:(NSString *)imageName startPosition:(NSInteger) position{
    
    //如果重新設置評分,會重新創建imageView 需要判斷 若已經存在5個imageView不去創建
    UIImageView *imageView = nil;
    if (self.subviews.count == kStartCount) {
        
        imageView = self.subviews[position];
        imageView.image = [UIImage imageNamed:imageName];
        
        return;
    }
    
    imageView = [[UIImageView alloc]init];
    imageView.image = [UIImage imageNamed:imageName];
    [imageView sizeToFit];
    imageView.frame = CGRectOffset(imageView.bounds, imageView.bounds.size.width * position, 0);
    [self addSubview:imageView];
    
}


3 .如果不判斷會出現下面的情況


bug.png

樣式2

樣式2.gif
  • 本想通過循環創建5個imageView 但實現不了預期效果

1.加載xib 偷懶~
loadNibNamed: owner: options:
這個方法加載xib會把xib文件加載入緩存 不建議使用

+ (instancetype)loadStarView{
    UINib *nib = [UINib nibWithNibName:@"XHStarView" bundle:nil];
    XHStarView *starView = [[nib instantiateWithOwner:nil options:nil]lastObject];
    return starView;
}

2 .判斷是否添加星星

@implementation XHStarView{
    BOOL _isAddStar;
}

3 . 判斷點擊的區域 (可以根據具體情況設置)


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    if((point.x > self.starOne.x && point.x < (self.starFive.x + self.starFive.width))&&(point.y > self.starOne.y && point.y< self.height)){
        _isAddStar = YES;
        
    }else{
        _isAddStar = NO;
    }
}

4 .移動的時候添加星星

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    
    if(_isAddStar){
        UITouch *touch = [touches anyObject];
        CGPoint point = [touch locationInView:self];
        [self setStarForegroundViewWithPoint:point];

    }

    return;
}

5 .結束的時候禁止添加星星

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if(_isAddStar){
        UITouch *touch = [touches anyObject];
        CGPoint point = [touch locationInView:self];
        [self setStarForegroundViewWithPoint:point];
    }
    _isAddStar = NO;
    return;
}

6 . 根據點擊的位置x設置圖片 并設置滿星 1 分 半星0.5 空星0

-(CGFloat)changeImg:(float)x image:(UIImageView*)img{
    if(x > img.x + img.width/2){
        [img setImage:[UIImage imageNamed:@"StarSelected"]];
        return 1;
    }else if(x > img.x){
        [img setImage:[UIImage imageNamed:@"StarSelectHeaf"]];
        return 0.5;
    }else{
        [img setImage:[UIImage imageNamed:@"StarUnSelect"]];
        return 0;
    }
}

7 . 得出分數

-(void)setStarForegroundViewWithPoint:(CGPoint)point{

    self.score = 0;
    self.score = self.score + [self changeImg:point.x image:self.starOne];
    self.score = self.score + [self changeImg:point.x image:self.starTwo];
    self.score = self.score + [self changeImg:point.x image:self.starThree];
    self.score = self.score + [self changeImg:point.x image:self.starFour];
    self.score = self.score + [self changeImg:point.x image:self.starFive];
    
    //評論最少半星
    if(self.score == 0){
        self.score = 0.5;
        [self.starOne setImage:[UIImage imageNamed:@"StarSelectHeaf"]];
    }
    NSLog(@"分數 %f",self.score);
}

把得出的分數傳給第一個demo就行了

代碼鏈接

還有不足歡迎指教交流

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

推薦閱讀更多精彩內容

  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,229評論 4 61
  • 1 生活總是瑣碎,一地雞毛狀。 偶得一日清閑,女人可能也要面對著家里雜亂的廚房,要分洗的衣服,親戚的雜七雜八……于...
    絢之平淡閱讀 1,344評論 4 24
  • 第八課 微寫作:做推薦 就像上節課的提建議一樣,做推薦也是我們生活中經常遇到的情景。當你在外面玩,吃到非常美味的食...
    田源ty閱讀 2,119評論 0 2