#import "ViewController.h"
@interface ViewController ()
/*
* deselectedName.png 未选择时显示空心图片
* halfSelectedName.png 选择奇数分时显示半实心图片
* fullSelectedName.png 选择后显示实心图片
*/
// 显示星星视图
@property (nonatomic, strong)UIView * myview;
// 显示图片数组
@property (nonatomic, strong)NSMutableArray * myImageArray;
// 显示分数
@property (nonatomic, strong)UILabel * score;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self addSubViews];
}
// 初始化界面
- (void)addSubViews
{
_myview = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 180, 24)];
self.myImageArray = [[NSMutableArray alloc] init];
UIImageView *imageView;
for (int i = 0; i < 5; i++) {
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"deselectedName.png"]];
imageView.frame = CGRectMake(_myview.bounds.origin.x+((i+1)*24), _myview.bounds.origin.y, 24, 24);
[_myview addSubview:imageView];
[_myImageArray addObject:imageView];
}
self.score = [[UILabel alloc] initWithFrame:(CGRectMake(6 * 24 + 5, 0, 40, 24))];
_score.textColor = [UIColor redColor];
[_myview addSubview:_score];
[self.view addSubview:_myview];
}
#pragma mark -- 点击的坐标
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:_myview];
UIImageView *im ;
for(int i = 0;i < 5 ; i++){
im = _myImageArray[i];
NSLog(@"_myImageArray[%i] = (%f,%f)",i,im.frame.origin.x,im.frame.origin.y);
if ((touchPoint.x > 0)&&(touchPoint.x < 144)&&(touchPoint.y > 0)&&(touchPoint.y < 24)) {
NSString *myscore = [NSString stringWithFormat:@"%i分",((int)touchPoint.x/12 - 1)];
_score.text = myscore;//_score是一个UILable,myscore为分数,显示在给用户看,关于这个不在赘述
if (im.frame.origin.x > touchPoint.x) {
im.image =[UIImage imageNamed:@"deselectedName.png"];
}else if (im.frame.origin.x <= touchPoint.x && im.frame.origin.x + 12 >= touchPoint.x){
im.image =[UIImage imageNamed:@"halfSelectedName.png"];
}else{
im.image =[UIImage imageNamed:@"fullSelectedName.png"];
}
}
}
}
#pragma mark - 滑动的坐标
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:_myview];
UIImageView *im ;
for(int i = 0;i < 5 ; i++){
im = _myImageArray[i];
NSLog(@"_myImageArray[%i] = (%f,%f)",i,im.frame.origin.x,im.frame.origin.y);
if ((touchPoint.x > 0)&&(touchPoint.x < 144)&&(touchPoint.y > 0)&&(touchPoint.y < 24)) {
NSString *myscore = [NSString stringWithFormat:@"%i分",((int)touchPoint.x/12 - 1)];
_score.text = myscore;//_score是一个UILable,myscore为分数,显示在给用户看,关于这个不在赘述
if (im.frame.origin.x > touchPoint.x) {
im.image =[UIImage imageNamed:@"deselectedName.png"];
}else if (im.frame.origin.x <= touchPoint.x && im.frame.origin.x + 12 >= touchPoint.x){
im.image =[UIImage imageNamed:@"halfSelectedName.png"];
}else{
im.image =[UIImage imageNamed:@"fullSelectedName.png"];
}
}
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
附上一张效果图