iOS开发评分、打星

你好,欢迎浏览该文章。

评分、打星功能在很多APP中都会用到,因此自己封装了一个类实现该功能。其中分数可以为整数和小数。希望对你有所帮助。效果如下:

INTEGER_TYPE类型效果
FLOAT_TYPE类型效果

具体实现:

1.首先添加一个协议,协议中申明一个optional方法,该方法监听评分改变事件。因为有的地方只显示星星对应的分数,因此该方法为optional。

@protocol ratingViewDelegate <NSObject>
@optional
- (void)ratingView:(LHRatingView *)view score:(CGFloat)score;
@end

2.定义一个枚举,枚举里面有两种类型,INTEGER_TYPE表示评分为整数,FLOAT_TYPE表示评分为小数。

typedef NS_ENUM(NSUInteger, RatingType) {
    INTEGER_TYPE,
    FLOAT_TYPE
};

3.实现类,定义一个LHRatingView类继承自UIView,再在类上添加两个view,一个grayStarView,一个foreStarView。grayStarView上添加空心的星星,foreStarView上添加实心的星星。再在LHRatingView上添加UITapGestureRecognizer和UIPanGestureRecognizer事件,用户点击或拖动时,改变foreStarView的宽度,即可实现该功能。

    for (int i = 0; i < starNumber; i++) {
        UIImage * grayImg = [UIImage imageNamed:@"image.bundle/starGray"];
        UIImageView * grayImgView = [[UIImageView alloc]initWithFrame:CGRectMake((eachWidth+self.widDistance)*i, self.heiDistance, eachWidth, height)];
        grayImgView.image = grayImg;
        [self.grayStarView addSubview:grayImgView];

        UIImage * foreImg = [UIImage imageNamed:@"image.bundle/starFore"];
        UIImageView * foreImgView = [[UIImageView alloc]initWithFrame:CGRectMake((eachWidth+self.widDistance)*i, self.heiDistance, eachWidth, height)];
        foreImgView.image = foreImg;
        [self.foreStarView addSubview:foreImgView];
    }
        
    UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureEvent:)];
    UIPanGestureRecognizer * panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureEvent:)];
        
    [self addGestureRecognizer:tapGesture];
    [self addGestureRecognizer:panGesture];

注意:foreStarView的clipsToBounds属性需设置成YES。
self.foreStarView.clipsToBounds = YES;

拖动处理及事件监听:

#pragma mark - 拖动
- (void)panGestureEvent:(UIPanGestureRecognizer *)pan_
{
    
    CGPoint point = [pan_ locationInView:self];

    if (point.x < 0) {
        return;
    }
    if(_ratingType == INTEGER_TYPE){
        NSInteger count = (NSInteger)(point.x/(eachWidth+self.widDistance))+1;
        point.x = count*(eachWidth+self.widDistance);
    }
    
    [self changeStarForeViewWithPoint:point];
}

#pragma mark - 设置显示的星星
- (void)changeStarForeViewWithPoint:(CGPoint)point
{
    CGPoint p = point;
    
    if (p.x < 0) {
        return;
    }
    
    if (p.x < self.lowestScore/maxScore*CGRectGetWidth(self.frame))
    {
        p.x = self.lowestScore/maxScore*CGRectGetWidth(self.frame);
    }
    else if (p.x > self.frame.size.width)
    {
        p.x = self.frame.size.width;
    }
    
    float sc = p.x/CGRectGetWidth(self.frame);
    
    CGRect fRect = self.foreStarView.frame;
    fRect.size.width = p.x;
    self.foreStarView.frame = fRect;
    
    _score = sc*maxScore;
    
    if(_ratingType == INTEGER_TYPE){

        _score = (int)_score;
    }
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(ratingView:score:)])
    {
        [self.delegate ratingView:self score:self.score];
    }
}

GitHub Address:https://github.com/yutiandesan/PlayStar
恭喜你,看完了。

第一次在这儿发表文章,若有不合理的地方,欢迎指正。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • importUIKit classViewController:UITabBarController{ enumD...
    明哥_Young阅读 9,506评论 1 10
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 177,252评论 25 709
  • 简单如他,他的世界里只有数学和自己所在意的情谊;简单如他,除却他的在意其他都是如齿轮般重复,未曾让他有任何起伏;简...
    凌言落雪阅读 1,765评论 0 0
  • 我和A认识好几年了,他小小的个子,笑起来却异常温暖,他喜欢读书,而我又偏偏文字可以写上那么几段,所以我们在...
    风痕times阅读 1,328评论 0 0

友情链接更多精彩内容