CTSlidingUpPanel 1.2.0

CTSlidingUpPanel 1.2.0

Giorgi Andriadze 维护。



  • Giorgi Andriadze

iOS-CTSlidingUpPanel

Version License Platform

将任何视图转换为滑动面板

面板支持锚点、TabBarController 和 NavigationController,也具有基本的 TableView 支持。

Cocoapods

Swift 4.2

将以下内容添加到您的 Podfile 中

pod "CTSlidingUpPanel"

Swift 4.0

pod "CTSlidingUpPanel", '~> 1.0.4'

如何导入

请确保构建并清理项目

import CTSlidingUpPanel

Objective C - 支持

版本 1.2.0 开始,您可以在 Objective C 项目中使用这个库。

要这样做,首先在您的 podfile 中添加: use_frameworks!。 运行 pod install 并构建项目。

要在 Objective C 类中导入使用

@import CTSlidingUpPanel;

Objective C 中构造函数的示例

[[CTBottomSlideController alloc] initWithParent:self.view bottomView:_testView tabController:nil navController:nil visibleHeight:20];

手动设置说明

  1. 下载此仓库
  2. 将文件从 lib 文件夹复制到您的项目中。
  3. 应该完成了。

如何使用

您可以查看示例项目以更好地了解内容工作原理。

但以下是使用 Storyboard 的基本步骤

  1. 将任何视图(或容器视图)拖放到父视图。 不要为此视图设置任何约束

  2. 创建要用作底部面板的视图的 outlet

然后在您的 ViewController 中

    @IBOutlet weak var bottomView: UIView!
    var bottomController:CTBottomSlideController?;
    

    override func viewDidLoad() {
        super.viewDidLoad()
        //You can provide nil to tabController and navigationController
        bottomController = CTBottomSlideController(parent: view, bottomView: bottomView, 
                        tabController: self.tabBarController?,
                        navController: self.navigationController, visibleHeight: 64)
        //0 is bottom and 1 is top. 0.5 would be center                
        bottomController?.setAnchorPoint(anchor: 0.7)
    }
    
    ///Added in Version: 1.1.0 - To support Screen orientation changes!
    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
        bottomController?.viewWillTransition(to: size, with: coordinator)
    }
    

完成,您提供的底部视图应该可以上下滑动。

您可以在不带 Storyboard 的情况下做同样的事情。将新的子视图添加到父视图,然后将其提供给 CTBottomSlideController。建议您不要为此子视图设置任何约束。

如果您想自定义滑动面板的宽度/位置

在这种情况下,您应将:顶边、高度和其他您想设置的约束(但不要设置底部约束)设置到滑动视图,然后使用此初始化器

bottomController = CTBottomSlideController(topConstraint: slidingPanelTopConstraint, 
                                            heightConstraint: slidingPanelHeightConstraint, 
                                            parent: view, 
                                            bottomView: bottomView, 
                                            tabController: self.tabBarController!, 
                                            navController: self.navigationController, 
                                            visibleHeight: 64)

非常重要,您不要设置滑动视图的底部约束,否则滑动视图将开始调整大小,这可能会影响性能。

iPhone X 安全区域支持

  1. 取一个滑块视图(您提供给CTBottomSlideController的视图)并将它包装在一个通用的UIView中。
  2. 在包装视图中启用Clip to Bounds。
  3. 为此包装视图设置任何您想要的约束,并将其放置在您想要的任何位置,根据需要调整大小。
  4. 然后在CTBottomSlideController构造函数中,提供该包装视图作为父视图,而不是根视图。
//This is a wrapper view
@IBOutlet weak var parentView: UIView!

//In viewDidLoad() or something
parentView.clipsToBounds = true;
bottomController = CTBottomSlideController(parent: parentView/*instead of view*/, bottomView: bottomView, 
                        tabController: self.tabBarController!,
                        navController: self.navigationController, visibleHeight: 64)

获取滑块控制器的事件

有2种方法可以做到这一点。一种是通过委托,另一种是通过使用闭包。

委托

  1. 将此代码添加到您的视图控制器中
   class ViewController: UIViewController, CTBottomSlideDelegate
   {...}
  1. 将视图控制器设置为委托
   bottomController?.delegate = self;
  1. 实现以下方法
  func didPanelCollapse();
  func didPanelExpand();
  func didPanelAnchor();
  func didPanelMove(panelOffset: CGFloat);

闭包

 bottomController?.onPanelExpanded = {
      print("Panel Expanded in closure")
 }
        
 bottomController?.onPanelCollapsed = {
      print("Panel Collapsed in closure")
 }
        
 bottomController?.onPanelAnchored = {
      print("On Panel anchored")
 }
        
 bottomController?.onPanelMoved = { offset in
      print("Panel moved in closure " + offset.description)
 }

方法和其他内容

如果您想根据TableView的偏移量使滑动面板上下滑动,请使用此方法。

func set(table:UITableView)

如果您想指定滑动面板应该上升多高,请使用此方法。

func setExpandedTopMargin(pixels: CGFloat)

使用这些方法来编程更改面板的状态。

func expandPanel();
func anchorPanel();
func closePanel();
func hidePanel();
func setSlideEnabled(bool) // Enable or disable sliding

使用此方法检查面板的状态。

bottomController?.currentState 

状态可以是:

.collapsed
.expanded
.anchored
.hidden