iOS 开发 一个既可用于展示的⭐️又可用于点击评价的⭐️View

最近项目中需要评分⭐️,2种用法,1.只需要展示,不需要点击评分;2.需要点击评分。
HDScoreStarView.h

#import 

NS_ASSUME_NONNULL_BEGIN

@interface HDScoreStarView : UIView
/*
 *@pramas  spacing 星星之间的间距
 */
@property (nonatomic, assign, readwrite) CGFloat spacing;

/*
 *@pramas  starCount 星星需要设置成的数量
 大小为:1~5,超过5则置为5
 默认不设置,用于点击评分
 设置了则表示用于展示评分,不可点击
 */
@property (nonatomic, assign, readwrite) NSUInteger starCount;

/*
 *@pramas  tapEnabled 关闭星星点击手势
 */
@property (nonatomic, assign, readwrite, getter=isTapEnabled) BOOL tapEnabled;
/// 点击了几个星星
@property(nonatomic,strong) void(^indexCount)(NSUInteger indexCount);

@end

NS_ASSUME_NONNULL_END

HDScoreStarView.m

#import "HDScoreStarView.h"
@interface HDScoreStarView()
@property (nonatomic, assign) NSUInteger index;
//需要替换成自己项目中的星星☆图片名称
@property (nonatomic, copy) NSString *imageNameNor; //图片名称☆
@property (nonatomic, copy) NSString *imageNameSel; //选中的图片名称⭐️
@end
@implementation HDScoreStarView

#pragma mark - init

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        //初始化 用于评价
        self.imageNameNor = @"common_icon_star64_sel";
        self.imageNameSel = @"common_icon_star64_nor";
    };
    return self;
}
- (void)setStarCount:(NSUInteger)starCount {
    if (starCount == 0) {
        return;
    }
    if (_starCount != starCount) {
        _starCount = starCount;
        if (starCount > 5) {
            starCount = 5;
        }
        self.index = starCount;
        [self setNeedsDisplay];
        //设置了startCount  则不能点击 只做展示用
        self.tapEnabled = NO;
        self.imageNameNor = @"common_icon_star32_sel";
        self.imageNameSel = @"common_icon_star32_nor";
    }
}

- (void)setTapEnabled:(BOOL)tapEnabled {
    _tapEnabled = tapEnabled;
    self.userInteractionEnabled = tapEnabled;
}

#pragma mark - 绘制
- (void)drawRect:(CGRect)rect {
    UIImage *norImage = [UIImage imageNamed:self.imageNameNor];
    UIImage *selImage = [UIImage imageNamed:self.imageNameSel];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGFloat spacing = self.spacing;
    CGFloat starWidth = (self.frame.size.width - (spacing *4)) / 5;
    for (NSInteger i = 0; i < 5; i ++) {
        UIImage *drawImage = i < self.index ? selImage : norImage;
        [self drawImage:context CGImageRef:drawImage.CGImage CGRect:CGRectMake((starWidth + spacing) * i, 0, starWidth, self.frame.size.height)];
    }
}

- (void)drawImage:(CGContextRef)context
       CGImageRef:(CGImageRef)image
           CGRect:(CGRect)rect {
    CGContextSaveGState(context);
    
    CGContextTranslateCTM(context, rect.origin.x, rect.origin.y);
    CGContextTranslateCTM(context, 0, rect.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextTranslateCTM(context, -rect.origin.x, -rect.origin.y);
    CGContextDrawImage(context, rect, image);
    
    CGContextRestoreGState(context);
}


#pragma mark - touch

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    self.index = point.x / (self.frame.size.width / 5) + 1;
    if (self.index == 6) {
        self.index --;
    }
    [self setNeedsDisplay];
    if (self.indexCount) {
        self.indexCount(self.index);
    }
    
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [self touchesEnded:touches withEvent:event];
}

@end

代码下载

你可能感兴趣的:(iOS 开发 一个既可用于展示的⭐️又可用于点击评价的⭐️View)