美文网首页iOS 开发
简单实现点触/输入值给五颗星评价

简单实现点触/输入值给五颗星评价

作者: 嗜糖63 | 来源:发表于2016-03-24 18:24 被阅读89次

先上效果图

gif.gif

1.码UI。。。

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 30)];
    label.text = @"点击星星可以自动获取评分哦~";
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label];
    label.center = CGPointMake(kScreenWidth / 2, 60);
    
    self.colorStars = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"StarsForeground"]];
    self.blankStars = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"StarsBackground"]];
    // 宽高根据Image实际大小,如果放到了2倍图记得➗2
    self.colorStars.frame = CGRectMake((kScreenWidth - 65) / 2, 100, 65, 23);
    self.blankStars.frame = CGRectMake((kScreenWidth - 65) / 2, 100, 65, 23);
    // 顺序不能错
    [self.view addSubview:self.blankStars];
    [self.view addSubview:self.colorStars];
    // 关键
    self.colorStars.clipsToBounds = YES;
    self.colorStars.contentMode = UIViewContentModeLeft | UIViewContentModeTop;
    
    self.tf = [[UITextField alloc] initWithFrame:CGRectMake((kScreenWidth - 300) / 2, 250, 300, 30)];
    self.tf.borderStyle = UITextBorderStyleRoundedRect;
    self.tf.backgroundColor = [UIColor lightGrayColor];
    self.tf.textColor = [UIColor blueColor];
    self.tf.backgroundColor = [UIColor whiteColor];
    self.tf.placeholder = @"请输入1.0-5.0的评分等级";
    self.tf.keyboardType = UIKeyboardTypeDecimalPad;
    self.tf.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.tf];
    
    UIButton *Stars = [UIButton buttonWithType:UIButtonTypeCustom];
    [Stars setTitle:@"评星" forState:UIControlStateNormal];
    [Stars setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    Stars.frame = CGRectMake((kScreenWidth - 50) / 2, 350, 50, 30);
    [Stars addTarget:self action:@selector(starsBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:Stars];

2.评星按钮的回调事件

- (void)starsBtnClick {
    if ([self.tf.text floatValue] <= 0) {
        NSLog(@"请输入评分");
        [self alertMSG:@"请输入评分"];
    } else if ([self.tf.text floatValue] > 5) {
        NSLog(@"请按规则输入评分");
        [self alertMSG:@"请按规则输入评分"];
    } else {
        CGFloat value = self.tf.text.floatValue;
        CGFloat width = value/5.0*65;
        CGPoint orgin = self.colorStars.frame.origin;
        CGFloat height = self.colorStars.frame.size.height;
        [UIView animateWithDuration:0.23 animations:^{
            self.colorStars.frame = CGRectMake(orgin.x, orgin.y, width, height);
        }];
    }
}

3.点触评星的实现

// 点触评分的关键
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    
    UITouch *touche = [touches anyObject];
    CGPoint touchPoint = [touche locationInView:self.blankStars];
    NSLog(@"%@", NSStringFromCGPoint(touchPoint));
    // 如果点触在图片外,就不进行操作
    if (touchPoint.x >= 0 && touchPoint.y >= 0 && touchPoint.x < self.blankStars.frame.size.width + 10 && touchPoint.y <= self.blankStars.frame.size.height) {
        CGPoint orgin = self.colorStars.frame.origin;
        CGFloat height = self.colorStars.frame.size.height;
        CGFloat touchX = touchPoint.x;
        if (touchPoint.x > self.blankStars.frame.size.width) {
            touchX = self.blankStars.frame.size.width;
        }
        [UIView animateWithDuration:0.23 animations:^{
            self.colorStars.frame = CGRectMake(orgin.x, orgin.y, touchX, height);
        } completion:^(BOOL finished) {
            self.tf.text = [NSString stringWithFormat:@"%.2lf", touchX / self.blankStars.frame.size.width * 5];
        }];
    }
    
    [self.view endEditing:YES];
}

Demo地址:https://github.com/ChengLuffy/StarsForEvaluation.git

相关文章

  • 简单实现点触/输入值给五颗星评价

    先上效果图 1.码UI。。。 2.评星按钮的回调事件 3.点触评星的实现 Demo地址:https://githu...

  • 一件让我感动的小事

    在抖音一家小店买了一套书,事后抖音购物助手要求评价。我给快递点赞了五颗星,对店铺评价时,心一懒,点赞了三颗星,评价...

  • 2015 Baidu IFE Task 0002

    终于用上了markdown,COOL 任务说明 实现一个小需求,在输入框输入数字,数字没有进行数字判断,值做简单的...

  • vue.js学习(二)

    1、简单的todoList()实现输入框输入值显示在下方,每添加一次,清空输入框。 2、组件组件:页面上的某一部分...

  • 手指触控点

    触控点id和索引区别 getActionIndex方法,获取触控点索引,索引值和触控手指个数相关,从0开始。触控点...

  • 第一至三章

    本章比较简单,只挑一些典型题目的java实现 输入一个正整数,反转其各位的值,然后输出,比如输入98765,输出5...

  • 224. 基本计算器

    实现一个基本的计算器来计算一个简单的字符串表达式 s 的值。输入:s = "1 + 1"输出:2输入:s = "(...

  • Android MultiTouch 多点触控

    演示效果 demo实现第一、二个触控点的坐标记录及绘制展示e47k6-a128q.gif 多点触控操作判断的值 关键代码

  • input可选列表输入框 Bootstrap-Typeahead

    要实现是效果 input输入框 点击输入框时出现下列列表可以选值,也可以直接输入值,自动筛选匹配到列表后在输入框下...

  • 手机端 input 输入框校验小数点后两位

    问题:今天做输入框校验金额,小数点后只能输入两位,借鉴了网上大神之后,发现实现不了,value值始终无法赋值给 p...

网友评论

    本文标题: 简单实现点触/输入值给五颗星评价

    本文链接:https://www.haomeiwen.com/subject/lxmrlttx.html