ScrollCoordinator 1.2.0

ScrollCoordinator 1.2.0

Shubhankar Yash 维护。



  • 作者
  • shubhankaryash

ScrollCoordinator

Version License Platform

示例

要运行示例项目,请克隆仓库,然后首先从 Example 目录运行 pod install

需求

安装

ScrollCoordinator 通过 CocoaPods 提供。要安装它,只需简单地将以下行添加到您的 Podfile:

pod 'ScrollCoordinator'

屏幕截图

隐藏 NavigationBar 和 ToolBar

隐藏 NavigationBar 和 TabBar

锚定 ScrollView

用法

使用方法,请让你的视图控制器符合 ScrollCoordinatorManager 协议。如果打算使用它,则需要初始化滚动协调器。你可以自定义何时以及是否初始化它的逻辑。这里的实现只在方法内有最小内容。你可以根据需要添加自定义逻辑和执行 further operations,但将事件传递给滚动协调器是必须的。

//Implement the ScrollCoordinatorManager in your view controller
class YourViewController: UIViewController, ScrollCoordinatorManager {
//Variable of ScrollCoordinatorManager protocol
var scrollCoordinator: ScrollCoordinator?

/*
Methods of ScrollCoordinatorManager protocol
*/
func getScrollCoordinator() -> ScrollCoordinator? {
    return scrollCoordinator
}

func addBehaviour(view: UIView, behaviour: Behaviour) {
    scrollCoordinator?.addBehaviour(view: view, behaviour: behaviour)
}

func removeBehaviour(view: UIView) {
    scrollCoordinator?.removeBehaviour(view: view)
}

func getBehaviour(for view: UIView) -> Behaviour? {
    return scrollCoordinator?.getBehaviour(for: view)
}

func registerScrollViewToCoordinator(scrollView: UIScrollView) {
    scrollCoordinator?.registerScrollView(scrollView: scrollView)
}

func informCoordinatorVCWillAppear() {
    scrollCoordinator?.vcWillAppear()
}

func informCoordinatorVCWillDisappear() {
    scrollCoordinator?.vcWillDisappear()
}

func informCoordinatorVCDidSublayoutViews() {
    scrollCoordinator?.vcDidSublayoutViews()
}
}

所有需要手势和滚动事件的滚动视图都需要注册到实现 ScrollCoordinatorManager 的 VC 中。以上示例中的 YourViewController 实现了此方法。

registerScrollViewToCoordinator(scrollView: tableView)

类似地,所有你想要添加到实现 ScrollCoordinatorManager 的 VC 的行为都需要添加。这也由 YourViewController 实现。请注意,NavBarSnapBehaviour 在嵌套滚动视图中不受支持。

//Adding NavBarSnapBehaviour
if (navigationController != nil || navigationController?.navigationBar != nil) {
    if let navController = navigationController {
        self.extendedLayoutIncludesOpaqueBars = true //this needs to be done
        addBehaviour(view: navController.navigationBar, behaviour: NavbarSnapBehaviour(snapDirection: .TOP, navController: navController, scrollView: tableView, refreshControl: nil, snapDelegate: nil))
    }
}

//Adding the SnapBehaviour to the bottom bar
if let bottomBar = navigationController?.tabBarController?.tabBar {
    addBehaviour(view: navController.navigationBar, behaviour: NavbarSnapBehaviour(snapDirection: .TOP, navController: navController, scrollView: tableView, refreshControl: nil, snapDelegate: nil))
}

//Adding the AnchorBehaviour
if isAnchorBehaviourEnabled {
    tableView.isScrollEnabled = false //this needs to be disabled to allow the behaviour to scroll the view
    let anchorHeight: CGFloat = 60 //this is the height where the scroll will be anchored
    addBehaviour(view: tableView, behaviour: AnchorBehaviour(scrollView: tableView, anchorHeight: anchorHeight, shouldPreventOriginalScroll: true))
}

要创建自己的自定义行为,你必须符合 Behavior 协议。

class PercentageBehaviour: Behaviour {
//This variable determines whether you would receive the scroll events after gesture ends. If you want to listen only to the gestures keep this false. Otherwise you can make this true.
var needsPostGestureInfo: Bool = true

//You will receive all the gestures with the relevant info from the scroll
func handleGestureFromDependantScroll(gestureInfo: PanGestureInformation, scrollTranslationInfo: ScrollTranslationInformation) {
}

//If your behaviour needs to respond to only a single scroll view you can return that as the dependant scrollview. Then you would only receive events from that scrollview
func getDependantScrollView() -> UIScrollView? {
}

//Your behaviour will receive this when a new gesture starts
func gestureDidStart(scrollView: UIScrollView) {
}

//Your behaviour will receive this when the gesture ends
func gestureDidFinish(gestureInfo: PanGestureInformation, scrollView: UIScrollView) {
}

//These are scroll events after the gesture has finished but the scrollview is still scrolling
public func scrollDidTranslateAfterGesture(scrollTranslationInfo: ScrollTranslationInformation) {
}

//These are vc level events
func vcWillAppear() {
}

func vcWillDisappear() {
}

func vcDidSubLayoutViews() {
}


}

作者

shubhankaryashmanishPatwari

许可证

ScrollCoordinator 适用于 MIT 许可证。有关更多信息,请参阅 LICENSE 文件。