/Shazam

A pure-Swift library for nested display of horizontal and vertical scrolling views

Primary LanguageSwiftMIT LicenseMIT

Shazam

License MIT Pod version Platform info Support Swift 4.2

A pure-Swift library for nested display of horizontal and vertical scrolling views.

demo

Requirements

  • iOS 9.0+
  • Swift 4.2+
  • Xcode 10+

Installation

CocoaPods (recommended)

use_frameworks!

pod 'Shazam'

Usage

中文文档

First make sure to import the framework:

import Shazam

Basically, we just need to provide the list of child view controllers to show. Then call some necessary methods.

Let's see the steps to do this:

Create a ShazamPageViewController subclass

import Shazam

class PageViewController: ShazamPageViewController {
  // ...
}

Provide the view controllers that will appear embedded into the ShazamPageViewController

override func numberOfViewControllers(in pageController: ShazamPageViewController) -> Int {
    return count
}
    
override func pageController(_ pageController: ShazamPageViewController, viewControllerAt index: Int) -> (UIViewController & ShazamChildViewController) {
    // ...
    return viewController
}
    

Every UIViewController that will appear within the ShazamPageViewController should conform to ShazamChildViewController by implementing func shazamChildScrollView() -> UIScrollView

import Shazam
class ChildViewController: UIViewController, ShazamChildViewController {

    @IBOutlet weak var tableView: UITableView!
    func shazamChildScrollView() -> UIScrollView {
        return tableView
    }
    // ...
}

Note: The scrollview of ChildViewController must reserve the height of headerView + menuView at the top. For example, in BatmanViewController in Demo, you need to set the height of Header of collectionView.

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        guard let pageViewContoller = szPageViewContoller else {
            return .zero
        }
        let headerViewHeight = pageViewContoller.headerViewHeightFor(pageViewContoller)
        let menuViewHeight = pageViewContoller.menuViewHeightFor(pageViewContoller)
        return CGSize(width: collectionView.bounds.width, height: headerViewHeight + menuViewHeight)
    }

Provide the headerView and headerView height

override func headerViewFor(_ pageController: ShazamPageViewController) -> UIView & ShazamHeaderView {
    return HeaderView()
}

override func headerViewHeightFor(_ pageController: ShazamPageViewController) -> CGFloat {
    return headerViewHeight
}

The headerView should conform to ShazamHeaderView by implementing func userInteractionViews() -> [UIView]?

func userInteractionViews() -> [UIView]? {
    return [button]
}

Provide the menuView and menuView height

override func menuViewFor(_ pageController: ShazamPageViewController) -> UIView {
    return menuView
}

override func menuViewHeightFor(_ pageController: ShazamPageViewController) -> CGFloat {
    return menuViewHeight
}

Update menuView's layout when main scroll view did scroll and check state when did end scoll

override func pageController(_ pageController: ShazamPageViewController, mainScrollViewDidScroll scrollView: UIScrollView) {
    menuView.updateLayout(scrollView)
}

override func pageController(_ pageController: ShazamPageViewController,
                                 mainScrollViewDidEndScroll scrollView: UIScrollView) {
    menuView.checkState()
}

Examples

Follow these 4 steps to run Example project:

  1. Clone Shazam repository
  2. Run the pod install command
  3. Open Shazam workspace
  4. Run the Shazam-Demo project.

License

Shazam is released under the MIT license. See LICENSE for details.