首先創(chuàng)建一個(gè)StarView
#import "StarView.h"
@implementation StarView
{
UIImageView * _foregroundImageView;// 前景圖
UIImageView * _backgroundImageView; // 背景圖
}
// 創(chuàng)建視圖
- (void)createViews
{
_backgroundImageView = [[UIImageView alloc] init];
[self addSubview:_backgroundImageView];
// 自動(dòng)布局
_backgroundImageView.translatesAutoresizingMaskIntoConstraints = NO;
[_backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self);
make.top.equalTo(self);
make.width.equalTo(@46);
make.height.equalTo(@12);
}];
// 設(shè)置背景圖屬性
_backgroundImageView.image = [UIImage imageNamed:@"star_gray"];
// 設(shè)置圖片顯示模式
_backgroundImageView.contentMode = UIViewContentModeLeft;
// 前景圖
_foregroundImageView = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:@"star_hight"]];
[self addSubview:_foregroundImageView];
[_foregroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(_backgroundImageView);
}];
// 設(shè)置圖片顯示模式
_foregroundImageView.contentMode = UIViewContentModeLeft;
// 裁剪
_foregroundImageView.clipsToBounds = YES;
}
// 重寫init方法
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self createViews];
}
return self;
}
// 當(dāng)在Xib或者Storyboard中關(guān)聯(lián)類時(shí),程序從xib或者storyboard創(chuàng)建對象時(shí)赤拒,會調(diào)用該方法
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
[self createViews];
}
return self;
}
-(void)setStarValue:(CGFloat)starValue
{
_starValue = starValue;
if (_starValue >= 0 && _starValue <= 5)
{
// 重建約束
[_foregroundImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(_backgroundImageView);
make.top.equalTo(_backgroundImageView);
make.height.equalTo(_backgroundImageView);
make.width.equalTo(_backgroundImageView).multipliedBy(_starValue/5);
}];
}
// NSLog(@"starValue is %0.1f",_starValue/5);
}
#import <UIKit/UIKit.h>
@interface StarView : UIView
@property (nonatomic, assign) CGFloat starValue; // 星標(biāo)的值
@end
最后在要顯示星級評價(jià)的界面將其帶入
/**星級*/
// 設(shè)置星標(biāo)的值
self.calenderStartView.starValue =[_calendarModel.star floatValue];
最后來一張顯示圖
IMG_1688.PNG