iOS开发

iOS 星星评分控件

2017-09-22  本文已影响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);

}

上一篇下一篇

猜你喜欢

热点阅读