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

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

#import <UIKit/UIKit.h>

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<UITouch *> *)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<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self touchesEnded:touches withEvent:event];
}

@end

代码下载

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 渐变的面目拼图要我怎么拼? 我是疲乏了还是投降了? 不是不允许自己坠落, 我没有滴水不进的保护膜。 就是害怕变得面...
    闷热当乘凉阅读 4,368评论 0 13
  • 夜莺2517阅读 127,780评论 1 9
  • 版本:ios 1.2.1 亮点: 1.app角标可以实时更新天气温度或选择空气质量,建议处女座就不要选了,不然老想...
    我就是沉沉阅读 7,000评论 1 6
  • 我是一名过去式的高三狗,很可悲,在这三年里我没有恋爱,看着同龄的小伙伴们一对儿一对儿的,我的心不好受。怎么说呢,高...
    小娘纸阅读 3,445评论 4 7
  • 那一年,我选择了独立远行,火车带着我在前进的轨道上爬行了超过23个小时; 那一年,我走过泥泞的柏油路,在那个远离故...
    木芽阅读 1,682评论 4 5