美文网首页
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开发评分、打星

    你好,欢迎浏览该文章。 评分、打星功能在很多APP中都会用到,因此自己封装了一个类实现该功能。其中分数可以为整数和...

  • iOS评分打星控件封装

    现今,五星打分已成为目前主流的评分方式。在各个app上也十分常见。接下来是对评分控件的详解。 思路:首先我们想...

  • iOS开发中打开本地应用、打开appStore应用、给app评分

    iOS开发中打开本地应用、打开appStore应用、给app评分功能实现 app开发中,通常会有邀请用户给app打...

  • APP评分打星

    App Store评分方式 1、通用方式通过App内部打开网页形式,跳转到AppStore编辑评论,可评分,可评论...

  • iOS 应用内评分

    iOS 应用内评分 iOS10.3允许开发者敦促用户在 App Store 上对应用进行评分。整个评分过程直接在 ...

  • iOS评分功能、APP中打开其他应用程序

    1、评分功能 iOS中评分支持功能开发非常简单。 NSString*str=[NSStringstringWith...

  • iOS开发星级评分

    前言 在开发电商类的app中,我们经常会遇到用户评价“打星”这样的需求,因为iOS上没有这个控件,所以这时需要我们...

  • Android实现商城评分系统功能

    概述 商城项目开发过程中,当用户购买商品完成后,需要用户对买入的商品进行打星评分,这在开发过程中似乎这个需求是必须...

  • Android实现商城评分系统功能

    概述 商城项目开发过程中,当用户购买商品完成后,需要用户对买入的商品进行打星评分,这在开发过程中似乎这个需求是必须...

  • JAVA--Day1

    iOS出身, 一直在开发iOS, 近期公司不做app开发了, 再做VR,游戏之类的开发, 公司缺后台, 所以自己打...

网友评论

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

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