extension UIView {
/**
To setup your rating view, you need to add 5 UIImageViews inside a UIView
They must be in the "off" state (not filled)
This method assume it will receive values around 0 to 5,
being only possible to have values iterating by 0.5.
E.g: 0, 0.5, 1, 1.5, 2.5...
*/
func paintStars(rating: Double) {
let integerRating = Int(rating)
for i in 0..<integerRating {
if integerRating == 0 {
return
}
(subviews[i] as! UIImageView).image = UIImage(named: "star") //filled star
}
if rating.truncatingRemainder(dividingBy: 1) != 0 {
//check if the number is integer
(subviews[integerRating] as! UIImageView).image = UIImage(named: "star_semi") //half star painted
}
}
}