TSegmentedView
功能
- 与
Objective-C
和Swift(3/4/5)
完全兼容 - 支持用户滚动和点击标签
- 支持滑动回退(任何标签)
- 支持
Frame
和Autolayout
,可以使用Masonry
/SnapKit
/NSLayoutConstraint
布局视图 - 支持普通
UIView
(以及非滑动的UIView
子类View
),UIScrollView
,UITableView
- 支持
UITableView
添加tableHeaderView
- 支持
UITableView
添加分区头部视图,显示不会出现悬停位置不正确的问题 - 支持自定义
SegmentedControlView
(点击标签)样式,可以设置自己的动画,设置自己的高度等
支持UIView
支持UIScrollView
支持UITableView添加tableHeaderView
支持UITableView添加分区头部
TSegmentedView
为何编写现在有很多类似的框架,但还是写了一个,主要是因为互联网上大部分框架都是用来写SegmentedControlView
(即标签样式),另一个重要的点是,我试过很多框架,发现当UITableView
的tableHeaderView
有问题时,一旦有分区头部视图,悬停位置就出现错误,所以我就写了这个...
安装
源文件
如果你的项目使用Swift 3/3.1
并且不使用Xcode 9
,请下载位于Source
目录下的TSegmentedControlView.swift
,TSegmentedView.swift
,TSVExtension.swift
,并将它们放入你的项目中,不需要其他配置。
如果你的项目使用Xcode 9
,建议使用CocoaPods
或Carthage
。
使用 CocoaPods
CocoaPods
是一款 Cocoa 项目的依赖管理工具。您可以使用以下命令进行安装:
$ gem install cocoapods
要将 TSegmentedView
集成到您的 Xcode 项目中,使用 CocoaPods,在您的 Podfile
中指定:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
target '<Your Target Name>' do
pod 'TSegmentedView'
end
然后,执行以下命令:
$ pod install
使用 Carthage
Carthage
是一款协作依赖管理工具,可编译依赖项并为您提供二进制框架。
您可以使用以下命令用 Homebrew
安装 Carthage:
$ brew update
$ brew install carthage
要将 TSegmentedView
集成到您的 Xcode 项目中,使用 Carthage,在您的 Cartfile
中指定:
github "tobedefined/TSegmentedView" ~> 1.1.0
运行 carthage update
命令来构建框架,并将构建的 TSegmentedView.framework
拖放到您的 Xcode 项目中。
如何使用
- swift
import TSegmentedView
- Objective-C
#import <TSegmentedView/TSegmentedView-Swift.h>
您可以通过以下具体示例了解其具体用法:
TSegmentedViewDelegate
适配协议:func segmentedViewTitles(in segmentedView: TSegmentedView) -> [String]
func segmentedView(_ view: TSegmentedView, viewForIndex index: Int) -> UIView
- 第一个函数是对
TSegmentedView
无标题标签的赋予,数组计数是标签数量 - 第二个函数是为每个标签提供一个视图
可选协议函数
// 1
@objc optional func segmentedView(_ view: TSegmentedView, didShow index: Int) -> Void
// 2.1 (Swift 3.2/4)
@objc optional func segmentedViewSegmentedControlView(in segmentedView: TSegmentedView) -> (UIView & TSegmentedControlProtocol)
// 2.2 (Swift 3/3.1)
@objc optional func segmentedViewSegmentedControlView(in segmentedView: TSegmentedView) -> UIView
// 3
// default is 0
@objc optional func segmentedViewFirstStartSelectIndex(in segmentedView: TSegmentedView) -> Int
// 4
// default is nil
@objc optional func segmentedViewHeaderView(in segmentedView: TSegmentedView) -> UIView
// 5
// default is segmentedViewHeaderView height
@objc optional func segmentedViewHeaderMaxHeight(in segmentedView: TSegmentedView) -> CGFloat
// 6
// default is segmentedViewHeaderView height
@objc optional func segmentedViewHeaderMinHeight(in segmentedView: TSegmentedView) -> CGFloat
// 7
// when scroll top or bottom, change the titles view height , will run this method
@objc optional func segmentedView(_ view: TSegmentedView, didChangeHeaderHeightTo height: CGFloat) -> Void
-
可选函数使用
- 在索引对应的视图将调用此函数,每次选择或滚动到该索引时都会调用
- 该函数返回定义的
SegmentedControlView
,它需要是符合TSegmentedControlProtocol
协议的UIView
- 函数返回创建
TSegmentedView
时选择的哪个标签(默认选择第一个标签 -> 索引 = 0) - 返回头部视图(默认为 nil)
- 设置头部视图的最大高度(默认为头部视图的 frame 高度尺寸)
- 设置标题的最小高度(默认值与最大高度相同)
- 当标题高度变化时,此函数会被调用,允许根据新的高度进行一些动画操作
TSegmentedControlProtocol
关于 您可以在 TSegmentedView.swift
中看到此协议的定义
@objc protocol TSegmentedControlProtocol: class {
func reloadData(with titles: [String]) -> Void
func userScrollExtent(_ extent: CGFloat) -> Void
func setAction(_ actionBlock: ((_ index: Int) -> Void)?) -> Void
}
-
为什么要定义
TSegmentedControlProtocol
TSegmentedView
允许用户自定义SegmentedControlView
,而不是必须使用TSegmentedControlView
-
如何自定义
SegmentedControlView
首先创建的视图必须是
UIView
的子类,然后符合TSegmentedControlProtocol
协议并实现这三个方法 -
func reloadData (with titles: [String]) -> Void
在
TSegmentedView
的reloadData
调用回调时,此方法需要更新以实现对应标签的创建显示和其他操作,其中titles
是TSegmentedControlView
代理方法返回的数组 -
func userScrollExtent (_ extent: CGFloat) -> Void
在
TSegmentedView
滑动(用户手动滑动)时,此方法会被回调,需要更新对应标签的视图显示样式或自定义动画,其中extent
的值为当前滑动的比例。例如,有三个标签,范围为0.0 ~ 2.0
-
func setAction(_ actionBlock: ((_ index: Int) -> Void)?) -> Void
此方法需要您保存
actionBlock
并在点击标签时调用actionBlock
,然后,将滚动到对应标签的视图。 (最初考虑在协议中定义一个actionBlock
变量,以便与Objective-C
兼容,因此将其定义为函数。)