The ios app demonstrates animated segmented buttons that are integrated with collectionView instagram-like followers/following screen.
The app is created programmatically using UIKit with no storyboards. However, if you are using storyboards in your project:
- Import 'SegmentedButtonsViewdrop.swift' file into your project.
- Drop a UIView in your UIViewController.
- Select the UIView, then in the identity inspector write in the custom class textfield 'SegmentedButtonsView' then hit enter.
For the functionality that is demonstrated above, there are two main classes:
SegmentedButtonsView and MainViewController.
for instance in the code below:
lazy var segmentedButtonsView:SegmentedButtonsView = {
let segmentedButtonsView = SegmentedButtonsView()
segmentedButtonsView.setLabelsTitles(titles: ["Followers", "Following"])
return collectionViewHeader
}()
Please consider to call 'setLabelsTitles' method of 'SegmentedButtonsView' instance in order to set labels titles. In your viewController make it conforms to 'SegmentedControlDelegate' in order to trigger selected button index:
extension MainViewController: SegmentedControlDelegate {
//MARK: - segmented Control delegate
func didIndexChanged(at index: Int) {
if index == 0 {
// do something
}else if index == 1 {
//do something
}
}
}
If you're willing to use segmentedViewButtons to be integrated with a collection view with more than 2 buttons, you can replace the code inside the 'collectionViewDidScroll' delegate function of 'SegmentedButtonsView.swift' :
if (label.frame.width / 2 >= self.selectorView.frame.origin.x && titles[0] == label.text! ||
label.frame.width / 2 <= self.selectorView.frame.origin.x && titles[1] == label.text! ) {
label.textColor = selectorTextColor
}else {
label.textColor = textColor
}
with the following code that supports more than two buttons(labeLs) view:
if label.frame.origin.x == self.selectorView.frame.origin.x {
label.textColor = selectorTextColor
} else {
label.textColor = textColor
}