美文网首页iOS开发
iOS 星星评分控件

iOS 星星评分控件

作者: 乱尘 | 来源:发表于2017-09-22 11:15 被阅读50次

    简单的自用星星控件,有空再加上手势

    星星的间距取的是星星的宽度的五分之一,view的宽度等于五个星星加上四个空隙

    - (UIView *)starWithfloat:(CGFloat)score frame:(CGRect)frame{

    UIView *contentV = [[UIView alloc] initWithFrame:frame];

    CGFloat starW = frame.size.width*(5.0/(5*5+4*1));        //星星与星星间距5:1

    CGFloat starH = frame.size.height;

    CGFloat starSpace = frame.size.width*(1.0/(5*5+4*1));

    for (int i = 0; i<2; i++) {

    NSString *starImageName;

    switch (i) {

    case 0:

    starImageName = @"home_floa_icon_stars5";

    break;

    case 1:

    starImageName = @"home_floa_icon_stars1";

    break;

    default:

    break;

    }

    UIView *bg = [[UIView alloc] initWithFrame:frame];

    bg.tag = 100+i;

    [contentV addSubview:bg];

    for (int j = 0; j<5; j++) {

    UIImageView *star = [[UIImageView alloc] initWithFrame:CGRectMake(j*(starW+starSpace), 0, starW, starH)];

    star.image = [UIImage imageNamed:starImageName];

    [bg addSubview:star];

    }

    if (i == 1) {

    NSInteger count = floor(score);

    CGFloat bg2 = count*(starW+starSpace)+(score-count)*starW;

    bg.frame = CGRectMake(0, 0, bg2, frame.size.height);

    bg.clipsToBounds = YES;

    bg.layer.masksToBounds = YES;

    }

    }

    return contentV;

    }

    - (void)refreshStarView:(UIView *)starView Score:(CGFloat)score {

    UIView *star_bg2 = [starView viewWithTag:101];

    CGRect frame = starView.bounds;

    CGFloat starW = frame.size.width*(5.0/(5*5+4*1));        //星星与星星间距5:1

    CGFloat starSpace = frame.size.width*(1.0/(5*5+4*1));

    NSInteger count = floor(score);

    CGFloat bg2 = count*(starW+starSpace)+(score-count)*starW;

    star_bg2.frame = CGRectMake(0, 0, bg2, frame.size.height);

    }

    相关文章

      网友评论

        本文标题:iOS 星星评分控件

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