星星评分定制

简介:
//
//  HYBCommentStarView.h
//  TQStarRatingView
//
//  Created by huangyibiao on 14-8-13.
//  Copyright (c) 2014年 TinyQ. All rights reserved.
//

#import <UIKit/UIKit.h>

@class HYBCommentStarView;

/*!
 * @brief 点击星星评分代理
 * @author huangyibiao
 */
@protocol HYBCommentStarViewDelegate <NSObject>

@optional
// 通过调用此方法来获取得分
- (void)commentStarView:(HYBCommentStarView *)view score:(float)score;

@end

/*!
 * @brief 评论星星视图
 * @author huangyibiao
 */
@interface HYBCommentStarView : UIView

// 获取或设置能否通过点击评分
@property (nonatomic, assign)   BOOL        canTouch;
// 总星级数
@property (nonatomic, readonly) NSUInteger  numberOfStar;
@property (nonatomic, weak)     id<HYBCommentStarViewDelegate> delegate;

- (id)initWithFrame:(CGRect)frame numberOfStar:(int)number;
/*!
 * @brief 设置得分,并更新星级,精确到0.1分
 * @param score 评分
 */
- (void)setScore:(float)score;

@end


//
//  HYBCommentStarView.m
//  CloudShopping
//
//  Created by huangyibiao on 14-8-4.
//  Copyright (c) 2014年 uni2uni. All rights reserved.
//

#import "HYBCommentStarView.h"

#define kBackgroundStarImageName @"evaluation_uncheck"
#define kForegroundStarImageName @"evaluation_ico"

@interface HYBCommentStarView ()

@property (nonatomic, strong) UIView *starBackgroundView;
@property (nonatomic, strong) UIView *starForegroundView;

@end


@implementation HYBCommentStarView

- (id)initWithFrame:(CGRect)frame {
    return [self initWithFrame:frame numberOfStar:5];
}

- (id)initWithFrame:(CGRect)frame numberOfStar:(int)number {
    frame.size.width = number * kImageWithName(kBackgroundStarImageName).size.width;
    self = [super initWithFrame:frame];
    if (self) {
        self.canTouch = NO;
        _numberOfStar = number;
        self.starBackgroundView = [self buidlStarViewWithImageName:kBackgroundStarImageName];
        self.starForegroundView = [self buidlStarViewWithImageName:kForegroundStarImageName];
        [self addSubview:self.starBackgroundView];
        [self addSubview:self.starForegroundView];
        [self setScore:0];
    }
    return self;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    if (!self.canTouch) {
        return;
    }
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    CGRect rect = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    if(CGRectContainsPoint(rect,point)) {
        [self changeStarForegroundViewWithPoint:point];
    }
    return;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if (!self.canTouch) {
        return;
    }
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    __weak HYBCommentStarView *weekSelf = self;
    
    [UIView transitionWithView:self.starForegroundView
                      duration:0.2
                       options:UIViewAnimationOptionCurveEaseInOut
                    animations:^ {
                        [weekSelf changeStarForegroundViewWithPoint:point];
                    }
                    completion:nil];
    return;
}

- (UIView *)buidlStarViewWithImageName:(NSString *)imageName {
    UIView *view = [[UIView alloc] initWithFrame:self.bounds];
    view.clipsToBounds = YES;
    
    for (int i = 0; i < self.numberOfStar; i ++) {
        CGRect fm = CGRectMake(i * self.width / self.numberOfStar,
                               0,
                               self.width / self.numberOfStar,
                               self.height);
        UIImageView *imageView = [HYBUIMaker imageViewWithFrame:fm imageName:imageName];
        [view addSubview:imageView];
    }
    return view;
}

- (void)changeStarForegroundViewWithPoint:(CGPoint)point {
    CGPoint p = point;
    
    if (p.x < 0) {
        p.x = 0;
    } else if (p.x > self.frame.size.width) {
        p.x = self.frame.size.width;

    }
    
    NSString *str = [NSString stringWithFormat:@"%0.2f", p.x / self.width];
    float score = [str floatValue];
    p.x = score * self.width;
    self.starForegroundView.frame = CGRectMake(0, 0, p.x, self.height);
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(commentStarView:score:)]) {
        [self.delegate commentStarView:self score:score];
    }
    return;
}

- (void)setScore:(float)score {
    CGPoint point = CGPointZero;
    point.x = self.width * score;
    NSLog(@"score: %f", score);
    self.starForegroundView.frame = CGRectMake(0, 0, point.x, self.height);
    NSLog(@"%@, %@", NSStringFromCGRect(self.starForegroundView.frame) , NSStringFromCGRect(self.bounds));
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(commentStarView:score:)]) {
        [self.delegate commentStarView:self score:score];
    }
    return;
}

@end


目录
相关文章
|
3月前
leetcode-6126:设计食物评分系统
leetcode-6126:设计食物评分系统
18 0
|
4月前
|
图形学
unity小游戏——得分高低的判定
unity小游戏——得分高低的判定
|
8月前
do while实现九宫格概率抽奖
do while实现九宫格概率抽奖
|
8月前
|
人工智能 算法 C语言
LeetCode.每日一题 1039. 多边形三角剖分的最低得分
这题是一道区间Dp问题,将一个多边形形划分为若干个三角形,求其最小的得分.
65 0
|
11月前
|
人工智能 算法 C++
每日算法系列【LeetCode 1039】多边形三角剖分的最低得分
每日算法系列【LeetCode 1039】多边形三角剖分的最低得分
|
Android开发
RatingBar(星级评分条)
RatingBar(星级评分条)也非常简单,相信在某宝, 买过东西的对这个应该不陌生,收到卖家的包裹,里面很多时候会有个小纸片,五星好评返还多少元这样, 而评分的时候就可以用到我们这个星级评分条了。
97 0
|
编解码
中秋节祝福程序源代码分享:土地分类数据阈值筛选和重投影分类
中秋节祝福程序源代码分享:土地分类数据阈值筛选和重投影分类
105 0
中秋节祝福程序源代码分享:土地分类数据阈值筛选和重投影分类
花朵分类(一)
本次教程的目的是带领大家学会基本的花朵图像分类 首先我们来介绍下数据集,该数据集有5种花,一共有3670张图片,分别是daisy、dandelion、roses、sunflowers、tulips,数据存放结构如下所示
220 0