RatingBar.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // RatingBar.m
  3. // MyRatingBar
  4. //
  5. // Created by Leaf on 14-8-28.
  6. // Copyright (c) 2014年 Leaf. All rights reserved.
  7. //
  8. // 版权属于原作者
  9. // http://code4app.com (cn) http://code4app.net (en)
  10. // 发布代码于最专业的源码分享网站: Code4App.com
  11. #import "RatingBar.h"
  12. #define ZOOM 0.8f
  13. @interface RatingBar()
  14. @property (nonatomic,strong) UIView *bottomView;
  15. @property (nonatomic,strong) UIView *topView;
  16. @property (nonatomic,assign) CGFloat starWidth;
  17. @end
  18. @implementation RatingBar
  19. - (id)initWithFrame:(CGRect)frame
  20. {
  21. self = [super initWithFrame:frame];
  22. if (self) {
  23. // Initialization code
  24. self.backgroundColor = [UIColor whiteColor];
  25. self.bottomView = [[UIView alloc] initWithFrame:self.bounds];
  26. self.topView = [[UIView alloc] initWithFrame:CGRectZero];
  27. [self addSubview:self.bottomView];
  28. [self addSubview:self.topView];
  29. self.topView.clipsToBounds = YES;
  30. self.topView.userInteractionEnabled = NO;
  31. self.bottomView.userInteractionEnabled = NO;
  32. self.userInteractionEnabled = YES;
  33. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
  34. UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
  35. [self addGestureRecognizer:tap];
  36. [self addGestureRecognizer:pan];
  37. //
  38. CGFloat width = frame.size.width/7.0;
  39. self.starWidth = width;
  40. for(int i = 0;i<5;i++){
  41. UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, width*ZOOM, width*ZOOM)];
  42. img.center = CGPointMake((i+1.5)*width, frame.size.height/2);
  43. img.image = [UIImage imageNamed:@"bt_star_a"];
  44. [self.bottomView addSubview:img];
  45. UIImageView *img2 = [[UIImageView alloc] initWithFrame:img.frame];
  46. img2.center = img.center;
  47. img2.image = [UIImage imageNamed:@"bt_star_b"];
  48. [self.topView addSubview:img2];
  49. }
  50. self.enable = YES;
  51. }
  52. return self;
  53. }
  54. -(void)setViewColor:(UIColor *)backgroundColor{
  55. if(_viewColor!=backgroundColor){
  56. self.backgroundColor = backgroundColor;
  57. self.topView.backgroundColor = backgroundColor;
  58. self.bottomView.backgroundColor = backgroundColor;
  59. }
  60. }
  61. -(void)setStarNumber:(NSInteger)starNumber{
  62. if(_starNumber!=starNumber){
  63. _starNumber = starNumber;
  64. self.topView.frame = CGRectMake(0, 0, self.starWidth*(starNumber+1), self.bounds.size.height);
  65. }
  66. }
  67. -(void)tap:(UITapGestureRecognizer *)gesture{
  68. if(self.enable){
  69. CGPoint point = [gesture locationInView:self];
  70. NSInteger count = (int)(point.x/self.starWidth)+1;
  71. self.topView.frame = CGRectMake(0, 0, self.starWidth*count, self.bounds.size.height);
  72. if(count>5){
  73. _starNumber = 5;
  74. }else{
  75. _starNumber = count-1;
  76. }
  77. }
  78. }
  79. -(void)pan:(UIPanGestureRecognizer *)gesture{
  80. if(self.enable){
  81. CGPoint point = [gesture locationInView:self];
  82. NSInteger count = (int)(point.x/self.starWidth);
  83. if(count>=0 && count<=5 && _starNumber!=count){
  84. self.topView.frame = CGRectMake(0, 0, self.starWidth*(count+1), self.bounds.size.height);
  85. _starNumber = count;
  86. }
  87. }
  88. }
  89. /*
  90. // Only override drawRect: if you perform custom drawing.
  91. // An empty implementation adversely affects performance during animation.
  92. - (void)drawRect:(CGRect)rect
  93. {
  94. // Drawing code
  95. }
  96. */
  97. @end