CDPStarEvaluation.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // CDPStarEvaluation.m
  3. // CDPStarEvaluation
  4. //
  5. // Created by MAC on 15/4/20.
  6. // Copyright (c) 2015年 com.xuezi.CDP. All rights reserved.
  7. //
  8. #import "CDPStarEvaluation.h"
  9. @implementation CDPStarEvaluation
  10. -(id)initWithFrame:(CGRect)frame onTheView:(UIView *)view{
  11. if (self=[super init]) {
  12. _commentText=@"请评分";
  13. //空星级imageView
  14. _starEmptyImageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"评价1"]];
  15. _starEmptyImageView.frame=frame;
  16. _starEmptyImageView.userInteractionEnabled=YES;
  17. [view addSubview:_starEmptyImageView];
  18. UIImage *image = [self reSizeImage:[UIImage imageNamed:@"评价2"] toSize:CGSizeMake(frame.size.width, frame.size.height)];
  19. _starImageView=[[UIImageView alloc] initWithImage:image];
  20. _starImageView.frame=CGRectMake(frame.origin.x,frame.origin.y,0,frame.size.height);
  21. _starImageView.contentMode=UIViewContentModeLeft;
  22. _starImageView.clipsToBounds=YES;
  23. [view addSubview:_starImageView];
  24. //单击手势
  25. UITapGestureRecognizer *tapGR=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGR:)];
  26. [_starEmptyImageView addGestureRecognizer:tapGR];
  27. //拖动手势
  28. UIPanGestureRecognizer *panGR=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGR:)];
  29. [_starEmptyImageView addGestureRecognizer:panGR];
  30. }
  31. return self;
  32. }
  33. - (UIImage *)reSizeImage:(UIImage *)image toSize:(CGSize)reSize
  34. {
  35. UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));
  36. [image drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)];
  37. UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
  38. UIGraphicsEndImageContext();
  39. return reSizeImage;
  40. }
  41. #pragma mark 外部设置星级
  42. -(void)setStarWithFloat:(CGFloat)wid
  43. {
  44. _starEmptyImageView.userInteractionEnabled = NO;
  45. _starImageView.frame=CGRectMake(_starEmptyImageView.frame.origin.x,_starEmptyImageView.frame.origin.y,wid * _starEmptyImageView.bounds.size.width,_starEmptyImageView.bounds.size.height);
  46. if (wid<=1/5.0) {
  47. //差
  48. _commentText=@"差";
  49. }
  50. else if(wid<=2/5.0&&wid>1/5.0){
  51. //一般
  52. _commentText=@"一般";
  53. }
  54. else if(wid<=3/5.0&&wid>2/5.0){
  55. //好
  56. _commentText=@"好";
  57. }
  58. else if(wid<=4/5.0&&wid>3/5.0){
  59. //很好
  60. _commentText=@"很好";
  61. }
  62. else{
  63. //非常好
  64. _commentText=@"非常好";
  65. }
  66. }
  67. #pragma mark 手势
  68. //单击手势
  69. -(void)tapGR:(UITapGestureRecognizer *)tapGR{
  70. CGPoint tapPoint=[tapGR locationInView:_starEmptyImageView];
  71. _width = tapPoint.x/_starEmptyImageView.bounds.size.width;
  72. _starImageView.frame=CGRectMake(_starEmptyImageView.frame.origin.x,_starEmptyImageView.frame.origin.y,tapPoint.x,_starEmptyImageView.bounds.size.height);
  73. if (_width<=1/5.0) {
  74. //差
  75. _commentText=@"差";
  76. }
  77. else if(_width<=2/5.0&&_width>1/5.0){
  78. //一般
  79. _commentText=@"一般";
  80. }
  81. else if(_width<=3/5.0&&_width>2/5.0){
  82. //好
  83. _commentText=@"好";
  84. }
  85. else if(_width<=4/5.0&&_width>3/5.0){
  86. //很好
  87. _commentText=@"很好";
  88. }
  89. else{
  90. //非常好
  91. _commentText=@"非常好";
  92. }
  93. if (_delegate) {
  94. [_delegate theCurrentCommentText:_commentText Tag:_delegateTag];
  95. }
  96. }
  97. //拖动手势
  98. - (void)panGR:(UIPanGestureRecognizer *)panGR
  99. {
  100. CGPoint panPoint = [panGR locationInView:_starEmptyImageView];
  101. if (panPoint.x<0||panPoint.x>_starEmptyImageView.bounds.size.width) {
  102. return;
  103. }
  104. _width=panPoint.x/_starEmptyImageView.bounds.size.width;
  105. _starImageView.frame=CGRectMake(_starEmptyImageView.frame.origin.x,_starEmptyImageView.frame.origin.y,panPoint.x,_starEmptyImageView.bounds.size.height);
  106. if (_width<=1/5.0) {
  107. //差
  108. _commentText=@"差";
  109. }
  110. else if(_width<=2/5.0&&_width>1/5.0){
  111. //一般
  112. _commentText=@"一般";
  113. }
  114. else if(_width<=3/5.0&&_width>2/5.0){
  115. //好
  116. _commentText=@"好";
  117. }
  118. else if(_width<=4/5.0&&_width>3/5.0){
  119. //很好
  120. _commentText=@"很好";
  121. }
  122. else{
  123. //非常好
  124. _commentText=@"非常好";
  125. }
  126. if (_delegate) {
  127. [_delegate theCurrentCommentText:_commentText Tag:_delegateTag];
  128. }
  129. }
  130. @end