Advertisement
Guest User

Untitled

a guest
Nov 27th, 2015
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. //
  2. // MNGStarRating.swift
  3. // CommonCents
  4. //
  5. // Created by Tommie N. Carter, Jr., MBA on 11/27/15.
  6. // Copyright © 2015 MING Technology. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. /*
  12. Usage: On Controller ~>
  13. @IBOutlet var ratingHeader: MNGStarRating! {
  14. didSet {
  15. ratingHeader.ratingDelegate = self
  16. }
  17. }
  18.  
  19. //within delegate methods: ratingWasChanged
  20. self.ratingButton.setTitle(self.starRatingOutlet.starTitle(), forState: UIControlState.Normal)
  21.  
  22. */
  23. protocol MNGStarRatingDelegate {
  24. func ratingWasChanged(value:Float?)
  25. }
  26.  
  27. class MNGStarRating: UIView {
  28.  
  29. //MARK: Outlets
  30. @IBOutlet var starCollection:[UIButton]!
  31.  
  32. //MARK: Class Properties
  33. var ratingDelegate:MNGStarRatingDelegate? = nil
  34. private var starRating:Float = 0.0
  35. //MARK: View Lifecycle
  36. override func awakeFromNib() {
  37. super.awakeFromNib()
  38. loadDefaultRatingImages()
  39. }
  40. /*
  41. // Only override drawRect: if you perform custom drawing.
  42. // An empty implementation adversely affects performance during animation.
  43. override func drawRect(rect: CGRect) {
  44. // Drawing code
  45. }
  46. */
  47.  
  48. //MARK: Helper Methods
  49.  
  50. func loadDefaultRatingImages() {
  51. for b in starCollection {
  52.  
  53. switch b.tag {
  54. case 1:
  55. b.setBackgroundImage(UIImage(named: "star1"), forState: .Highlighted)
  56. break
  57. case 2:
  58. b.setBackgroundImage(UIImage(named: "star2"), forState: .Highlighted)
  59. break
  60. case 3:
  61. b.setBackgroundImage(UIImage(named: "star3"), forState: .Highlighted)
  62. break
  63. case 4:
  64. b.setBackgroundImage(UIImage(named: "star4"), forState: .Highlighted)
  65. break
  66. case 5:
  67. b.setBackgroundImage(UIImage(named: "star5"), forState: .Highlighted)
  68. break
  69. default:
  70. break
  71.  
  72. }
  73. }
  74. }
  75. func starTitle() -> String {
  76. guard self.starRating > 0.0 else {
  77. return "Transaction Rating"
  78. }
  79. let title = [String](count: Int(self.starRating), repeatedValue: "✭")
  80. return title.joinWithSeparator("")
  81. }
  82. //MARK: Action Methods
  83. @IBAction func highlightButtonsUpto(button:UIButton) {
  84. let targetValue = (button.tag)
  85. let _ = starCollection.map({ $0.highlighted = false })
  86.  
  87. for b in starCollection {
  88. if targetValue > b.tag {
  89. b.highlighted = true
  90. }
  91. }
  92. //keeps last button highlighted
  93. button.cancelTrackingWithEvent(nil)
  94. button.highlighted = true
  95.  
  96. starRating = Float(targetValue)
  97. ratingDelegate?.ratingWasChanged(Float(targetValue))
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement