美文网首页
iOS开发评分、打星

iOS开发评分、打星

作者: neo63 | 来源:发表于2017-04-11 14:29 被阅读1275次

    你好,欢迎浏览该文章。

    评分、打星功能在很多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
    恭喜你,看完了。

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

    相关文章

      网友评论

          本文标题:iOS开发评分、打星

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