附上效果圖

效果圖
實(shí)現(xiàn)的功能:
1.可以自定義星星的數(shù)量(默認(rèn)為5顆星)
2.可以初始化設(shè)置星級(jí)(初始化設(shè)置星級(jí)就不能更改星級(jí))
3.實(shí)現(xiàn)了點(diǎn)擊選擇星級(jí)(不可以選擇半星)
使用:只有一個(gè)StarBarView類(lèi)添加即可
初始化默認(rèn)為5顆星星
StarBarView *starBarView = [[StarBarView alloc]init];
starBarView.frame = CGRectMake(10, 600, 300, 60);
[self.view addSubview:starBarView];
初始化設(shè)置星星的數(shù)量
StarBarView *starBarView = [[StarBarView alloc]initWithFrame:CGRectMake(10, 600, 300, 60) startCount:6];
[self.view addSubview:starBarView];
設(shè)置初始化星級(jí)
[starBarView setUpCount:5];
自定義星星的圖片
[starBarView starBarSelImage:@"sel" norImage:@"nol"];
StarBarView.h文件內(nèi)容
#import <UIKit/UIKit.h>
@interface StarBarView : UIView
/** 當(dāng)前星級(jí) */
@property (nonatomic,assign) NSInteger count;
/** 初始化 */
-(instancetype)initWithFrame:(CGRect)frame startCount:(NSInteger)startCount;
/**
初始星級(jí)設(shè)置 如設(shè)置了?初始星級(jí)就不能再改變了
@param count 初始星級(jí)
*/
-(void)setUpCount:(NSInteger)count;
/**
設(shè)置星星圖片
@param selImage 選中的圖片
@param norImage 沒(méi)有選中的圖片
*/
-(void)starBarSelImage:(NSString *)selImage norImage:(NSString *)norImage;
@end
StarBarView.m文件內(nèi)容
#import "StarBarView.h"
#define kviewS self.frame.size
@interface StarBarView ()
@property (nonatomic,assign) NSInteger startCount;
@end
@implementation StarBarView
-(instancetype)initWithFrame:(CGRect)frame startCount:(NSInteger)startCount{
self.startCount = startCount;
return [self initWithFrame:frame];
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor lightGrayColor];
if (self.startCount == 0) {
self.startCount = 5;
}
for (int i = 0; i < self.startCount; i++) {
UIButton *starBtn = [[UIButton alloc]init];
[starBtn setImage:[UIImage imageNamed:@"star_nor"] forState:UIControlStateNormal];
[starBtn setImage:[UIImage imageNamed:@"star_sel"] forState:UIControlStateSelected];
starBtn.userInteractionEnabled = NO;
starBtn.tag = 100 + i;
[self addSubview:starBtn];
}
}
return self;
}
-(void)starBarSelImage:(NSString *)selImage norImage:(NSString *)norImage
{
for (int i = 0; i < self.startCount; i++) {
UIButton *starBtn = (UIButton *)[self viewWithTag:100+i];
[starBtn setImage:[UIImage imageNamed:selImage] forState:UIControlStateNormal];
[starBtn setImage:[UIImage imageNamed:norImage] forState:UIControlStateSelected];
}
}
-(void)layoutSubviews
{
[super layoutSubviews];
CGFloat starBtnW = kviewS.width / self.startCount;
CGFloat starBtnH = kviewS.height;
for (int i = 0; i < self.startCount; i++)
{
UIButton *starBtn = (UIButton *)[self viewWithTag:100+i];
starBtn.frame = CGRectMake(i*starBtnW, 0, starBtnW, starBtnH);
}
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
for (int i = 0; i < self.startCount; i++)
{
UIButton *starBtn = (UIButton *)[self viewWithTag:100+i];
starBtn.selected = NO;
}
CGPoint point = [[touches anyObject] locationInView:self];
CGFloat starBtnW = kviewS.width / self.startCount;
self.count = (int)(point.x/starBtnW + 1);
for (int i = 0; i < self.count; i++)
{
UIButton *starBtn = (UIButton *)[self viewWithTag:100+i];
starBtn.selected = YES;
}
}
-(void)setUpCount:(NSInteger)count
{
if (count > self.startCount) {
NSLog(@"星級(jí)大于最大星級(jí)");
return ;
}
self.userInteractionEnabled = NO;//如設(shè)置了?初始星級(jí)就不能再改變了
self.count = count;//賦值初始星級(jí)
for (int i = 0; i < count; i++)
{
UIButton *starBtn = (UIButton *)[self viewWithTag:100+i];
starBtn.selected = YES;
}
}
@end
補(bǔ)充說(shuō)明
1.如想要初始化之后還可以點(diǎn)擊設(shè)置星級(jí)
需要-(void)setUpCount:(NSInteger)count方法中設(shè)置self.userInteractionEnabled = YES;
2.獲取當(dāng)前的星級(jí)
調(diào)用count屬性即為當(dāng)前的星級(jí)