1 Star 0 Fork 0

不器 / JXSegmentedView

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

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分类切换滚动视图)

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

  • 指示器逻辑面向协议编程(Protocol Oriented Programming),可以为所欲为的扩展指示器效果;
  • 提供更加全面丰富效果,几乎支持所有主流APP效果;
  • 使用子类化管理cell样式,逻辑更清晰,扩展更简单;
  • 列表支持整个生命周期方法;

Objective-C版本

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

效果预览

指示器效果预览

说明 Gif
Line固定宽度
Line与cell等宽
Line延长
Line延长+偏移
RainbowLine🌈彩虹
DotLine点线
DoubleLine双线
Triangle三角形底部
Triangle三角形顶部
Background椭圆形
Background椭圆形+阴影
Background遮罩有背景
Background遮罩无背景
Background渐变色
(渐变是固定的)
Gradient渐变色
(渐变随着位置变动)
Image底部
Image背景
混合使用

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

Cell样式效果预览

说明 Gif
颜色渐变
文字渐变
大小缩放
大小缩放+字体粗细
大小缩放+点击动画
大小缩放+cell宽度缩放
TitleImage_Top
TitleImage_Left
TitleImage_Bottom
TitleImage_Right
TitleImage_只有图片
TitleOrImage(高仿腾讯视频)
数字
红点
多行富文本
多种cell混用

特殊效果预览

说明 Gif
数据源过少
isItemSpacingAverageEnabled为true
数据源过少
isItemSpacingAverageEnabled为false
SegmentedControl
参考SegmentedControlViewController
导航栏使用
参考NaviSegmentedControlViewController
嵌套使用
参考NestViewController
个人主页(上下左右滚动、header悬浮)
参考PagingViewController
更多样式请点击查看JXPagingView库
数据加载&刷新
参考LoadDataViewController

要求

  • iOS 9.0+
  • Xcode 9+
  • Swift 5.0

安装

手动

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 = ["猴哥", "青蛙王子", "旺财"]
egmentedDataSource.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是对列表视图高度封装的类,具有以下优点:

  • 相对于直接使用UIScrollView自定义,封装度高、代码集中、使用简单;
  • 列表懒加载:当显示某个列表的时候,才进行列表初始化。而不是一次性加载全部列表,性能更优;
  • 可以选用CollectionView作为列表容器,内存管理更加优秀;
  • 支持列表的整个生命周期方法调用;

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样式、列表容器等,如何有序管理好代码成了一个难题。借助于协议、继承、封装类极大的简化了使用难度,而且提高了灵活性,扩展相当容易。

  • 核心主类:JXSegmentedView
  • 数据源&cell样式定制类:遵从JXSegmentedViewDataSource协议的类
  • 指示器类:遵从JXSegmentedIndicatorProtocol协议的UIView
  • 列表容器:官方推荐JXSegmentedListContainerView类,特殊情况可以使用UIScrollView自定义

指示器样式自定义

  • 需要继承JXSegmentedIndicatorProtocol协议,点击参看JXSegmentedIndicatorProtocol
  • 提供了继承JXSegmentedIndicatorProtocol协议的基类JXSegmentedIndicatorBaseView,里面提供了许多基础属性。点击参看JXSegmentedIndicatorBaseView
  • 自定义指示器,请参考已实现的指示器视图,多尝试、多思考,再有问题请提Issue或加入反馈QQ群

dataSource和Cell自定义

  • 需要继承JXSegmentedViewDataSource协议,点击参看JXSegmentedViewDataSource
  • 提供了继承JXSegmentedViewDataSource协议的基类JXSegmentedBaseDataSource,里面提供了许多基础属性。点击参看JXSegmentedBaseDataSource
  • 任何自定义需求,dataSource、cell、itemModel三个都要子类化。即使某个子类cell什么事情都不做。用于维护继承链,以免以后子类化都不知道要继承谁了;
  • dataSource和Cell自定义,请参考已实现的dataSource,多尝试、多思考,再有问题请提Issue或加入反馈QQ群

常用属性说明

常用属性说明文档地址

其他使用注意事项

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

重要版本更新记录

补充

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

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

喜欢就star❤️一下吧

License

JXSegmentedView is released under the MIT license.

MIT License Copyright (c) 2018 暴走的鑫鑫 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

暂无描述 展开 收起
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/Symbol/JXSegmentedView.git
git@gitee.com:Symbol/JXSegmentedView.git
Symbol
JXSegmentedView
JXSegmentedView
master

搜索帮助

14c37bed 8189591 565d56ea 8189591