ALAccordion 0.4.0

ALAccordion 0.4.0

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最后发布2016 年 9 月
SPM支持 SPM

Samuel Williams 维护。



  • Samuel Williams 和 Chris Miller

ALAccordion

概述

ALAccordion 是一个为 iOS 优化的折叠风格容器视图,用于管理一组可展开/折叠的内容部分。当部分展开时,它将消耗整个屏幕(见截图)。

每个折叠的部分都与一个自定义视图控制器相关联,该控制器管理其自己的标题和内容视图。ALAccordion 依赖于自动布局来为标题视图提供固有内容大小。这使得拥有可以由每个部分修改和动画处理的自定义部分标题变得非常容易。

ALAccordion 还允许可选的标题和页脚视图,当部分展开时它们将隐藏。

要求

  • Xcode 8.0
  • Swift 3.0
  • 需要 iOS 7+
  • 需要 AutoLayout

Swift 2.2 使用版本 0.3

注意: ALAccordion 使用 ARC

屏幕截图

ALAccordion Screenshot ALAccordion Demo

示例

要运行示例项目,先克隆仓库,然后在示例目录中首先运行 pod install

安装

手动安装

将 ALAccordion/Classes 目录中的文件添加到您的项目中

用法

以下显示了 ALAccordion 的基本用法。对于更全面的示例,请参阅示例项目。

ALAccordionController

ALAccordionController 是一个用于管理部分的 UIViewController 子类容器视图。

首先,创建一个继承自 ALAccordionController 的视图控制器。

class ALHomeViewController: ALAccordionController
{
  override func viewDidLoad()
  {
    super.viewDidLoad()
  }
}

折叠标题/页脚

您可以通过设置 headerViewfooterView 属性来为手风琴添加可选的头部和/或尾部。

注意: 手风琴使用头部和尾部视图的内建内容大小进行布局,请确保您正确设置自动布局约束。

override func viewDidLoad()
{
  ...

  // Header
  let header = UILabel()
  header.text = "Accordion Header"
  self.headerView = header

  // Footer
  let footer = UILabel()
  footer.text = "Accordion Footer"
  self.footerView = footer
}

创建分区

要创建您的分区,您必须按它们将显示的顺序将一组视图控制器传递给 ALAccordion。视图控制器可以从故事板解析,也可以通过代码创建。

注意: 视图控制器必须遵守 ALAccordionSectionDelegate 协议(见下文)

override func viewDidLoad()
{
  ...

  let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())

  let section1 = storyboard.instantiateViewControllerWithIdentifier("firstVC") as! Section1ViewController
  let section2 = storyboard.instantiateViewControllerWithIdentifier("secondVC") as! Section2ViewController
  let section3 = storyboard.instantiateViewControllerWithIdentifier("thirdVC") as! Section3TableViewController

  self.setViewControllers(section1, section2, section3)
}

分区视图控制器

您应该为手风琴控制器提供一个新的 UIViewController 实例来表示手风琴中的每个分区。

分区视图控制器必须遵守 ALAccordionSectionDelegate 协议。这意味着它必须提供一个头部视图。同样,像 ALAccordionController 的头部和尾部视图一样,头部视图必须使用自动布局来提供内建内容大小。通过更改头部视图的约束,您可以轻松地更改头部的大小,例如在分区打开时显示更多详细信息。

class ALFirstSectionViewController: UIViewController, ALAccordionSectionDelegate
{
  override func viewDidLoad()
  {
    super.viewDidLoad()
  }

  // MARK: - ALAccordionControllerDelegate

  // Required
  lazy var headerView: UIView =
  {
    let header = UILabel()
    header.text = "Section 1 Header"

    return header
  }()

  // Optional
  func sectionWillOpen(#animated: Bool)
  {

  }

  // Optional
  func sectionWillClose(#animated: Bool)
  {

  }

  // Optional
  func sectionDidOpen()
  {

  }

  // Optional
  func sectionDidClose()
  {

  }
}

注意: 您必须手动指定每个分区如何打开。下面的示例展示了如何在头部视图上使用点击手势来打开分区。

lazy var headerView: UIView =
{
  let header = UILabel()
  header.text = "Section 1 Header"

  // Add a tap gesture recogniser to open the section
  let tapGR = UITapGestureRecognizer(target: self, action: "headerTapped:")
  header.addGestureRecognizer(tapGR)

  return header
}()

func headerTapped(recognizer: UITapGestureRecognizer)
{
  // Get the section for this view controller
  if let sectionIndex = self.accordionController?.sectionIndexForViewController(self)
  {
    // If this section is open, close it - otherwise, open it
    if self.accordionController!.openSectionIndex == sectionIndex
    {
      self.accordionController?.closeSectionAtIndex(sectionIndex, animated: true)
    }
    else
    {
      self.accordionController?.openSectionAtIndex(sectionIndex, animated: true)
    }
  }
}

待办事项、备注 & 局限性

  • 在示例中突出显示头部视图的选中状态
  • 当在分区中嵌入 UITableViewDataSource 时,第一次打开分区时,单元格会有一个不希望出现的效应。为了避免这种情况,请在分区视图控制器的 viewDidLoad 方法中调用 self.view.layoutIfNeeded()
  • 目前,ALAccordion 只能显示有限数量的分区,并且在其关闭状态下,高度不能超过设备的尺寸。我们计划在将来允许可滚动的头部。请随意提交拉取请求!

许可证

ALAccordion 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。

关于

Alliants Limited