123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- //
- // RatingBar.m
- // MyRatingBar
- //
- // Created by Leaf on 14-8-28.
- // Copyright (c) 2014年 Leaf. All rights reserved.
- //
- // 版权属于原作者
- // http://code4app.com (cn) http://code4app.net (en)
- // 发布代码于最专业的源码分享网站: Code4App.com
- #import "RatingBar.h"
- #define ZOOM 0.8f
- @interface RatingBar()
- @property (nonatomic,strong) UIView *bottomView;
- @property (nonatomic,strong) UIView *topView;
- @property (nonatomic,assign) CGFloat starWidth;
- @end
- @implementation RatingBar
- - (id)initWithFrame:(CGRect)frame Flag:(BOOL)flag
- {
- self = [super initWithFrame:frame];
- if (self) {
- // Initialization code
- self.backgroundColor = [UIColor whiteColor];
- self.bottomView = [[UIView alloc] initWithFrame:self.bounds];
- self.topView = [[UIView alloc] initWithFrame:CGRectZero];
-
- // self.topView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
- // self.bottomView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
-
-
- [self addSubview:self.bottomView];
- [self addSubview:self.topView];
-
- self.topView.clipsToBounds = YES;
- self.topView.userInteractionEnabled = NO;
- self.bottomView.userInteractionEnabled = NO;
- self.userInteractionEnabled = YES;
-
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
- UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
- [self addGestureRecognizer:tap];
- [self addGestureRecognizer:pan];
-
- //
- if (flag) {
- CGFloat width = frame.size.width/5.0;
- self.starWidth = width;
- for(int i = 0;i<5;i++){
- UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(i*width, 0, width*ZOOM, width*ZOOM)];
- img.image = [UIImage imageNamed:@"bt_star_a"];
- [self.bottomView addSubview:img];
- UIImageView *img2 = [[UIImageView alloc] initWithFrame:img.frame];
- img2.center = img.center;
- img2.image = [UIImage imageNamed:@"bt_star_b"];
- [self.topView addSubview:img2];
- }
- }else{
- CGFloat width = frame.size.width/7.0;
- self.starWidth = width;
- for(int i = 0;i<5;i++){
- UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, width*ZOOM, width*ZOOM)];
- img.center = CGPointMake((i+1.5)*width, frame.size.height/2);
- img.image = [UIImage imageNamed:@"bt_star_a"];
- [self.bottomView addSubview:img];
- UIImageView *img2 = [[UIImageView alloc] initWithFrame:img.frame];
- img2.center = img.center;
- img2.image = [UIImage imageNamed:@"bt_star_b"];
- [self.topView addSubview:img2];
- }
- }
-
- self.enable = YES;
-
- }
- return self;
- }
- -(void)setViewColor:(UIColor *)backgroundColor{
- if(_viewColor!=backgroundColor){
- self.backgroundColor = backgroundColor;
- self.topView.backgroundColor = backgroundColor;
- self.bottomView.backgroundColor = backgroundColor;
- }
- }
- -(void)setStarNumber:(NSInteger)starNumber{
- if(_starNumber!=starNumber){
- _starNumber = starNumber;
- self.topView.frame = CGRectMake(0, 0, self.starWidth*(starNumber+1), self.bounds.size.height);
-
- if (starNumBlock) {
- starNumBlock([NSString stringWithFormat:@"%d",(int)starNumber]);
- }
- }
- }
- -(void)tap:(UITapGestureRecognizer *)gesture{
- if(self.enable){
- CGPoint point = [gesture locationInView:self];
- NSInteger count = (int)(point.x/self.starWidth)+1;
- self.topView.frame = CGRectMake(0, 0, self.starWidth*count, self.bounds.size.height);
- if(count>5){
- _starNumber = 5;
- }else{
- _starNumber = count-1;
- }
-
- if (starNumBlock) {
- starNumBlock([NSString stringWithFormat:@"%d",(int)_starNumber]);
- }
- }
- }
- -(void)pan:(UIPanGestureRecognizer *)gesture{
- if(self.enable){
- CGPoint point = [gesture locationInView:self];
- NSInteger count = (int)(point.x/self.starWidth);
- if(count>=0 && count<=5 && _starNumber!=count){
- self.topView.frame = CGRectMake(0, 0, self.starWidth*(count+1), self.bounds.size.height);
- _starNumber = count;
-
- if (starNumBlock) {
- starNumBlock([NSString stringWithFormat:@"%d",(int)_starNumber]);
- }
- }
- }
- }
- - (void)changeStarNumberBlock:(MyBlockType)starBlock
- {
- starNumBlock = starBlock;
- }
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect
- {
- // Drawing code
- }
- */
- @end
|