iOS评分星星允许半颗

作者: 超_iOS | 来源:发表于2016-04-01 17:50 被阅读1402次

你的喜欢是我的坚持的动力!

如图:就这种 Paste_Image.png
思路是黑星是背景,黄星在上边,根据分数来改变黄星的frame,上代码,.花费不小的精力,算是为这种需求提供些新思路,也许对大家有用,有用就点赞谢谢.不多说了

.m如下


#import "ZCStar.h"
#define FOREGROUND_STAR_IMAGE_NAME @"b27_icon_star_yellow"
#define BACKGROUND_STAR_IMAGE_NAME @"b27_icon_star_gray"
#define DEFALUT_STAR_NUMBER 5
@interface ZCStar   ()
@property (nonatomic, strong) UIView *foregroundStarView;
@property (nonatomic, strong) UIView *backgroundStarView;
@property (nonatomic, assign) NSInteger numberOfStars;
@end
@implementation ZCStar

- (instancetype)initWithFrame:(CGRect)frame
{
    return [self initWithFrame:frame numberOfStars:DEFALUT_STAR_NUMBER];
}
- (instancetype)initWithFrame:(CGRect)frame numberOfStars:(NSInteger)numberOfStar
{
    if (self = [super initWithFrame:frame]) {
        _numberOfStars = numberOfStar;
        [self buildDataAndUI];
    }
    return self;
}
- (void)buildDataAndUI
{
    _scorePercent = 1;
    self.foregroundStarView = [self createStarViewWithImage:FOREGROUND_STAR_IMAGE_NAME];
    self.backgroundStarView = [self createStarViewWithImage:BACKGROUND_STAR_IMAGE_NAME];
    
    [self addSubview:self.backgroundStarView];
    [self addSubview:self.foregroundStarView];
}
- (UIView *)createStarViewWithImage:(NSString *)imageName
{
    UIView *view = [[UIView alloc] initWithFrame:self.bounds];
    view.clipsToBounds = YES;
    view.backgroundColor = [UIColor clearColor];
    for (int i = 0; i < self.numberOfStars; i++) {
        UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
        imgView.frame = CGRectMake(i * self.bounds.size.width / self.numberOfStars, 0, self.bounds.size.width / self.numberOfStars, self.bounds.size.height );
        imgView.contentMode = UIViewContentModeScaleAspectFit;
        [view addSubview:imgView];
    }
    return view;
}
- (void)setScorePercent:(CGFloat)scorePercent
{
    if (_scorePercent == scorePercent) {
        return;
    }
    if (scorePercent < 0) {
        _scorePercent = 0;
        
    } else if (scorePercent > 1) {
        _scorePercent = 1;
    } else {
        _scorePercent = scorePercent;
    }
     self.foregroundStarView.frame = CGRectMake(0,0,self.bounds.size.width * self.scorePercent , self.bounds.size.height);
}

.h如下

@interface ZCStar : UIView
@property (nonatomic, assign)CGFloat scorePercent;//0到1,评分

- (instancetype)initWithFrame:(CGRect)frame numberOfStars:(NSInteger)numberOfStar;
@end

如何使用呢?一句话

 ZCStar *view = [[ZCStar alloc] initWithFrame:CGRectMake(100, 100, 200, 100) numberOfStars:5];//星星个数
    [self.view addSubview:view];
    view.scorePercent = 0.48;//评分0 到1 

相关文章

  • iOS评分星星允许半颗

    你的喜欢是我的坚持的动力! .m如下 .h如下 如何使用呢?一句话

  • iOS 应用内评分

    iOS 应用内评分 iOS10.3允许开发者敦促用户在 App Store 上对应用进行评分。整个评分过程直接在 ...

  • 五星展示

    评分一般都是一个数,假如评分只有整数星星 没有半颗星,首先要把数变成一个用1或者0代替星星有无的数组 然后在页面进...

  • 代码:大精度转化为小精度

    需求:项目中有一个星星评分,要求不能显示最精确的,只能显示0颗,半颗,1颗.之前写的是精确多位,显示的星星有问题,...

  • iOS 星星评分控件

    简单的自用星星控件,有空再加上手势 星星的间距取的是星星的宽度的五分之一,view的宽度等于五个星星加上四个空隙 ...

  • LBStarView 星星iOS评分

    效果图 Github : https://github.com/LeonLeeboy/LBStarView 只需要...

  • 这个五星评分,半颗星星怎么做功能我们应该怎么编写呢?

    概述 我们在学习微信小程序或者做项目时,应该会遇到五星评分效果情况,那么这个五星评分,半颗星星怎么做功能我们应该怎...

  • 评分功能

    iOS系统中没有自带的星星评分功能,需要自己封装。星星评分功能是整个工程的一个需求,但是代码的封装则是一个代码功底...

  • iOS五星评价(允许半颗星)

    项目中遇到了五星评价的功能,便自定义了一个五星评价的空间,允许半颗星,可以点击、滑动控制。 构建方法如下## 方法...

  • iOS之星星评分(一)

    今天我们说的第一种评分,是不可以点击评分的。 也就是说星星点亮的多少不是我们手动点击的,而是服务器返回一个数字,然...

网友评论

  • 益达哥:这不是TQStar里面的代码么,666666
  • ba1fa7bb7215:很实用,希望作者越来越牛逼,多发表一些
    智能居家:很实用,希望作者越来越牛逼,多发表一些
    与伟大LEE同行:@_超 听起来 Gay里Gay气的
    超_iOS: @iOS_盲僧 你要用双手成就哥的梦想?

本文标题:iOS评分星星允许半颗

本文链接:https://www.haomeiwen.com/subject/iheylttx.html