效果图.png
[self setStar:3.7];
- (void)setStar:(CGFloat)num{
NSString *nu = [NSString stringWithFormat:@"%.1f",num];
NSArray *array = [nu componentsSeparatedByString:@"."];
NSInteger number = [array[0] integerValue];;
CGFloat point = [array[1] integerValue]/10.0;
for (int i = 0; i < 5; i ++) {
UIImageView *image = [[UIImageView alloc]init];
if (i < number) {
image.image = IMGNAME(@"星");
} else{
image.image = IMGNAME(@"星 (1)");
}
if (i == number && point != 0) {
image = [ZQTools cutImage:IMGNAME(@"星") fraction:point];
}
[self.viewWithAppraise addSubview:image];
[image mas_makeConstraints:^(MASConstraintMaker *make) {
MLEFT(13 * i);
make.centerY.mas_equalTo(0);
MHEIGHT(11);
MWIDTH(11);
}];
}
}
图片处理。思路是把红色图片截取,然后截取出的图片放到imageView上。imageView的width = 图片image.width * 相应的小数,其他参数相同。再用一个imageView放背景图,两个imageView重叠。
+ (UIImageView *)cutImage:(UIImage *)image fraction:(CGFloat)fractonPart{
CGFloat width = image.size.width * fractonPart * image.scale;
CGRect newFrame = CGRectMake(0, 0, width , image.size.height * image.scale);
CGImageRef resultImage = CGImageCreateWithImageInRect(image.CGImage, newFrame);
UIImage *result = [UIImage imageWithCGImage:resultImage scale:image.scale orientation:image.imageOrientation];
CGImageRelease(resultImage);
UIImageView *imageNo = [[UIImageView alloc]init];
imageNo.frame = CGRectMake(0, 0, ADAPTATION_WIDTH(11), ADAPTATION_HEIGHT(11));//ADAPTATION_WIDTH、HEIGHT宽高自适应。
imageNo.image = IMGNAME(@"星 (1)");
UIImageView *imageNew = [[UIImageView alloc]init];
imageNew.image = result;
imageNew.frame = CGRectMake(0, 0, ADAPTATION_WIDTH(11 * fractonPart), ADAPTATION_HEIGHT(11));
[imageNo addSubview:imageNew];
return imageNo;
}
所有的星星图片加载到view上,星星图片宽高为11。