iOS 星级评价(类似美团、滴滴的服务评价)

之前项目中有用到星级评价,模仿美团的订单评价写了个小demo,效果如下
ZJYStarRateView.gif
  • 代码实现
    以为需求不确定有几行评价结果,然后我是按一行一个View处理的,上图对应的是四个View,后续有时间会再调整下。😭
#pragma mark - private Method
-(void)createStarView{
    self.testVC.view.backgroundColor = [UIColor redColor];
 
    self.backgroundColor = [UIColor whiteColor];
    if ([self.subviews containsObject:self.foregroundStarView]) {
        [self.foregroundStarView removeFromSuperview];
    }
    
    if ([self.subviews containsObject:self.backgroundStarView]) {
        [self.backgroundStarView removeFromSuperview];
    }
    
    self.foregroundStarView = [self createStarViewWithImage:ForegroundStarImage];
    self.backgroundStarView = [self createStarViewWithImage:BackgroundStarImage];
    
    CGFloat foregroundStarViewWidth = 0.;
    if (_currentScore > 0) {
        foregroundStarViewWidth = _currentScore * StartWidth + (ceilf(_currentScore) - 1) * _starSpaceing;
    } else {
        foregroundStarViewWidth = 0;
    }
    
    self.foregroundStarView.frame = CGRectMake(0, 0, foregroundStarViewWidth, self.bounds.size.height);
    
    [self addSubview:self.backgroundStarView];
    [self addSubview:self.foregroundStarView];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userTapRateView:)];
    tapGesture.numberOfTapsRequired = 1;
    [self addGestureRecognizer:tapGesture];
    
}

- (UIView *)createStarViewWithImage:(NSString *)imageName {
    UIView *view = [[UIView alloc] initWithFrame:self.bounds];
    view.clipsToBounds = YES;
    view.backgroundColor = [UIColor clearColor];
    for (NSInteger i = 0; i < self.numberOfStars; i ++)
    {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
        imageView.frame = CGRectMake(i * (StartWidth + _starSpaceing), 0, StartWidth, self.bounds.size.height);
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        [view addSubview:imageView];
    }
    return view;
}

- (void)userTapRateView:(UITapGestureRecognizer *)gesture {
    CGPoint tapPoint = [gesture locationInView:self];
    CGFloat offset = tapPoint.x;
    //    CGFloat realStarScore = offset / (((StartWidth + _starSpaceing) * self.numberOfStars - _starSpaceing) / self.numberOfStars);
    
    //一个星+一个间距为单位 向下取整
    CGFloat floorStarScore = floorf(offset / (StartWidth + _starSpaceing));
    
    //多于一个完整星(星宽+间距)外的宽度
    CGFloat excessWidth = offset - floorStarScore * (StartWidth + _starSpaceing);
    
    //真实星数目
    CGFloat realStarScore = (offset - floorStarScore * _starSpaceing) / StartWidth;
    ;
    
    if (excessWidth > StartWidth) { //多的是间距。向下取整
        realStarScore = floorf(realStarScore);
    }
    
    switch (_rateStyle) {
        case ZJYWholeStar:
        {
            self.currentScore = ceilf(realStarScore);
            break;
        }
        case ZJYHalfStar:
            self.currentScore = roundf(realStarScore)>=realStarScore ? ceilf(realStarScore):(ceilf(realStarScore)-0.5);
            break;
        case ZJYIncompleteStar:
            self.currentScore = realStarScore;
            break;
        default:
            break;
    }
    
}

  • 使用
ZJYStarRateView *starRateView = [[ZJYStarRateView alloc] initWithFrame:CGRectMake(20, 100, 200, 30)];
    
    starRateView.isAnimation = YES;
    starRateView.rateStyle = ZJYIncompleteStar;
    starRateView.tag = 1;
    starRateView.currentScore = 1.2;
    starRateView.starSpaceing = StarSpace;
    starRateView.delegate = self;
    [self.view addSubview:starRateView];
    
    ZJYStarRateView *starRateView2 = [[ZJYStarRateView alloc] initWithFrame:CGRectMake(20, 140, 200, 30) numberOfStars:5 rateStyle:ZJYHalfStar isAnination:YES delegate:self];
    starRateView2.tag = 2;
    starRateView2.starSpaceing = StarSpace;
    
    [self.view addSubview:starRateView2];
    
    ZJYStarRateView *starRateView3 = [[ZJYStarRateView alloc] initWithFrame:CGRectMake(20, 180, 200, 30) finish:^(CGFloat currentScore) {
        NSLog(@"3----  %f",currentScore);
    }];
    starRateView3.starSpaceing = StarSpace;
    
    [self.view addSubview:starRateView3];
    
    ZJYStarRateView *starRateView4 = [[ZJYStarRateView alloc] initWithFrame:CGRectMake(20, 220, 200, 30) numberOfStars:5 rateStyle:ZJYHalfStar isAnination:YES finish:^(CGFloat currentScore) {
        NSLog(@"4----  %f",currentScore);
        
    }];
    starRateView4.testVC = self;
    starRateView4.starSpaceing = StarSpace;
    
    [self.view addSubview:starRateView4];

#pragma mark -ZJYStarRateViewDelegate
-(void)starRateView:(ZJYStarRateView *)starRateView currentScore:(CGFloat)currentScore{
        NSLog(@"%ld----  %f",starRateView.tag,currentScore);
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 180,325评论 25 708
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 14,532评论 2 59
  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,368评论 3 119
  • 放 弃 热寒 有时 放弃 比得到 更加 不容易 放弃 久逢的相恋 需要勇气 放弃 未竟的事业 需要魄力 放弃 临顶...
    温热寒阅读 224评论 0 1
  • 孤独的台灯,柔弱的光 在萨克斯的音乐中趴着 素麻的桌布贴在脸上 无意翻动手中的书页 薄荷味的蚊香片四处飘散 我在想什么?
    比奇阅读 214评论 0 1

友情链接更多精彩内容