-
-
Save codytwinton/8f387de7cb1160fb41f45987acabea05 to your computer and use it in GitHub Desktop.
This file contains 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
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