Last active
January 19, 2016 11:43
-
-
Save Athosone/270979551e4132cc24ee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// RatingSession.swift | |
// | |
// | |
// Created by Werck Ayrton on 19/01/2016. | |
// | |
import UIKit | |
class RatingSession: UIView { | |
var rating:Int = 0 { | |
didSet { | |
setNeedsLayout() | |
} | |
} | |
var ratingButtons = [UIButton]() | |
var stars = 5 | |
var spacing:Int { | |
return Int(frame.width - (frame.height * CGFloat(stars))) / stars | |
} | |
var buttonSize:CGFloat { | |
return frame.height | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
let filledStarImage = UIImage(named: "fullHeart") | |
let emptyStarImage = UIImage(named: "emptyHeart") | |
backgroundColor = UIColor.clearColor() | |
for _ in 0..<stars | |
{ | |
let button = UIButton() | |
button.setImage(emptyStarImage, forState: .Normal) | |
button.setImage(filledStarImage, forState: .Selected) | |
button.setImage(filledStarImage, forState: [.Highlighted, .Selected]) | |
button.adjustsImageWhenHighlighted = false | |
button.addTarget(self, action: "ratingButtonTapped:", forControlEvents: .TouchDown) | |
ratingButtons += [button] | |
addSubview(button) | |
} | |
} | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
let filledStarImage = UIImage(named: "fullHeart") | |
let emptyStarImage = UIImage(named: "emptyHeart") | |
backgroundColor = UIColor.clearColor() | |
for _ in 0..<stars | |
{ | |
let button = UIButton() | |
button.setImage(emptyStarImage, forState: .Normal) | |
button.setImage(filledStarImage, forState: .Selected) | |
button.setImage(filledStarImage, forState: [.Highlighted, .Selected]) | |
button.adjustsImageWhenHighlighted = false | |
button.addTarget(self, action: "ratingButtonTapped:", forControlEvents: .TouchDown) | |
ratingButtons += [button] | |
addSubview(button) | |
} | |
} | |
override func layoutSubviews() { | |
var buttonFrame = CGRect(x: 0, y: 0, width: buttonSize, height: buttonSize) | |
// Offset each button's origin by the length of the button plus spacing. | |
for (index, button) in ratingButtons.enumerate() | |
{ | |
buttonFrame.origin.x = CGFloat(index * (Int(buttonSize) + spacing)) | |
button.frame = buttonFrame | |
} | |
updateButtonSelectionStates() | |
} | |
func ratingButtonTapped(button:UIButton) | |
{ | |
rating = ratingButtons.indexOf(button)! + 1 | |
updateButtonSelectionStates() | |
} | |
override func intrinsicContentSize() -> CGSize { | |
let buttonSize = Int(frame.size.height) | |
let width = (buttonSize + spacing) * stars | |
return CGSize(width: width, height: buttonSize) | |
} | |
func updateButtonSelectionStates() { | |
for (index, button) in ratingButtons.enumerate() | |
{ | |
button.selected = index < rating | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment