Skip to content

Instantly share code, notes, and snippets.

@jstart
Last active August 17, 2016 00:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jstart/9287ab16f40a4ba888204dd00fe67c29 to your computer and use it in GitHub Desktop.
Save jstart/9287ab16f40a4ba888204dd00fe67c29 to your computer and use it in GitHub Desktop.
enum ViewCategory : String {
case home = "home"
case info = "info"
case settings = "settings"
case interests = "interests"
func button() -> UIButton {
let button = UIButton()
button.setImage(UIImage(named:rawValue), for: .normal)
return button
}
}
protocol PageControlDelegate {
func selectedIndex(_ index:Int)
}
class PageControl : NSObject {
var stack : UIStackView? = nil
var delegate : PageControlDelegate?
init(categories: [ViewCategory]) {
let array = categories.map({return $0.button()})
stack = UIStackView(arrangedSubviews: array)
stack?.spacing = 20
stack?.distribution = .fillEqually
stack?.alignment = .center
super.init()
array.forEach({ button in
button.addTarget(self, action: #selector(selected(sender:)), for: .touchUpInside)
})
}
func selectIndex(_ index:Int){
for button in (stack?.subviews)! as! [UIButton] {
button.isSelected = true
}
}
func selected(sender : UIButton){
for button in (stack?.subviews)! as! [UIButton] {
button.isSelected = button == sender
}
delegate?.selectedIndex((stack?.subviews.index(of: sender))!)
}
}
@codytwinton
Copy link

If you remove the string values from the ViewCategory enum, the enum value will default to the name as a string. See fork! :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment