Last active
August 17, 2016 00:50
-
-
Save jstart/9287ab16f40a4ba888204dd00fe67c29 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
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))!) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you remove the string values from the ViewCategory enum, the enum value will default to the name as a string. See fork! :D