iOS 星星评分 点击 滑动 纯代码


#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

附上一张效果图
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值