Home

Awesome

<div align=center><img src="Example/JXSegmentedViewExample/Image/JXSegmentedViewSmall.png" width="467" height="84" /></div>

platform languages cocoapods carthage support

There is an English document here, click to view

A powerful and easy to use segmented view (segmentedcontrol, pagingview, pagerview, pagecontrol, categoryview) (腾讯新闻、今日头条、QQ音乐、网易云音乐、京东、爱奇艺、腾讯视频、淘宝、天猫、简书、微博等所有主流APP分类切换滚动视图)

与其他的同类三方库对比的优点:

Objective-C版本

如果你在找Objective-C版本,请点击查看 JXCategoryView

效果预览

指示器效果预览

说明Gif
Line固定宽度<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Indicator/LineFixedWidth.gif" width="350" height="80">
Line与cell等宽<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Indicator/LineFlexibleWidth.gif" width="350" height="80">
Line延长<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Indicator/LineLengthen.gif" width="350" height="80">
Line延长+偏移<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Indicator/LineLengthenOffset.gif" width="350" height="80">
RainbowLine🌈彩虹<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Indicator/LineRainbow.gif" width="350" height="80">
DotLine点线<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Indicator/LineDot.gif" width="334" height="80">
DoubleLine双线<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Indicator/LineDouble.gif" width="350" height="80">
Triangle三角形底部<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Indicator/Triangle.gif" width="350" height="80">
Triangle三角形顶部<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Indicator/TriangleTop.gif" width="350" height="80">
Background椭圆形<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Indicator/IndicatorBackground.gif" width="350" height="80">
Background椭圆形+阴影<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Indicator/IndicatorBackgroundShadow.gif" width="350" height="80">
Background遮罩有背景<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Indicator/IndicatorBackgroundMask.gif" width="350" height="80">
Background遮罩无背景<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Indicator/IndicatorBackgroundMaskPure.gif" width="350" height="80">
Background渐变色<br>(渐变是固定的)<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Indicator/IndicatorBackgroundGradient.gif" width="350" height="80">
Gradient渐变色<br>(渐变随着位置变动)<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Indicator/IndicatorGradient.gif" width="350" height="80">
Image底部<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Indicator/IndicatorImageBottom.gif" width="350" height="80">
Image背景<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Indicator/IndicatorImageBG.gif" width="350" height="80">
混合使用<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Indicator/IndicatorMixed.gif" width="350" height="80">

以下指示器支持上下位置切换: JXSegmentedIndicatorLineViewJXSegmentedIndicatorRainbowLineViewJXSegmentedIndicatorDotLineViewJXSegmentedIndicatorDoubleLineViewJXSegmentedIndicatorTriangleViewJXSegmentedIndicatorImageView

Cell样式效果预览

说明Gif
颜色渐变<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Cell/ColorGradient.gif" width="350" height="80">
文字渐变<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Cell/TextGradient.gif" width="350" height="80">
大小缩放<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Cell/ZoomOnly.gif" width="350" height="80">
大小缩放+字体粗细<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Cell/ZoomStrokeWidth.gif" width="350" height="80">
大小缩放+点击动画<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Cell/ZoomAnimation.gif" width="350" height="80">
大小缩放+cell宽度缩放<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Cell/ZoomCellWidth.gif" width="350" height="80">
TitleImage_Top<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Cell/TitleImageTop.gif" width="350" height="80">
TitleImage_Left<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Cell/TitleImageLeft.gif" width="350" height="80">
TitleImage_Bottom<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Cell/TitleImageBottom.gif" width="350" height="80">
TitleImage_Right<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Cell/TitleImageRight.gif" width="350" height="80">
TitleImage_只有图片<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Cell/TitleImageOnlyImage.gif" width="350" height="80">
TitleOrImage(高仿腾讯视频)<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Cell/TitleOrImage.gif" width="350" height="80">
数字<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Cell/Number.gif" width="350" height="80">
红点<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Cell/CellDot.gif" width="350" height="80">
多行富文本<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Cell/TitleAttributed.gif" width="350" height="80">
多种cell混用<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Cell/MixedCell.gif" width="350" height="80">

特殊效果预览

说明Gif
数据源过少<br/> isItemSpacingAverageEnabled为true<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Special/ItemAveTrue.gif" width="350" height="80">
数据源过少<br/> isItemSpacingAverageEnabled为false<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Special/ItemAveFalse.gif" width="350" height="80">
SegmentedControl<br/>参考SegmentedControlViewController<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Special/SegmentedControl.gif" width="350" height="80">
导航栏使用<br/>参考NaviSegmentedControlViewController<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Special/NavigationBar.gif" width="350" height="80">
嵌套使用<br/>参考NestViewController<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Special/Nest.gif" width="350" height="200">
个人主页(上下左右滚动、header悬浮)<br/>参考PagingViewController<br/> 更多样式请点击查看JXPagingView库<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Special/Personal.gif" width="350" height="567">
数据加载&刷新<br/>参考LoadDataViewController<img src="https://github.com/pujiaxin33/JXExampleImages/blob/master/JXSegmentedView/Special/LoadData.gif" width="350" height="200">

要求

安装

手动

Clone代码,把Sources文件夹拖入项目,就可以使用了;

CocoaPods

