swift 简单封装的一个五星评分器

前端之家收集整理的这篇文章主要介绍了swift 简单封装的一个五星评分器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

swift 封装的一个五星评分器,可以用于单纯展示评分,也可以用来手动打分。



gitHub地址:https://github.com/NinoWang/RatingBar

  1. import UIKit
  2. protocol RatingBarDelegate {
  3. func ratingChanged(ratingBar:RatingBar,newRating:Float)
  4. }
  5.  
  6. class RatingBar: UIView {
  7. var delegate:RatingBarDelegate?
  8.  
  9. var starRating:Float?
  10. var lastRating:Float?
  11. var starWidth:CGFloat?
  12. var starHeight:CGFloat?
  13.  
  14. var unSelectedImage:UIImage?
  15. var halfSelectedImage:UIImage?
  16. var fullSelectedImage:UIImage?
  17. var s1:UIImageView?
  18. var s2:UIImageView?
  19. var s3:UIImageView?
  20. var s4:UIImageView?
  21. var s5:UIImageView?
  22. //是否是指示器
  23. var isIndicator:Bool = false
  24. func setseletedState(deselectedName:String?,halfSelectedName:String?,fullSelectedName:String?,starSideLength:CGFloat,delegate:RatingBarDelegate){
  25. self.delegate = delegate
  26. unSelectedImage = UIImage(named: deselectedName!)
  27. fullSelectedImage = UIImage(named: fullSelectedName!)
  28. halfSelectedImage = halfSelectedName == nil ? fullSelectedImage:UIImage(named: halfSelectedName!)
  29. starWidth = 0
  30. starHeight = 0
  31. if (starHeight < starSideLength) {
  32. starHeight = starSideLength
  33. }
  34. if (starWidth < starSideLength) {
  35. starWidth = starSideLength
  36. }
  37.  
  38. //控件宽度适配
  39. var frame = self.frame
  40. var viewWidth:CGFloat = starWidth! * 5
  41. if (frame.size.width) > viewWidth {
  42. viewWidth = frame.size.width
  43. }
  44. frame.size.width = viewWidth
  45. self.frame = frame
  46. starRating = 0
  47. lastRating = 0
  48. s1 = UIImageView(image: unSelectedImage)
  49. s2 = UIImageView(image: unSelectedImage)
  50. s3 = UIImageView(image: unSelectedImage)
  51. s4 = UIImageView(image: unSelectedImage)
  52. s5 = UIImageView(image: unSelectedImage)
  53. //星星间距
  54. let space:CGFloat = (viewWidth - starWidth!*5)/6
  55. var starX = space
  56. let starY = (frame.height - starHeight!)/2
  57. s1?.frame = CGRectMake(starX,starY,starWidth!,starHeight!)
  58. starX = starX + starWidth! + space
  59. s2?.frame = CGRectMake(starX,starHeight!)
  60. starX = starX + starWidth! + space
  61. s3?.frame = CGRectMake(starX,starHeight!)
  62. starX = starX + starWidth! + space
  63. s4?.frame = CGRectMake(starX,starHeight!)
  64. starX = starX + starWidth! + space
  65. s5?.frame = CGRectMake(starX,starHeight!)
  66. starX = starX + starWidth! + space
  67. s1?.userInteractionEnabled = false
  68. s2?.userInteractionEnabled = false
  69. s3?.userInteractionEnabled = false
  70. s4?.userInteractionEnabled = false
  71. s5?.userInteractionEnabled = false
  72. self.addSubview(s1!)
  73. self.addSubview(s2!)
  74. self.addSubview(s3!)
  75. self.addSubview(s4!)
  76. self.addSubview(s5!)
  77. }
  78. //设置评分值
  79. func displayRating(rating:Float){
  80. s1?.image = unSelectedImage
  81. s2?.image = unSelectedImage
  82. s3?.image = unSelectedImage
  83. s4?.image = unSelectedImage
  84. s5?.image = unSelectedImage
  85. if (rating >= 1) {
  86. s1?.image = halfSelectedImage
  87. }
  88. if (rating >= 2) {
  89. s1?.image = fullSelectedImage
  90. }
  91. if (rating >= 3) {
  92. s2?.image = halfSelectedImage
  93. }
  94. if (rating >= 4) {
  95. s2?.image = fullSelectedImage
  96. }
  97. if (rating >= 5) {
  98. s3?.image = halfSelectedImage
  99. }
  100. if (rating >= 6) {
  101. s3?.image = fullSelectedImage
  102. }
  103. if (rating >= 7) {
  104. s4?.image = halfSelectedImage
  105. }
  106. if (rating >= 8) {
  107. s4?.image = fullSelectedImage
  108. }
  109. if (rating >= 9) {
  110. s5?.image = halfSelectedImage
  111. }
  112. if (rating >= 10) {
  113. s5?.image = fullSelectedImage
  114. }
  115. starRating = rating
  116. lastRating = rating
  117. delegate?.ratingChanged(self,newRating: rating)
  118.  
  119. }
  120. func rating() -> Float{
  121. return starRating!
  122. }
  123. //手势
  124. override func touchesBegan(touches: Set<UITouch>,withEvent event: UIEvent?) {
  125. super.touchesBegan(touches,withEvent: event)
  126. }
  127. override func touchesEnded(touches: Set<UITouch>,withEvent event: UIEvent?) {
  128. super.touchesEnded(touches,withEvent: event)
  129. self.touchesRating(touches)
  130. }
  131. override func touchesMoved(touches: Set<UITouch>,withEvent event: UIEvent?) {
  132. super.touchesMoved(touches,withEvent: event)
  133. self.touchesRating(touches)
  134. }
  135. //触发
  136. func touchesRating(touches:NSSet){
  137. if(self.isIndicator == false){
  138. return
  139. }
  140. let point:CGPoint = touches.anyObject()!.locationInView(self)
  141. let space:CGFloat = (self.frame.size.width - starWidth!*5)/6
  142. var newRating:Float = 0
  143. if (point.x >= 0 && point.x <= self.frame.size.width) {
  144. if (point.x <= space+starWidth!*0.5) {
  145. newRating = 1;
  146. }else if (point.x < space*2+starWidth!){
  147. newRating = 2;
  148. }else if (point.x < space*2+starWidth!*1.5){
  149. newRating = 3;
  150. }else if (point.x <= 3*space+2*starWidth!){
  151. newRating = 4;
  152. }else if (point.x <= 3*space+2.5*starWidth!){
  153. newRating = 5;
  154. }else if (point.x <= 4*space+3*starWidth!){
  155. newRating = 6;
  156. }else if (point.x <= 4*space+3.5*starWidth!){
  157. newRating = 7;
  158. }else if (point.x <= 5*space+4*starWidth!){
  159. newRating = 8;
  160. }else if (point.x <= 5*space+4.5*starWidth!){
  161. newRating = 9;
  162. }else {
  163. newRating = 10;
  164. }
  165. }
  166. if (newRating != lastRating){
  167. self.displayRating(newRating)
  168. }
  169. }
  170.  
  171. }

猜你在找的Swift相关文章