大体思路:
1.创建5个imageview,每个加上灰色星星图片(代表未选中)
2.1.手指滑动:手指在VC滑动时获取手指位置,判断手指位置在第几个星星处,获取星星个数,把手指前边的图片赋上黄色星星图片(代表选中);
2.2.点击结束时判断点击位置,前边所有图片赋值黄色星星,获取星星个数。
在展示评价页面同理,根据评价的分数给展示星星的imageview重新设置图片。
2017年2月18日更新
更新内容:简化代码、分离出view,可直接使用。
Coding上Demo地址
EvaluateView中主要代码
#import "EvaluateView.h"
@implementation EvaluateView
- (instancetype)initWithFrame:(CGRect)frame count:(int)count
{
self = [super initWithFrame:frame];
if (self)
{
[self createStarsWithCounts:count];
}
return self;
}
/**
创建星星的imageview
*/
- (void)createStarsWithCounts:(int)counts
{
//计算左右间距
CGFloat left = 20;
CGFloat middle = 10;
CGFloat width = (self.frame.size.width-2*left-middle*(counts-1))/counts;
self.imgWidth = width;
for (int i = 0; i < counts; i ++)
{
UIImageView *imgView = [[UIImageView alloc] initWithImage:UnSelectedImg];
imgView.tag = i;
[imgView setFrame:CGRectMake(left+i*width, left, width, width)];
[self addSubview:imgView];
}
}
/**
手指滑动时判选中第几个星星
*/
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
CGPoint point = [[touches anyObject] locationInView:self];
int x = point.x;
int y = point.y;
if (y >= 20 && y <= self.imgWidth + 20) //Y在星星同一行
{
if (x > 20 && x < self.frame.size.width - 20) //在星星范围内
{
self.count = (x - 20) / (self.imgWidth + 10) + 1;
[self refreshUI];
}
}
}
/**
点击结束时判断选择的星星位置
*/
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
CGPoint point = [[touches anyObject] locationInView:self];
int x = point.x;
int y = point.y;
if (y >= 20 && y <= self.imgWidth + 20) //Y在星星同一行
{
if (x > 20 && x < self.frame.size.width - 20) //在星星范围内
{
self.count = (x - 20) / (self.imgWidth + 10) + 1;
[self refreshUI];
}
}
}
/**
根据选中星星数重新展示图片
*/
- (void)refreshUI
{
//回调选中星星结果给VC
if ([self.delegate respondsToSelector:@selector(selectedStras:)])
{
[self.delegate selectedStras:self.count];
}
for (UIImageView *imgView in self.subviews)
{
if (imgView.tag < self.count)
{
[imgView setImage:SelectedImg];
}
else
{
[imgView setImage:UnSelectedImg];
}
}
}
@end
以下是老代码,方便理解,但是质量不是很好。。。
1)滑动选择星星触发方法
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
CGPoint point = [[touches anyObject] locationInView:self.view];
int x = point.x;
int y = point.y;
if (y >= 206 && y <= 246) //第一个星星的坐标范围
{
if (x >= MWIDTH/2-120 && x <= MWIDTH/2 - 80)
{
[self.imageView1 setImage:self.selectedImage];
[self.imageView2 setImage:self.unSelectedImage];
[self.imageView3 setImage:self.unSelectedImage];
[self.imageView4 setImage:self.unSelectedImage];
[self.imageView5 setImage:self.unSelectedImage];
self.ranking = @"1";
}
else if (x > MWIDTH/2 - 70 && x <= MWIDTH/2 - 30)
{
[self.imageView1 setImage:self.selectedImage];
[self.imageView2 setImage:self.selectedImage];
[self.imageView3 setImage:self.unSelectedImage];
[self.imageView4 setImage:self.unSelectedImage];
[self.imageView5 setImage:self.unSelectedImage];
self.ranking = @"2";
}
else if (x > MWIDTH/2 - 20 && x <= MWIDTH/2 + 20)
{
[self.imageView1 setImage:self.selectedImage];
[self.imageView2 setImage:self.selectedImage];
[self.imageView3 setImage:self.selectedImage];
[self.imageView4 setImage:self.unSelectedImage];
[self.imageView5 setImage:self.unSelectedImage];
self.ranking = @"3";
}
else if (x > MWIDTH/2 + 30 && x <= MWIDTH/2 + 70)
{
[self.imageView1 setImage:self.selectedImage];
[self.imageView2 setImage:self.selectedImage];
[self.imageView3 setImage:self.selectedImage];
[self.imageView4 setImage:self.selectedImage];
[self.imageView5 setImage:self.unSelectedImage];
self.ranking = @"4";
}
else if (x > MWIDTH/2 + 80 && x <= MWIDTH/2 + 120)
{
[self.imageView1 setImage:self.selectedImage];
[self.imageView2 setImage:self.selectedImage];
[self.imageView3 setImage:self.selectedImage];
[self.imageView4 setImage:self.selectedImage];
[self.imageView5 setImage:self.selectedImage];
self.ranking = @"5";
}
}
}
2)直接点击星星触发方法
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
CGPoint point = [[touches anyObject] locationInView:self.view];
int x = point.x;
int y = point.y;
if (y >= 206 && y <= 246) //第一个星星的坐标范围
{
if (x >= MWIDTH/2-120 && x <= MWIDTH/2 - 80)
{
[self.imageView1 setImage:self.selectedImage];
[self.imageView2 setImage:self.unSelectedImage];
[self.imageView3 setImage:self.unSelectedImage];
[self.imageView4 setImage:self.unSelectedImage];
[self.imageView5 setImage:self.unSelectedImage];
self.ranking = @"1";
}
else if (x > MWIDTH/2 - 70 && x <= MWIDTH/2 - 30)
{
[self.imageView1 setImage:self.selectedImage];
[self.imageView2 setImage:self.selectedImage];
[self.imageView3 setImage:self.unSelectedImage];
[self.imageView4 setImage:self.unSelectedImage];
[self.imageView5 setImage:self.unSelectedImage];
self.ranking = @"2";
}
else if (x > MWIDTH/2 - 20 && x <= MWIDTH/2 + 20)
{
[self.imageView1 setImage:self.selectedImage];
[self.imageView2 setImage:self.selectedImage];
[self.imageView3 setImage:self.selectedImage];
[self.imageView4 setImage:self.unSelectedImage];
[self.imageView5 setImage:self.unSelectedImage];
self.ranking = @"3";
}
else if (x > MWIDTH/2 + 30 && x <= MWIDTH/2 + 70)
{
[self.imageView1 setImage:self.selectedImage];
[self.imageView2 setImage:self.selectedImage];
[self.imageView3 setImage:self.selectedImage];
[self.imageView4 setImage:self.selectedImage];
[self.imageView5 setImage:self.unSelectedImage];
self.ranking = @"4";
}
else if (x > MWIDTH/2 + 80 && x <= MWIDTH/2 + 120)
{
[self.imageView1 setImage:self.selectedImage];
[self.imageView2 setImage:self.selectedImage];
[self.imageView3 setImage:self.selectedImage];
[self.imageView4 setImage:self.selectedImage];
[self.imageView5 setImage:self.selectedImage];
self.ranking = @"5";
}
}
}