美文网首页
一个星星评分的简单实现

一个星星评分的简单实现

作者: DDY | 来源:发表于2016-12-26 14:38 被阅读61次
    Star.png

    NAStarView.h

    #import <UIKit/UIKit.h>
    
    typedef void (^changeStar)(int starNum);
    
    @interface NAStarView : UIView
    
    @property (nonatomic, copy) changeStar selectedSatrNum;
    
    + (instancetype)starViewWithFrame:(CGRect)frame;
    
    @end
    

    NAStarView.m

    #import "NAStarView.h"
    #import "UIView+DDYExtension.h"
    
    @interface NAStarView ()
    
    @property (nonatomic, strong) NSMutableArray *allStar;
    
    @end
    
    @implementation NAStarView
    
    + (instancetype)starViewWithFrame:(CGRect)frame
    {
        return [[self alloc] initWithFrame:frame];
    }
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        if (self = [super initWithFrame:frame]) {
            [self layoutContentView];
        }
        return self;
    }
    
    - (void)layoutContentView
    {
        self.allStar = [[NSMutableArray alloc] init];
        
        for (int i = 0; i < 5; i++) {
            UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"StarNormal"]];
            imageView.frame = CGRectMake(((i+1)*25), 0, 25, 16);
            [self addSubview:imageView];
            imageView.contentMode = UIViewContentModeScaleAspectFit;
            [self.allStar addObject:imageView];
        }
    }
    
    #pragma mark 点击打分
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        CGPoint touchPoint = [[touches anyObject] locationInView:self];
        UIImageView *imgView;
        
        if ((touchPoint.x>0)&&(touchPoint.x<25*6)&&(touchPoint.y>0)&&(touchPoint.y <16))
        {
            for (int i=0; i<5; i++)
            {
                imgView = _allStar[i];
                if (imgView.ddy_x > touchPoint.x)
                {
                    imgView.image = [UIImage imageNamed:@"StarNormal"];
                }
                else
                {
                    imgView.image = [UIImage imageNamed:@"StarSelected"];
                }
            }
            if (self.selectedSatrNum) self.selectedSatrNum(((int)touchPoint.x)/25);
        }
    }
    

    使用

    NAStarView *starV = [NAStarView starViewWithFrame:CGRectMake(CGRectGetMaxX(lab.frame)-5, point.y+2, 25*6, 16)];
    [self.tableView.tableFooterView addSubview:starV];
    __weak __typeof__(self) weakSelf = self;
    starV.selectedSatrNum = ^(int starNum) {
      if (type == 1) weakSelf.conformityNum = starNum;
      else if (type == 2) weakSelf.logisticNum = starNum;
      else if (type == 3) weakSelf.serviceNum = starNum;
        };
    

    相关文章

      网友评论

          本文标题:一个星星评分的简单实现

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