This is an easy to integrate view controller that allows a UIScrollView or any of their widely used subclasses (such as UITableView and UICollectionView) to have a header view that can be animated along with scrolling.
Here an example:
OLSDynamicHeaderViewController is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "OLSDynamicHeaderViewController"
- Make your
UIViewController
to inherit either one of the helpersOLSDynamicHeaderTableViewController
orOLSDynamicHeaderCollectionViewController
class MyViewController: OLSDynamicHeaderTableViewController {
// MARK - OLSDynamicHeaderViewControllerProtocol
override func headerViewInstance() -> OLSDynamicHeaderView {
...
}
// ... (View Controller lifecycle and logic)
- On
headerViewInstance()
return an instance ofOLSDynamicHeaderView
subclass. This class is a basic UIView subclass that conforms the protocol that will help you with the animation part =D.
- Create a subclasss of
OLSDynamicHeaderView
, and implement the protocol:
class MyDynamicHeader: OLSDynamicHeaderView {
func maxHeight() -> CGFloat { ... }
func minHeight() -> CGFloat { ... }
func resize(withProgress progress: CGFloat) { ... }
func overflow(withPoints points: CGFloat) { ... }
}
maxHeight
returns the maximum height allowed for this view.minHeight
returns the minimum height allowed for this view.resize(withProgress)
will be called when the user scrolls and your header needs to animate.progress
goes from 0 to 1, 0 min height reached, 1 max height reached.overflow(withPoints)
will be called when the user scrolls further the max height.points
goes from 0 to N.
- The project includes a basic sample code you can use as a guidence to integrate the component in your app.
- The project provides helper subclasses for tableView and collectionView, and the example above takes advantage of those classes. But you can go a step under and inherit from
OLSDynamicHeaderViewController
and move from there to have additional control. - If you have questions, improvements, ideas or just comments, see to the links below!
Omar Hagopian, Antihero @OrangeLoops
Contact:
OLSDynamicHeaderViewController is under the MIT license. See the LICENSE file for more info!