Skip to content

Instantly share code, notes, and snippets.

@codytwinton
Forked from jstart/PageControl.swift
Last active August 17, 2016 00:49
Show Gist options
  • Save codytwinton/8f387de7cb1160fb41f45987acabea05 to your computer and use it in GitHub Desktop.
Save codytwinton/8f387de7cb1160fb41f45987acabea05 to your computer and use it in GitHub Desktop.
enum ViewCategory : String {
case home
case info
case settings
case 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))!)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment