RatingBar.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 Flag:(BOOL)flag
  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.topView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  28. // self.bottomView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  29. [self addSubview:self.bottomView];
  30. [self addSubview:self.topView];
  31. self.topView.clipsToBounds = YES;
  32. self.topView.userInteractionEnabled = NO;
  33. self.bottomView.userInteractionEnabled = NO;
  34. self.userInteractionEnabled = YES;
  35. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
  36. UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
  37. [self addGestureRecognizer:tap];
  38. [self addGestureRecognizer:pan];
  39. //
  40. if (flag) {
  41. CGFloat width = frame.size.width/5.0;
  42. self.starWidth = width;
  43. for(int i = 0;i<5;i++){
  44. UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(i*width, 0, width*ZOOM, width*ZOOM)];
  45. img.image = [UIImage imageNamed:@"bt_star_a"];
  46. [self.bottomView addSubview:img];
  47. UIImageView *img2 = [[UIImageView alloc] initWithFrame:img.frame];
  48. img2.center = img.center;
  49. img2.image = [UIImage imageNamed:@"bt_star_b"];
  50. [self.topView addSubview:img2];
  51. }
  52. }else{
  53. CGFloat width = frame.size.width/7.0;
  54. self.starWidth = width;
  55. for(int i = 0;i<5;i++){
  56. UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, width*ZOOM, width*ZOOM)];
  57. img.center = CGPointMake((i+1.5)*width, frame.size.height/2);
  58. img.image = [UIImage imageNamed:@"bt_star_a"];
  59. [self.bottomView addSubview:img];
  60. UIImageView *img2 = [[UIImageView alloc] initWithFrame:img.frame];
  61. img2.center = img.center;
  62. img2.image = [UIImage imageNamed:@"bt_star_b"];
  63. [self.topView addSubview:img2];
  64. }
  65. }
  66. self.enable = YES;
  67. }
  68. return self;
  69. }
  70. -(void)setViewColor:(UIColor *)backgroundColor{
  71. if(_viewColor!=backgroundColor){
  72. self.backgroundColor = backgroundColor;
  73. self.topView.backgroundColor = backgroundColor;
  74. self.bottomView.backgroundColor = backgroundColor;
  75. }
  76. }
  77. -(void)setStarNumber:(NSInteger)starNumber{
  78. if(_starNumber!=starNumber){
  79. _starNumber = starNumber;
  80. self.topView.frame = CGRectMake(0, 0, self.starWidth*(starNumber+1), self.bounds.size.height);
  81. if (starNumBlock) {
  82. starNumBlock([NSString stringWithFormat:@"%d",(int)starNumber]);
  83. }
  84. }
  85. }
  86. -(void)tap:(UITapGestureRecognizer *)gesture{
  87. if(self.enable){
  88. CGPoint point = [gesture locationInView:self];
  89. NSInteger count = (int)(point.x/self.starWidth)+1;
  90. self.topView.frame = CGRectMake(0, 0, self.starWidth*count, self.bounds.size.height);
  91. if(count>5){
  92. _starNumber = 5;
  93. }else{
  94. _starNumber = count-1;
  95. }
  96. if (starNumBlock) {
  97. starNumBlock([NSString stringWithFormat:@"%d",(int)_starNumber]);
  98. }
  99. }
  100. }
  101. -(void)pan:(UIPanGestureRecognizer *)gesture{
  102. if(self.enable){
  103. CGPoint point = [gesture locationInView:self];
  104. NSInteger count = (int)(point.x/self.starWidth);
  105. if(count>=0 && count<=5 && _starNumber!=count){
  106. self.topView.frame = CGRectMake(0, 0, self.starWidth*(count+1), self.bounds.size.height);
  107. _starNumber = count;
  108. if (starNumBlock) {
  109. starNumBlock([NSString stringWithFormat:@"%d",(int)_starNumber]);
  110. }
  111. }
  112. }
  113. }
  114. - (void)changeStarNumberBlock:(MyBlockType)starBlock
  115. {
  116. starNumBlock = starBlock;
  117. }
  118. /*
  119. // Only override drawRect: if you perform custom drawing.
  120. // An empty implementation adversely affects performance during animation.
  121. - (void)drawRect:(CGRect)rect
  122. {
  123. // Drawing code
  124. }
  125. */
  126. @end