iOS星级选择,可滑动的,可点击

iOS 星级选择,可以设置星星的个数,星级间距,可以设置分数。

一:.h文件定义

@interface FSStarView : UIView

/**
 *  设置控件分数
 *  score     分数
 */
@property(nonatomic,assign) CGFloat score;
/**
 *  Init FSStarView
 *
 *  @param frame  Rectangles
 *  @param numberOfStar 星星个数
 *  @param space 星星间距
 *
 *  @return FSStarViewObject
 */
- (instancetype)initWithFrame:(CGRect)frame NumberOfStar:(NSInteger)numberOfStar Space:(CGFloat)space;

/**
 * 选择的block回调
 */
@property(nonatomic,copy)  void(^CommentsStartViewBlock) (NSInteger);
@end

二:.m文件定义

#import "FSStarView.h"
#define kBACKGROUND_STAR @"xingxing-kong"
#define kFOREGROUND_STAR @"xingxing"

@interface FSStarView ()

@property(nonatomic,assign) CGFloat space;
@property (nonatomic, assign) NSInteger numberOfStar;
@property (nonatomic, strong) UIView *starBackgroundView;
@property (nonatomic, strong) UIView *starForegroundView;
@end

@implementation FSStarView
- (instancetype)initWithFrame:(CGRect)frame NumberOfStar:(NSInteger)numberOfStar Space:(CGFloat)space{
    self = [super initWithFrame:frame];
    if (self) {
        _numberOfStar = numberOfStar;
        _space = space;
        self.starBackgroundView = [self buidlStarViewWithImageName:kBACKGROUND_STAR];
        self.starForegroundView = [self buidlStarViewWithImageName:kFOREGROUND_STAR];
        [self addSubview:self.starBackgroundView];
        [self addSubview:self.starForegroundView];
        UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGR:)];
        [self addGestureRecognizer:tapGR];
        //移动手势
        UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGR:)];
        [self addGestureRecognizer:panGR];
    }
    return self;
}


#pragma mark - Buidl Star View

/**
 *  通过图片构建星星视图
 *
 *  @param imageName 图片名称
 *
 *  @return 星星视图
 */
- (UIView *)buidlStarViewWithImageName:(NSString *)imageName
{
    CGRect frame = self.bounds;
    UIView *view = [[UIView alloc] initWithFrame:frame];
    view.clipsToBounds = YES;
    for (int i = 0; i < self.numberOfStar; i ++){
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
        imageView.userInteractionEnabled = YES;
        imageView.frame = CGRectMake(i * (frame.size.height + _space), 0, frame.size.height, frame.size.height);
        [view addSubview:imageView];
    }
    return view;
}


#pragma mark - Set Score

/**
 *  设置控件分数
 *
 *  @param score     分数
 */
- (void)setScore:(CGFloat)score{
    CGFloat pointX = score * self.frame.size.height + ((NSInteger)score)*_space;
    self.starForegroundView.frame = CGRectMake(0, 0, pointX, self.frame.size.height);
}



- (void)tapGR:(UITapGestureRecognizer *)tapGR
{//点击手势对应的消息,如果有参数,参数就是手势本身
    CGPoint point = [tapGR locationInView:tapGR.view];
    NSLog(@"%f  %f",point.x,point.y);
    if (point.x > ((self.frame.size.height + _space)*_numberOfStar)) {
        point.x = (self.frame.size.height + _space)*_numberOfStar;
    }else{
        point.x = point.x;
    }

    NSInteger numInt = (point.x+_space)/(self.frame.size.height + _space);
    CGFloat numFloat = (point.x+_space)/(self.frame.size.height + _space);
    if (numFloat > numInt) {
        numInt++;
    }else{
        numInt = numInt;
    }
    self.starForegroundView.width = numInt*(self.frame.size.height + _space);
    self.CommentsStartViewBlock(numInt);
}

- (void)panGR:(UIPanGestureRecognizer *)panGR
{//点击手势对应的消息,如果有参数,参数就是手势本身
    CGPoint point = [panGR locationInView:panGR.view];
    NSLog(@"%f  %f",point.x,point.y);
    if ((point.x>=0)&&(point.x<=(5*(self.frame.size.height + _space)))) {
        NSInteger num = (point.x+_space)/(self.frame.size.height + _space);
        self.starForegroundView.width = num*(self.frame.size.height + _space);
        self.CommentsStartViewBlock(num);
    }

}

@end

三:控件的使用

    FSStarView *starView = [[FSStarView alloc] initWithFrame:FRAME(0, 0, 100, 15) NumberOfStar:5 Space:5];
    starView.score = 3.0;//分值
    [self.view addSubview:view];
    starView.CommentsStartViewBlock = ^(NSInteger num){
      NSLog(@"=======%ld", num);
    };

猜你喜欢

转载自blog.csdn.net/techalleyboy/article/details/70210225
今日推荐