target '<Your Target Name>' do
    pod 'JXSegmentedView'
end

先执行pod repo update,再执行pod install

Carthage

在cartfile文件添加:

github "pujiaxin33/JXSegmentedView"

然后执行carthage update --platform iOS 即可

Swift Package Manager

1.在Package.swift文件添加如下代码:

dependencies: [
  .package(url: "https://github.com/pujiaxin33/JXSegmentedView.git", from: "1.2.1")
]

2.使用命令行构建:

$ swift build

使用

JXSegmentedView使用示例

1.初始化JXSegmentedView

segmentedView = JXSegmentedView()
segmentedView.delegate = self
view.addSubview(self.segmentedView)

2.初始化dataSource

dataSouce类型为JXSegmentedViewDataSource协议。使用单独的类实现JXSegmentedViewDataSource协议,实现代码隔离。选择不同的类赋值给dataSource,就可以控制JXSegmentedView显示效果,实现插件化。比如选择JXSegmentedTitleImageDataSource类作为dataSource就选择了文字图片的显示效果;选择JXSegmentedNumberDataSource类作为dataSource就选择了文字加数字的显示效果;

//segmentedDataSource一定要通过属性强持有,不然会被释放掉
segmentedDataSource = JXSegmentedTitleDataSource()
//配置数据源相关配置属性
segmentedDataSource.titles = ["猴哥", "青蛙王子", "旺财"]
segmentedDataSource.isTitleColorGradientEnabled = true
//关联dataSource
segmentedView.dataSource = self.segmentedDataSource

3.初始化指示器indicator

let indicator = JXSegmentedIndicatorLineView()
indicator.indicatorWidth = 20
segmentedView.indicators = [indicator]

4.可选实现JXSegmentedViewDelegate代理

//点击选中或者滚动选中都会调用该方法。适用于只关心选中事件,而不关心具体是点击还是滚动选中的情况。
func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {}
// 点击选中的情况才会调用该方法
func segmentedView(_ segmentedView: JXSegmentedView, didClickSelectedItemAt index: Int) {}
// 滚动选中的情况才会调用该方法
func segmentedView(_ segmentedView: JXSegmentedView, didScrollSelectedItemAt index: Int) {}
// 正在滚动中的回调
func segmentedView(_ segmentedView: JXSegmentedView, scrollingFrom leftIndex: Int, to rightIndex: Int, percent: CGFloat) {}

contentScrollView列表容器使用示例

直接使用UIScrollView自定义使用示例

因为代码比较分散,而且代码量也比较多,所有不推荐使用该方法。要正确使用需要注意的地方比较多,尤其对于刚接触iOS的同学来说不太友好。

不直接贴代码了,具体点击LoadDataCustomViewController查看源代码了解。

作为替代,官方使用&强烈推荐使用下面这种方式👇👇👇。

配合JXSegmentedListContainerView封装类使用示例

JXSegmentedListContainerView是对列表视图高度封装的类,具有以下优点:

1.初始化JXSegmentedListContainerView

listContainerView = JXSegmentedListContainerView(dataSource: self)
view.addSubview(self.listContainerView)
//关联listContainer
segmentedView.listContainer = listContainerView

2.实现JXSegmentedListContainerViewDataSource代理方法

//返回列表的数量
func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
    return segmentedDataSource.titles.count
}
//返回遵从`JXSegmentedListContainerViewListDelegate`协议的实例
func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
    return ListBaseViewController()
}

3.列表实现JXSegmentedListContainerViewListDelegate代理方法

不管列表是UIView还是UIViewController都可以,提高使用灵活性,更便于现有的业务接入。

/// 如果列表是VC,就返回VC.view
/// 如果列表是View,就返回View自己
/// - Returns: 返回列表视图
func listView() -> UIView {
    return view
}
func listWillAppear() {}
func listDidAppear() {}
func listDidDisappear() {}
func listDidDisappear() {}

具体点击LoadDataViewController查看源代码了解

使用总结

因为JXSegmentedView本身支持许多特性:指示器、cell样式、列表容器等,如何有序管理好代码成了一个难题。借助于协议、继承、封装类极大的简化了使用难度,而且提高了灵活性,扩展相当容易。

指示器样式自定义

dataSource和Cell自定义

常用属性说明

常用属性说明文档地址

其他使用注意事项

其他使用注意事项文档总地址

重要版本更新记录

补充

如果刚开始使用JXSegmentedView,当开发过程中需要支持某种特性时,请务必先搜索使用文档或者源代码。确认是否已经实现支持了想要的特性。请别不要文档和源代码都没有看,就直接提问,这对于大家都是一种时间浪费。如果没有支持想要的特性,欢迎提Issue讨论,或者自己实现提一个PullRequest。

该仓库保持及时更新,对于主流新的分类选择效果会第一时间支持。使用过程中,有任何建议或问题,可以通过以下方式联系我:</br> 邮箱:317437084@qq.com </br> QQ群: 112440276

<img src="https://note.youdao.com/yws/public/resource/c6fa96a65e424afcf7f6304ddf5c283a/xmlnote/8dc821d271c35845acff3f853f434bce/3913" width="300" height="411">

喜欢就star❤️一下吧

License

JXSegmentedView is released under the MIT license.