星星评论

首先你要有两张星星图片

这里写图片描述

在ViewController中创建评论星星等级的滑杆

#import "ViewController.h"
#import "StarSliderView.h"

@interface ViewController ()<StarSLiderDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self  createStarSliderView];
}

#pragma mark - - 创建评论星星等级的滑杆
-(void)createStarSliderView{
    StarSliderView *star = [[StarSliderView alloc]initWithFrame:CGRectMake(self.view.frame.size.width/4, 400, self.view.frame.size.width/2, 100) andWithCurrentStarNum:1 andUserEnabled:YES andWithDistance:10];
    star.delegate = self;
    [self.view addSubview:star];
}

#pragma mark - - 代理方法
-(void)starSliderMoveWithCurrentNum:(int)starNum{
    NSLog(@"^^^^^^^^^%d^^^^^^^^^^^^^",starNum);
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

在StarSliderView中星星滑动动画

#import <UIKit/UIKit.h>
@protocol StarSLiderDelegate <NSObject>
@required
-(void)starSliderMoveWithCurrentNum:(int)starNum;

@end
@interface StarSliderView : UIView


/*currentStarNum:设置初始化当前星星等级。
   frame:大小。PSFrame的高度设置是无效的因为高度与一个星星的宽度相同
   nabled:是否可以滑动,NO一般用于已经评论的星级显示。
   distance :设置星星之间的距离*/
-(instancetype)initWithFrame:(CGRect)frame
       andWithCurrentStarNum:(int)currentStarNum
       andUserEnabled:(BOOL)nabled
       andWithDistance:(float)distance;

@property(nonatomic,assign)float distance;
@property(nonatomic,strong)id<StarSLiderDelegate>delegate;
@end
#import "StarSliderView.h"
@interface StarSliderView(){
    NSMutableArray *_imgViewArray;

}
@end

//设置星星个数
#define kStarsNum 5
//设置星星大小
#define kstarImgWitch (self.frame.size.width-_distance*(kStarsNum+1))/kStarsNum

@implementation StarSliderView

-(instancetype)initWithFrame:(CGRect)frame andWithCurrentStarNum:(int)currentStarNum andUserEnabled:(BOOL)nabled andWithDistance:(float)distance{
    self = [super initWithFrame:frame];

    if (self) {
        self.userInteractionEnabled = nabled;
        _distance =distance;
        [self createView];

        if (currentStarNum != 0 &&currentStarNum <= kStarsNum) {
            [self highlightStarShow:currentStarNum];
        }
    }
    return self;
}

#pragma mark - - 
-(void)createView{
    _imgViewArray = [NSMutableArray array];

    //默认状态
    for (int i = 0; i<kStarsNum; i++) {
        //
        UIImageView *imgViewNomal = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"btn_star_evaluation_normal"]];
        imgViewNomal.frame = CGRectMake(_distance+(kstarImgWitch+_distance)*i, 0, kstarImgWitch,  kstarImgWitch);
        imgViewNomal.tag = i;
        [self addSubview:imgViewNomal];

        //
        UIImageView *imgViewSeleted = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"btn_star_evaluation_press"]];
        imgViewSeleted.frame = CGRectMake(_distance+(kstarImgWitch+_distance)*i, 0, kstarImgWitch,  kstarImgWitch);
        imgViewSeleted.hidden = YES;
        imgViewSeleted.tag = i;
        [self addSubview:imgViewSeleted];
        [_imgViewArray addObject:imgViewSeleted];
    }
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, kstarImgWitch);
}

#pragma mark - -
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    CGPoint pt = [[touches anyObject] locationInView:self];
    [self showStarSlider:pt.x];
    [self selectAnimal:pt.x];
}

#pragma mark - -
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    CGPoint pt = [[touches anyObject] locationInView:self];
    [self showStarSlider:pt.x];
}

#pragma mark - -
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    CGPoint pt = [[touches anyObject] locationInView:self];
    [self showStarSlider:pt.x];
    [self selectAnimal:pt.x];
}

#pragma mark - - 展示滑杆
-(void)showStarSlider:(float)pt{
    int num;

    //0*
    if (pt<_distance) {
        for (int i = 0; i<_imgViewArray.count; i++) {
            UIImageView *imgView = _imgViewArray[i];
            imgView.hidden = YES;
        }
        num = 0;

    //5*
    }else if (pt>=(kstarImgWitch*kStarsNum+_distance*kStarsNum)){
        for (int i = 0; i<_imgViewArray.count; i++) {
            UIImageView *imgView = _imgViewArray[i];
            imgView.hidden = NO;
        }
        num = kStarsNum;

    //1—5*
    }else{
        int  currentInt = pt/(kstarImgWitch+_distance);
        for (int i =0; i<=currentInt; i++) {
            UIImageView *imgView = _imgViewArray[i];
            imgView.hidden = NO;
        }
        for (int i = currentInt+1; i<=_imgViewArray.count-1; i++) {
            UIImageView *imgView = _imgViewArray[i];
            imgView.hidden = YES;
        }
        num = currentInt +1;
    }

    //
    if ( [_delegate respondsToSelector:@selector(starSliderMoveWithCurrentNum:)]) {
        [_delegate starSliderMoveWithCurrentNum:num];
    }

}

#pragma mark - - 简单动画效果
-(void)selectAnimal:(float)pt{
    UIImageView *imgViewanimal = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"btn_star_evaluation_press"]];
    imgViewanimal.frame = CGRectMake(pt,0, 19, 19);
    imgViewanimal.alpha = 0.3;
    [self addSubview:imgViewanimal];

    [UIView animateWithDuration:0.5 animations:^{
        imgViewanimal.frame = CGRectMake(pt,-30, 19, 19);
        imgViewanimal.alpha = 0;
    }];
    [self performSelector:@selector(delayMethod:) withObject:imgViewanimal afterDelay:0.5];
}

//个人习惯喜欢延时操作也可以使用动画执行之后的操作
#pragma mark - - 动画延时
-(void)delayMethod:(UIView*)view{
    [view removeFromSuperview];
    view = nil;
}

#pragma mark - -
-(void)highlightStarShow:(int)currentInt{
    for (int i =0; i<=currentInt-1; i++) {
        UIImageView *imgView = _imgViewArray[i];
        imgView.hidden = NO;
    }
    for (int i = currentInt; i<=_imgViewArray.count-1; i++) {
        UIImageView *imgView = _imgViewArray[i];
        imgView.hidden = YES;
    }
}
@end
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值