# MSStarEvaluator **Repository Path**: mlsRepository/MSStarEvaluator ## Basic Information - **Project Name**: MSStarEvaluator - **Description**: 五星评价 - **Primary Language**: Objective-C - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2017-07-18 - **Last Updated**: 2020-12-20 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README #MSStarEvaluator 五星评价 // // MSStarEvaluator.h // Takeaway // // Created by iOS on 2017/7/18. // Copyright © 2017年 MS. All rights reserved. // #import /** 五星评价 */ @interface MSStarEvaluator : UIControl /** 隐式属性动画 */ @property (nonatomic) BOOL animation; @property (copy, nonatomic) void(^starEvaluatorValueBlock)(CGFloat currentValue); /** 重写初始化方法 @param frame frame @param emptyImgName 下层星星图片名字 @param fullImgName 上星星图片名字 @return 五星评价器 */ - (instancetype)initWithFrame:(CGRect)frame emptyImgName:(NSString *)emptyImgName fullImgName:(NSString *)fullImgName; /** 设置初始星级 @param initialValue 0-5 @param block 星级变化回调 */ - (void)setupInitialValue:(CGFloat)initialValue handle:(void(^)(CGFloat currentValue))block; @end // // MSStarEvaluator.m // Takeaway // // Created by iOS on 2017/7/18. // Copyright © 2017年 MS. All rights reserved. // #import "MSStarEvaluator.h" #define Space 8.0f @interface MSStarEvaluator () { float aWidth; //一个星星+间隙的宽度 float aStarWidth; //一个星星的宽度 NSMutableArray *fullStarArray; } @property (nonatomic) float currentValue; @property (strong, nonatomic) NSString *emptyImgName, *fullImgName; @end @implementation MSStarEvaluator - (instancetype)initWithFrame:(CGRect)frame emptyImgName:(NSString *)emptyImgName fullImgName:(NSString *)fullImgName { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor clearColor]; _emptyImgName = emptyImgName; _fullImgName = fullImgName; [self loadSubviews]; } return self; } - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor clearColor]; _emptyImgName = @"StarEvaluator_StarEmpty"; _fullImgName = @"StarEvaluator_Star"; [self loadSubviews]; } return self; } - (void)loadSubviews { fullStarArray = [NSMutableArray arrayWithCapacity:5]; for (int i = 0; i < 5; i ++) { CGFloat width = (self.bounds.size.width - Space * 4) / 5; aStarWidth = width; aWidth = width + Space; UIImageView *emptyImgView = [[UIImageView alloc] initWithFrame:CGRectMake(i * (width + Space), 0, width, width)]; emptyImgView.image = [UIImage imageNamed:_emptyImgName]; [self addSubview:emptyImgView]; UIImageView *fullImgView = [[UIImageView alloc] initWithFrame:CGRectMake(i * (width + Space), 0, width, width)]; fullImgView.contentMode = UIViewContentModeScaleToFill; fullImgView.image = [UIImage imageNamed:_fullImgName]; CALayer *layer = [CALayer layer]; layer.frame = CGRectMake(0, 0, 0, 0); layer.backgroundColor = [UIColor blackColor].CGColor; fullImgView.layer.mask = layer; [self addSubview:fullImgView]; fullImgView.layer.mask.frame = CGRectMake(0, 0, 0, width); [fullStarArray addObject:fullImgView]; } } - (void)setCurrentValue:(float)currentValue { _currentValue = currentValue; [self setNeedsDisplay]; } - (void)setupInitialValue:(CGFloat)initialValue handle:(void (^)(CGFloat))block { self.currentValue = initialValue; self.starEvaluatorValueBlock = block; } - (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event { CGPoint touchPoint = [touch locationInView:self]; int n = (int)(touchPoint.x / aWidth); // 整颗星数 float f = (touchPoint.x - n * Space - n * aStarWidth) / aStarWidth; // 半颗星比例 f = MIN(f, 1.0f); self.currentValue = n + f; return YES; } - (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event { CGPoint touchPoint = [touch locationInView:self]; int c = (int)(touchPoint.x / aWidth); // 整颗星数 float f = (touchPoint.x - c * Space - c * aStarWidth) / aStarWidth; // 半颗星比例 f = MIN(f, 1.0f); self.currentValue = c + f; return YES; } // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code [CATransaction setDisableActions:!_animation]; int c = (int)_currentValue; float f = _currentValue - c; for (int i = 0; i < 5; i++) { UIImageView *fullImgView = fullStarArray[i]; if (i < c) { // 整颗星 fullImgView.layer.mask.frame = CGRectMake(0, 0, aStarWidth, aStarWidth);; } else if (i == c) { fullImgView.layer.mask.frame = CGRectMake(0, 0, aStarWidth*f, aStarWidth); } else { fullImgView.layer.mask.frame = CGRectZero; } } if (self.starEvaluatorValueBlock) { self.starEvaluatorValueBlock(_currentValue); } } @end