这两天项目有个需求是用户评价分数(0-5)用星星多少来展示
自己自定义了一个UIView控件来显示,代码如下:
.h文件:
@class ZPMyStarShow;
/** * 星级评分条代理 */
@protocol ZPMyStarShowDelegate/**
* 评分改变
* @param ratingBar 评分控件
* @param newRating 评分值
*/
- (void)ratingBar:(ZPMyStarShow *)ratingBar ratingChanged:(float)newRating;
@end
以上是代理方法
@interface ZPMyStarShow : UIView
/** * 初始化设置未选中图片、半选中图片、全选中图片,以及评分值改变的代理(可以用 * Block)实现 *
* @param deselectedName 未选中图片名称
* @param halfSelectedName 半选中图片名称
* @param fullSelectedName 全选中图片名称
* @param delegate 代理 */
- (void)setImageDeselected:(NSString *)deselectedName halfSelected:(NSString *)halfSelectedName fullSelected:(NSString *)fullSelectedName andDelegate:(id)delegate;
/**
* 是否是指示器,如果是指示器,就不能滑动了,只显示结果,不是指示器的话就能滑动修改值
* 默认为NO
*/
@property (nonatomic,assign) BOOL isIndicator;
/**
* 当前应显示的星星数
* 默认设置是0
*/
@property (assign, nonatomic) CGFloat starRating;
.m文件:
@interface ZPMyStarShow (){
float _lastRating;
float height;
float width;
UIImage *unSelectedImage;
UIImage *halfSelectedImage;
UIImage *fullSelectedImage;
}
@property (nonatomic,strong) UIImageView *s1;
@property (nonatomic,strong) UIImageView *s2;
@property (nonatomic,strong) UIImageView *s3;
@property (nonatomic,strong) UIImageView *s4;
@property (nonatomic,strong) UIImageView *s5;
@property (nonatomic,weak) id delegate;
@end
@implementation ZPMyStarShow
/**
* 初始化设置未选中图片、半选中图片、全选中图片,以及评分值改变的代理(可以用 * Block)实现
* @param deselectedName 未选中图片名称
* @param halfSelectedName 半选中图片名称
* @param fullSelectedName 全选中图片名称 *
@param delegate 代理
*/
-(void)setImageDeselected:(NSString *)deselectedName halfSelected:(NSString *)halfSelectedName fullSelected:(NSString *)fullSelectedName andDelegate:(id)delegate{
self.delegate = delegate;
unSelectedImage = [UIImage imageNamed:deselectedName];
halfSelectedImage = halfSelectedName == nil ? unSelectedImage : [UIImage imageNamed:halfSelectedName];
fullSelectedImage = [UIImage imageNamed:fullSelectedName];
height = 0.0,width = 0.0;
if (height < [fullSelectedImage size].height) {
height = [fullSelectedImage size].height;
}
if (height < [halfSelectedImage size].height) {
height = [halfSelectedImage size].height;
}
if (height < [unSelectedImage size].height) {
height = [unSelectedImage size].height;
}
if (width < [fullSelectedImage size].width) {
width = [fullSelectedImage size].width;
}
if (width < [halfSelectedImage size].width) {
width = [halfSelectedImage size].width;
}
if (width < [unSelectedImage size].width) {
width = [unSelectedImage size].width;
}
_s1 = [[UIImageView alloc] initWithImage:unSelectedImage];
_s2 = [[UIImageView alloc] initWithImage:unSelectedImage];
_s3 = [[UIImageView alloc] initWithImage:unSelectedImage];
_s4 = [[UIImageView alloc] initWithImage:unSelectedImage];
_s5 = [[UIImageView alloc] initWithImage:unSelectedImage];
[_s1 setFrame:CGRectMake(0, 0, width, height)];
[_s2 setFrame:CGRectMake(width, 0, width, height)];
[_s3 setFrame:CGRectMake(2 * width, 0, width, height)];
[_s4 setFrame:CGRectMake(3 * width, 0, width, height)];
[_s5 setFrame:CGRectMake(4 * width, 0, width, height)];
[_s1 setUserInteractionEnabled:NO];
[_s2 setUserInteractionEnabled:NO];
[_s3 setUserInteractionEnabled:NO];
[_s4 setUserInteractionEnabled:NO];
[_s5 setUserInteractionEnabled:NO];
[self addSubview:_s1];
[self addSubview:_s2];
[self addSubview:_s3];
[self addSubview:_s4];
[self addSubview:_s5];
CGRect frame = [self frame];
frame.size.width = width * 5;
frame.size.height = height;
[self setFrame:frame];
_starRating = 0.0;
_lastRating = 0.0;
}
//外部传入评分值
-(void)setStarRating:(CGFloat)starRating{
_starRating = starRating;
_lastRating = starRating;
[self displayRating:starRating];
}
/**
* 设置评分值,显示相应的星星
* @param rating 评分值
*/
-(void)displayRating:(float)rating{
[_s1 setImage:unSelectedImage];
[_s2 setImage:unSelectedImage];
[_s3 setImage:unSelectedImage];
[_s4 setImage:unSelectedImage];
[_s5 setImage:unSelectedImage];
if (rating >= 0.5) {
[_s1 setImage:halfSelectedImage];
}
if (rating >= 1) {
[_s1 setImage:fullSelectedImage];
}
if (rating >= 1.5) {
[_s2 setImage:halfSelectedImage];
}
if (rating >= 2) {
[_s2 setImage:fullSelectedImage];
}
if (rating >= 2.5) {
[_s3 setImage:halfSelectedImage];
}
if (rating >= 3) {
[_s3 setImage:fullSelectedImage];
}
if (rating >= 3.5) {
[_s4 setImage:halfSelectedImage];
}
if (rating >= 4) {
[_s4 setImage:fullSelectedImage];
}
if (rating >= 4.5) {
[_s5 setImage:halfSelectedImage];
}
if (rating >= 5) {
[_s5 setImage:fullSelectedImage];
}
if ([self respondsToSelector:@selector(ratingBar:ratingChanged:)]) {
[_delegate ratingBar:self ratingChanged:rating];
}
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesBegan:touches withEvent:event];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesEnded:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
if (self.isIndicator) {
return;
}
CGPoint point = [[touches anyObject] locationInView:self];
int newRating = (int) (point.x / width) + 1;
if (newRating > 5)
return;
if (point.x < 0) {
newRating = 0;
}
if (newRating != _lastRating){
[self displayRating:newRating];
}
}
直接拷贝.h和.m文件就可以使用,- (void)setImageDeselected:(NSString *)deselectedName halfSelected:(NSString *)halfSelectedName fullSelected:(NSString *)fullSelectedName andDelegate:(id)delegate,在此方法里设置显示星星的图片和代理,然后设置isIndicator的值为YES,则只是显示器,不能滑动改变星星显示,默认值是NO,设置starRating为2.0,则显示两个点亮的星星,默认值是0.0,即不显示点亮的星星.