UIView+SGFrame.h
#import
@interface UIView (SGFrame)
@property (nonatomic, assign) CGFloat SG_x;
@property (nonatomic, assign) CGFloat SG_y;
@property (nonatomic, assign) CGFloat SG_width;
@property (nonatomic, assign) CGFloat SG_height;
@property (nonatomic, assign) CGFloat SG_centerX;
@property (nonatomic, assign) CGFloat SG_centerY;
@property (nonatomic, assign) CGPoint SG_origin;
@property (nonatomic, assign) CGSize SG_size;
/// 从 XIB 中加载视图
+ (instancetype)SG_loadViewFromXib;
@end
UIView+SGFrame.m
#import "UIView+SGFrame.h"
@implementation UIView (SGFrame)
- (void)setSG_x:(CGFloat)SG_x {
CGRect frame = self.frame;
frame.origin.x = SG_x;
self.frame = frame;
}
- (CGFloat)SG_x {
return self.frame.origin.x;
}
- (void)setSG_y:(CGFloat)SG_y {
CGRect frame = self.frame;
frame.origin.y = SG_y;
self.frame = frame;
}
- (CGFloat)SG_y {
return self.frame.origin.y;
}
- (void)setSG_width:(CGFloat)SG_width {
CGRect frame = self.frame;
frame.size.width = SG_width;
self.frame = frame;
}
- (CGFloat)SG_width {
return self.frame.size.width;
}
- (void)setSG_height:(CGFloat)SG_height {
CGRect frame = self.frame;
frame.size.height = SG_height;
self.frame = frame;
}
- (CGFloat)SG_height {
return self.frame.size.height;
}
- (CGFloat)SG_centerX {
return self.center.x;
}
- (void)setSG_centerX:(CGFloat)SG_centerX {
CGPoint center = self.center;
center.x = SG_centerX;
self.center = center;
}
- (CGFloat)SG_centerY {
return self.center.y;
}
- (void)setSG_centerY:(CGFloat)SG_centerY {
CGPoint center = self.center;
center.y = SG_centerY;
self.center = center;
}
- (void)setSG_origin:(CGPoint)SG_origin {
CGRect frame = self.frame;
frame.origin = SG_origin;
self.frame = frame;
}
- (CGPoint)SG_origin {
return self.frame.origin;
}
- (void)setSG_size:(CGSize)SG_size {
CGRect frame = self.frame;
frame.size = SG_size;
self.frame = frame;
}
- (CGSize)SG_size {
return self.frame.size;
}
/// 从 XIB 中加载视图
+ (instancetype)SG_loadViewFromXib {
return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil].lastObject;
}
@end
SGPageContentScrollView.h
#import<UIKit/UIkit.h>
@class SGPageContentScrollView;
@protocol SGPageContentScrollViewDelegate<NSObject>
@optional
/** * 联动 SGPageTitleView 的方法 * * @param pageContentScrollView SGPageContentScrollView * @param progress SGPageContentScrollView 内部视图滚动时的偏移量 * @param originalIndex 原始视图所在下标 * @param targetIndex 目标视图所在下标 */
- (void)pageContentScrollView:(SGPageContentScrollView *)pageContentScrollView progress:(CGFloat)progress originalIndex:(NSInteger)originalIndex targetIndex:(NSInteger)targetIndex;
/** * 给 SGPageContentScrollView 所在控制器提供的方法(根据偏移量来处理返回手势的问题) * * @param pageContentScrollView SGPageContentScrollView * @param offsetX SGPageContentScrollView 内部视图的偏移量 */
- (void)pageContentScrollView:(SGPageContentScrollView *)pageContentScrollView offsetX:(CGFloat)offsetX;
@end
@interface SGPageContentScrollView : UIView
/** * 对象方法创建 SGPageContentScrollView * * @param frame frame * @param parentVC 当前控制器 * @param childVCs 子控制器个数 */
- (instancetype)initWithFrame:(CGRect)frame parentVC:(UIViewController *)parentVC childVCs:(NSArray *)childVCs;
/** * 类方法创建 SGPageContentScrollView * * @param frame frame * @param parentVC 当前控制器 * @param childVCs 子控制器个数 */
+ (instancetype)pageContentScrollViewWithFrame:(CGRect)frame parentVC:(UIViewController *)parentVC childVCs:(NSArray *)childVCs;
/** SGPageContentScrollViewDelegate */
@property (nonatomic, weak) id delegatePageContentScrollView;
/** 是否需要滚动 SGPageContentScrollView 默认为 YES;设为 NO 时,不必设置 SGPageContentScrollView 的代理及代理方法 */
@property (nonatomic, assign) BOOL isScrollEnabled;
/** 给外界提供的方法,获取 SGPageTitleView 选中按钮的下标 */
- (void)setPageCententScrollViewCurrentIndex:(NSInteger)currentIndex;
@end
SGPageContentScrollView.m
#import "SGPageContentScrollView.h"
#import "UIView+SGFrame.h"
@interface SGPageContentScrollView ()// 外界父控制器
@property (nonatomic, weak) UIViewController *parentViewController;
/// 存储子控制器
@property (nonatomic, strong) NSArray *childViewControllers;
/// scrollView
@property (nonatomic, strong) UIScrollView *scrollView;
/// 记录刚开始时的偏移量
@property (nonatomic, assign) NSInteger startOffsetX;
/// 标记按钮是否点击
@property (nonatomic, assign) BOOL isClickBtn;
/// 标记是否默认加载第一个子视图
@property (nonatomic, assign) BOOL isFirstViewLoaded;
@end
@implementation SGPageContentScrollView
- (instancetype)initWithFrame:(CGRect)frame parentVC:(UIViewController *)parentVC childVCs:(NSArray *)childVCs {
if (self = [super initWithFrame:frame]) {
if (parentVC == nil) {
@throw [NSException exceptionWithName:@"SGPagingView" reason:@"SGPageContentScrollView 所在控制器必须设置" userInfo:nil];
}
self.parentViewController = parentVC;
if (childVCs == nil) {
@throw [NSException exceptionWithName:@"SGPagingView" reason:@"SGPageContentScrollView 子控制器必须设置" userInfo:nil];
}
self.childViewControllers = childVCs;
[self initialization];
[self setupSubviews];
}
return self;
}
+ (instancetype)pageContentScrollViewWithFrame:(CGRect)frame parentVC:(UIViewController *)parentVC childVCs:(NSArray *)childVCs {
return [[self alloc] initWithFrame:frame parentVC:parentVC childVCs:childVCs];
}
- (void)initialization {
self.isClickBtn = YES;
self.startOffsetX = 0;
self.isFirstViewLoaded = YES;
}
- (void)setupSubviews {
// 0、处理偏移量
UIView *tempView = [[UIView alloc] initWithFrame:CGRectZero];
[self addSubview:tempView];
// 1、添加 scrollView
[self addSubview:self.scrollView];
}
- (UIScrollView *)scrollView {
if (!_scrollView) {
_scrollView = [[UIScrollView alloc] init];
_scrollView.frame = self.bounds;
_scrollView.bounces = NO;
_scrollView.delegate = self;
_scrollView.pagingEnabled = YES;
_scrollView.showsVerticalScrollIndicator = NO;
_scrollView.showsHorizontalScrollIndicator = NO;
CGFloat contentWidth = self.childViewControllers.count * _scrollView.SG_width;
_scrollView.contentSize = CGSizeMake(contentWidth, 0);
}
return _scrollView;
}
#pragma mark - - - UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
self.isClickBtn = NO;
self.startOffsetX = scrollView.contentOffset.x;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
CGFloat offsetX = scrollView.contentOffset.x;
// 1、pageContentScrollView:offsetX:
if (self.delegatePageContentScrollView && [self.delegatePageContentScrollView respondsToSelector:@selector(pageContentScrollView:offsetX:)]) {
[self.delegatePageContentScrollView pageContentScrollView:self offsetX:offsetX];
}
NSInteger index = offsetX / scrollView.frame.size.width;
UIViewController *childVC = self.childViewControllers[index];
// 2、判断控制器的view有没有加载过,如果已经加载过,就不需要加载
if (childVC.isViewLoaded) return;
[self.scrollView addSubview:childVC.view];
[self.parentViewController addChildViewController:childVC];
childVC.view.frame = CGRectMake(offsetX, 0, self.SG_width, self.SG_height);
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (self.isClickBtn == YES) {
[self scrollViewDidEndDecelerating:scrollView];
return;
}
// 1、定义获取需要的数据
CGFloat progress = 0;
NSInteger originalIndex = 0;
NSInteger targetIndex = 0;
// 2、判断是左滑还是右滑
CGFloat currentOffsetX = scrollView.contentOffset.x;
CGFloat scrollViewW = scrollView.bounds.size.width;
if (currentOffsetX > self.startOffsetX) { // 左滑
// 1、计算 progress
progress = currentOffsetX / scrollViewW - floor(currentOffsetX / scrollViewW);
// 2、计算 originalIndex
originalIndex = currentOffsetX / scrollViewW;
// 3、计算 targetIndex
targetIndex = originalIndex + 1;
if (targetIndex >= self.childViewControllers.count) {
progress = 1;
targetIndex = self.childViewControllers.count - 1;
}
// 4、如果完全划过去
if (currentOffsetX - self.startOffsetX == scrollViewW) {
progress = 1;
targetIndex = originalIndex;
}
} else { // 右滑
// 1、计算 progress
progress = 1 - (currentOffsetX / scrollViewW - floor(currentOffsetX / scrollViewW));
// 2、计算 targetIndex
targetIndex = currentOffsetX / scrollViewW;
// 3、计算 originalIndex
originalIndex = targetIndex + 1;
if (originalIndex >= self.childViewControllers.count) {
originalIndex = self.childViewControllers.count - 1;
}
}
// 3、pageContentViewDelegare; 将 progress/sourceIndex/targetIndex 传递给 SGPageTitleView
if (self.delegatePageContentScrollView && [self.delegatePageContentScrollView respondsToSelector:@selector(pageContentScrollView:progress:originalIndex:targetIndex:)]) {
[self.delegatePageContentScrollView pageContentScrollView:self progress:progress originalIndex:originalIndex targetIndex:targetIndex];
}
}
#pragma mark - - - 给外界提供的方法,获取 SGPageTitleView 选中按钮的下标
- (void)setPageCententScrollViewCurrentIndex:(NSInteger)currentIndex {
self.isClickBtn = YES;
CGFloat offsetX = currentIndex * self.SG_width;
if (self.isFirstViewLoaded && currentIndex == 0) {
self.isFirstViewLoaded = NO;
// 2、默认选中第一个子控制器;self.scrollView.contentOffset = 0
UIViewController *childVC = self.childViewControllers[0];
if (childVC.isViewLoaded) return;
[self.scrollView addSubview:childVC.view];
[self.parentViewController addChildViewController:childVC];
childVC.view.frame = CGRectMake(offsetX, 0, self.SG_width, self.SG_height);
}
// 1、处理内容偏移
self.scrollView.contentOffset = CGPointMake(offsetX, 0);
// 2、pageContentScrollView:offsetX:
if (self.delegatePageContentScrollView && [self.delegatePageContentScrollView respondsToSelector:@selector(pageContentScrollView:offsetX:)]) {
[self.delegatePageContentScrollView pageContentScrollView:self offsetX:offsetX];
}
}
#pragma mark - - - set
- (void)setIsScrollEnabled:(BOOL)isScrollEnabled {
_isScrollEnabled = isScrollEnabled;
if (isScrollEnabled) {
} else {
_scrollView.scrollEnabled = NO;
}
}
@end
SGPageContentView.h
#import<UIKit/UIKit.h>
@class SGPageContentView;
@protocol SGPageContentViewDelegate<NSObject>
@optional
/** * 联动 SGPageTitleView 的方法 * * @param pageContentView SGPageContentView * @param progress SGPageContentView 内部视图滚动时的偏移量 * @param originalIndex 原始视图所在下标 * @param targetIndex 目标视图所在下标 */
- (void)pageContentView:(SGPageContentView *)pageContentView progress:(CGFloat)progress originalIndex:(NSInteger)originalIndex targetIndex:(NSInteger)targetIndex;
/** * 给 SGPageContentView 所在控制器提供的方法(根据偏移量来处理返回手势的问题) * * @param pageContentView SGPageContentView * @param offsetX SGPageContentView 内部视图的偏移量 */
- (void)pageContentView:(SGPageContentView *)pageContentView offsetX:(CGFloat)offsetX;
@end@interface SGPageContentView : UIView
/** * 对象方法创建 SGPageContentView * * @param frame frame * @param parentVC 当前控制器 * @param childVCs 子控制器个数 */
- (instancetype)initWithFrame:(CGRect)frame parentVC:(UIViewController *)parentVC childVCs:(NSArray *)childVCs;
/** * 类方法创建 SGPageContentView * * @param frame frame * @param parentVC 当前控制器 * @param childVCs 子控制器个数 */
+ (instancetype)pageContentViewWithFrame:(CGRect)frame parentVC:(UIViewController *)parentVC childVCs:(NSArray *)childVCs;
/** SGPageContentViewDelegate */
@property (nonatomic, weak) id delegatePageContentView;
/** 是否需要滚动 SGPageContentView 默认为 YES;设为 NO 时,不必设置 SGPageContentView 的代理及代理方法 */
@property (nonatomic, assign) BOOL isScrollEnabled;
/** 给外界提供的方法,获取 SGPageTitleView 选中按钮的下标 */
- (void)setPageCententViewCurrentIndex:(NSInteger)currentIndex;
@end
SGPageContentView.m
#import "SGPageContentView.h"
#import "UIView+SGFrame.h"
@interface SGPageContentView ()/// 外界父控制器
@property (nonatomic, weak) UIViewController *parentViewController;
/// 存储子控制器
@property (nonatomic, strong) NSArray *childViewControllers;
/// collectionView
@property (nonatomic, strong) UICollectionView *collectionView;
/// 记录刚开始时的偏移量
@property (nonatomic, assign) NSInteger startOffsetX;
/// 标记按钮是否点击
@property (nonatomic, assign) BOOL isClickBtn;
@end
@implementation SGPageContentView
- (instancetype)initWithFrame:(CGRect)frame parentVC:(UIViewController *)parentVC childVCs:(NSArray *)childVCs {
if (self = [super initWithFrame:frame]) {
if (parentVC == nil) {
@throw [NSException exceptionWithName:@"SGPagingView" reason:@"SGPageContentView 所在控制器必须设置" userInfo:nil];
}
self.parentViewController = parentVC;
if (childVCs == nil) {
@throw [NSException exceptionWithName:@"SGPagingView" reason:@"SGPageContentView 子控制器必须设置" userInfo:nil];
}
self.childViewControllers = childVCs;
[self initialization];
[self setupSubviews];
}
return self;
}
+ (instancetype)pageContentViewWithFrame:(CGRect)frame parentVC:(UIViewController *)parentVC childVCs:(NSArray *)childVCs {
return [[self alloc] initWithFrame:frame parentVC:parentVC childVCs:childVCs];
}
- (void)initialization {
self.isClickBtn = NO;
self.startOffsetX = 0;
}
- (void)setupSubviews {
// 0、处理偏移量
UIView *tempView = [[UIView alloc] initWithFrame:CGRectZero];
[self addSubview:tempView];
// 1、将所有的子控制器添加父控制器中
for (UIViewController *childVC in self.childViewControllers) {
[self.parentViewController addChildViewController:childVC];
}
// 2、添加UICollectionView, 用于在Cell中存放控制器的View
[self addSubview:self.collectionView];
}
- (UICollectionView *)collectionView {
if (!_collectionView) {
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.itemSize = self.bounds.size;
flowLayout.minimumLineSpacing = 0;
flowLayout.minimumInteritemSpacing = 0;
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
CGFloat collectionViewX = 0;
CGFloat collectionViewY = 0;
CGFloat collectionViewW = self.SG_width;
CGFloat collectionViewH = self.SG_height;
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(collectionViewX, collectionViewY, collectionViewW, collectionViewH) collectionViewLayout:flowLayout];
_collectionView.showsVerticalScrollIndicator = NO;
_collectionView.showsHorizontalScrollIndicator = NO;
_collectionView.pagingEnabled = YES;
_collectionView.bounces = NO;
_collectionView.backgroundColor = [UIColor whiteColor];
_collectionView.delegate = self;
_collectionView.dataSource = self;
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}
return _collectionView;
}
#pragma mark - - - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.childViewControllers.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
[cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
// 设置内容
UIViewController *childVC = self.childViewControllers[indexPath.item];
childVC.view.frame = cell.contentView.frame;
[cell.contentView addSubview:childVC.view];
return cell;
}
#pragma mark - - - UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
self.isClickBtn = NO;
self.startOffsetX = scrollView.contentOffset.x;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
CGFloat offsetX = scrollView.contentOffset.x;
// pageContentView:offsetX:
if (self.delegatePageContentView && [self.delegatePageContentView respondsToSelector:@selector(pageContentView:offsetX:)]) {
[self.delegatePageContentView pageContentView:self offsetX:offsetX];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (self.isClickBtn == YES) {
return;
}
// 1、定义获取需要的数据
CGFloat progress = 0;
NSInteger originalIndex = 0;
NSInteger targetIndex = 0;
// 2、判断是左滑还是右滑
CGFloat currentOffsetX = scrollView.contentOffset.x;
CGFloat scrollViewW = scrollView.bounds.size.width;
if (currentOffsetX > self.startOffsetX) { // 左滑
// 1、计算 progress
progress = currentOffsetX / scrollViewW - floor(currentOffsetX / scrollViewW);
// 2、计算 originalIndex
originalIndex = currentOffsetX / scrollViewW;
// 3、计算 targetIndex
targetIndex = originalIndex + 1;
if (targetIndex >= self.childViewControllers.count) {
progress = 1;
targetIndex = originalIndex;
}
// 4、如果完全划过去
if (currentOffsetX - self.startOffsetX == scrollViewW) {
progress = 1;
targetIndex = originalIndex;
}
} else { // 右滑
// 1、计算 progress
progress = 1 - (currentOffsetX / scrollViewW - floor(currentOffsetX / scrollViewW));
// 2、计算 targetIndex
targetIndex = currentOffsetX / scrollViewW;
// 3、计算 originalIndex
originalIndex = targetIndex + 1;
if (originalIndex >= self.childViewControllers.count) {
originalIndex = self.childViewControllers.count - 1;
}
}
// 3、pageContentViewDelegare; 将 progress/sourceIndex/targetIndex 传递给 SGPageTitleView
if (self.delegatePageContentView && [self.delegatePageContentView respondsToSelector:@selector(pageContentView:progress:originalIndex:targetIndex:)]) {
[self.delegatePageContentView pageContentView:self progress:progress originalIndex:originalIndex targetIndex:targetIndex];
}
}
#pragma mark - - - 给外界提供的方法,获取 SGPageTitleView 选中按钮的下标
- (void)setPageCententViewCurrentIndex:(NSInteger)currentIndex {
self.isClickBtn = YES;
CGFloat offsetX = currentIndex * self.collectionView.SG_width;
// 1、处理内容偏移
self.collectionView.contentOffset = CGPointMake(offsetX, 0);
// 2、pageContentView:offsetX:
if (self.delegatePageContentView && [self.delegatePageContentView respondsToSelector:@selector(pageContentView:offsetX:)]) {
[self.delegatePageContentView pageContentView:self offsetX:offsetX];
}
}
#pragma mark - - - set
- (void)setIsScrollEnabled:(BOOL)isScrollEnabled {
_isScrollEnabled = isScrollEnabled;
if (isScrollEnabled) {
} else {
_collectionView.scrollEnabled = NO;
}
}
@end
SGPageTitleView.h
#import<UIKit/UIKit.h>
@class SGPageTitleViewConfigure, SGPageTitleView;
@protocol SGPageTitleViewDelegate<NSObject>
/** * 联动 pageContent 的方法 * * @param pageTitleView SGPageTitleView * @param selectedIndex 选中按钮的下标 */
- (void)pageTitleView:(SGPageTitleView *)pageTitleView selectedIndex:(NSInteger)selectedIndex;
@end@interface SGPageTitleView : UIView
/** * 对象方法创建 SGPageTitleView * * @param frame frame * @param delegate delegate * @param titleNames 标题数组 * @param configure SGPageTitleView 信息配置 */
- (instancetype)initWithFrame:(CGRect)frame delegate:(id)delegate titleNames:(NSArray *)titleNames configure:(SGPageTitleViewConfigure *)configure;
/** * 类方法创建 SGPageTitleView * * @param frame frame * @param delegate delegate * @param titleNames 标题数组 * @param configure SGPageTitleView 信息配置 */
+ (instancetype)pageTitleViewWithFrame:(CGRect)frame delegate:(id)delegate titleNames:(NSArray *)titleNames configure:(SGPageTitleViewConfigure *)configure;
/** SGPageTitleView 是否需要弹性效果,默认为 YES */
@property (nonatomic, assign) BOOL isNeedBounces;
/** 选中标题按钮下标,默认为 0 */
@property (nonatomic, assign) NSInteger selectedIndex;
/** 重置选中标题按钮下标(用于子控制器内的点击事件改变标题的选中下标)*/
@property (nonatomic, assign) NSInteger resetSelectedIndex;
/** 是否让标题按钮文字有渐变效果,默认为 YES */
@property (nonatomic, assign) BOOL isTitleGradientEffect;
/** 是否开启标题按钮文字缩放效果,默认为 NO */
@property (nonatomic, assign) BOOL isOpenTitleTextZoom;
/** 标题文字缩放比,默认为 0.1f,取值范围 0 ~ 0.3f */
@property (nonatomic, assign) CGFloat titleTextScaling;
/** 是否显示指示器,默认为 YES */
@property (nonatomic, assign) BOOL isShowIndicator;
/** 是否显示底部分割线,默认为 YES */
@property (nonatomic, assign) BOOL isShowBottomSeparator;
/** 给外界提供的方法,获取 SGPageContentView 的 progress/originalIndex/targetIndex, 必须实现 */
- (void)setPageTitleViewWithProgress:(CGFloat)progress originalIndex:(NSInteger)originalIndex targetIndex:(NSInteger)targetIndex;
/** 根据下标重置标题文字(index 标题所对应的下标值、title 新的标题名)*/
- (void)resetTitleWithIndex:(NSInteger)index newTitle:(NSString *)title;
@end
SGPageTitleView.m
#import "SGPageTitleView.h"
#import "UIView+SGFrame.h"
#import "SGPageTitleViewConfigure.h"
#define SGPageTitleViewWidth self.frame.size.width
#define SGPageTitleViewHeight self.frame.size.height
#pragma mark - - - SGPageTitleButton
@interface SGPageTitleButton : UIButton
@end
@implementation SGPageTitleButton
- (void)setHighlighted:(BOOL)highlighted {
}
@end
#pragma mark - - - SGPageTitleView
@interface SGPageTitleView ()
/// SGPageTitleViewDelegate
@property (nonatomic, weak) iddelegatePageTitleView;
/// SGPageTitleViewConfigure
@property (nonatomic, strong) SGPageTitleViewConfigure *configure;
/// scrollView
@property (nonatomic, strong) UIScrollView *scrollView;
/// 指示器
@property (nonatomic, strong) UIView *indicatorView;
/// 底部分割线
@property (nonatomic, strong) UIView *bottomSeparator;
/// 保存外界传递过来的标题数组
@property (nonatomic, strong) NSArray *titleArr;
/// 存储标题按钮的数组
@property (nonatomic, strong) NSMutableArray *btnMArr;
/// tempBtn
@property (nonatomic, strong) UIButton *tempBtn;
/// 记录所有按钮文字宽度
@property (nonatomic, assign) CGFloat allBtnTextWidth;
// 记录所有子控件的宽度
@property (nonatomic, assign) CGFloat allBtnWidth;
/// 标记按钮下标
@property (nonatomic, assign) NSInteger signBtnIndex;
/// 开始颜色, 取值范围 0~1
@property (nonatomic, assign) CGFloat startR;
@property (nonatomic, assign) CGFloat startG;
@property (nonatomic, assign) CGFloat startB;
/// 完成颜色, 取值范围 0~1
@property (nonatomic, assign) CGFloat endR;
@property (nonatomic, assign) CGFloat endG;
@property (nonatomic, assign) CGFloat endB;
@end
@implementation SGPageTitleView
- (instancetype)initWithFrame:(CGRect)frame delegate:(id)delegate titleNames:(NSArray *)titleNames configure:(SGPageTitleViewConfigure *)configure {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.77];
if (delegate == nil) {
@throw [NSException exceptionWithName:@"SGPagingView" reason:@"SGPageTitleView 的代理方法必须设置" userInfo:nil];
}
self.delegatePageTitleView = delegate;
if (titleNames == nil) {
@throw [NSException exceptionWithName:@"SGPagingView" reason:@"SGPageTitleView 的标题数组必须设置" userInfo:nil];
}
self.titleArr = titleNames;
if (configure == nil) {
@throw [NSException exceptionWithName:@"SGPagingView" reason:@"SGPageTitleView 的配置属性必须设置" userInfo:nil];
}
self.configure = configure;
[self initialization]; [self setupSubviews];
}
return self;
}
+ (instancetype)pageTitleViewWithFrame:(CGRect)frame delegate:(id)delegate titleNames:(NSArray *)titleNames configure:(SGPageTitleViewConfigure *)configure {
return [[self alloc] initWithFrame:frame delegate:delegate titleNames:titleNames configure:configure];
}
- (void)initialization {
_isTitleGradientEffect = YES;
_isOpenTitleTextZoom = NO;
_isShowIndicator = YES;
_isNeedBounces = YES;
_isShowBottomSeparator = YES;
_selectedIndex = 0;
_titleTextScaling = 0.1;
}
- (void)setupSubviews {
// 0、处理偏移量
UIView *tempView = [[UIView alloc] initWithFrame:CGRectZero];
[self addSubview:tempView];
// 1、添加 UIScrollView
[self addSubview:self.scrollView];
// 2、添加标题按钮
[self setupTitleButtons];
// 3、添加底部分割线
[self addSubview:self.bottomSeparator];
// 4、添加指示器
[self.scrollView insertSubview:self.indicatorView atIndex:0];
}
#pragma mark - - - layoutSubviews
- (void)layoutSubviews {
[super layoutSubviews];
// 选中按钮下标初始值
UIButton *lastBtn = self.btnMArr.lastObject;
if (lastBtn.tag >= _selectedIndex && _selectedIndex >= 0) {
[self P_btn_action:self.btnMArr[_selectedIndex]];
} else {
return;
}
}
#pragma mark - - - 懒加载
- (NSArray *)titleArr {
if (!_titleArr) {
_titleArr = [NSArray array];
}
return _titleArr;
}
- (NSMutableArray *)btnMArr {
if (!_btnMArr) {
_btnMArr = [NSMutableArray array];
}
return _btnMArr;
}
- (UIScrollView *)scrollView {
if (!_scrollView) {
_scrollView = [[UIScrollView alloc] init];
_scrollView.showsVerticalScrollIndicator = NO;
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.alwaysBounceHorizontal = YES;
_scrollView.frame = CGRectMake(0, 0, SGPageTitleViewWidth, SGPageTitleViewHeight);
}
return _scrollView;
}
- (UIView *)indicatorView {
if (!_indicatorView) {
_indicatorView = [[UIView alloc] init];
if (self.configure.indicatorStyle == SGIndicatorStyleDefault) {
CGFloat indicatorViewH = self.configure.indicatorHeight;
_indicatorView.SG_height = indicatorViewH;
_indicatorView.SG_y = self.SG_height - indicatorViewH;
} else {
CGFloat tempIndicatorViewH = [self SG_heightWithString:[self.btnMArr[0] currentTitle] font:self.configure.titleFont];
if (self.configure.indicatorHeight > self.SG_height) {
_indicatorView.SG_y = 0;
_indicatorView.SG_height = self.SG_height;
} else if (self.configure.indicatorHeight < tempIndicatorViewH) {
_indicatorView.SG_y = 0.5 * (self.SG_height - tempIndicatorViewH);
_indicatorView.SG_height = tempIndicatorViewH;
} else {
_indicatorView.SG_y = 0.5 * (self.SG_height - self.configure.indicatorHeight);
_indicatorView.SG_height = self.configure.indicatorHeight;
}
// 圆角处理
if (self.configure.indicatorCornerRadius > 0.5 * _indicatorView.SG_height) {
_indicatorView.layer.cornerRadius = 0.5 * _indicatorView.SG_height;
} else {
_indicatorView.layer.cornerRadius = self.configure.indicatorCornerRadius;
}
// 边框宽度及边框颜色
_indicatorView.layer.borderWidth = self.configure.indicatorBorderWidth;
_indicatorView.layer.borderColor = self.configure.indicatorBorderColor.CGColor;
}
_indicatorView.backgroundColor = self.configure.indicatorColor;
}
return _indicatorView;
}
- (UIView *)bottomSeparator {
if (!_bottomSeparator) {
_bottomSeparator = [[UIView alloc] init];
CGFloat bottomSeparatorW = self.SG_width;
CGFloat bottomSeparatorH = 0.5;
CGFloat bottomSeparatorX = 0;
CGFloat bottomSeparatorY = self.SG_height - bottomSeparatorH;
_bottomSeparator.frame = CGRectMake(bottomSeparatorX, bottomSeparatorY, bottomSeparatorW, bottomSeparatorH);
_bottomSeparator.backgroundColor = [UIColor lightGrayColor];
}
return _bottomSeparator;
}
#pragma mark - - - 计算字符串宽度
- (CGFloat)SG_widthWithString:(NSString *)string font:(UIFont *)font {
NSDictionary *attrs = @{NSFontAttributeName : font};
return [string boundingRectWithSize:CGSizeMake(0, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size.width;
}
- (CGFloat)SG_heightWithString:(NSString *)string font:(UIFont *)font {
NSDictionary *attrs = @{NSFontAttributeName : font};
return [string boundingRectWithSize:CGSizeMake(0, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size.height;
}
#pragma mark - - - 添加标题按钮
- (void)setupTitleButtons {
// 计算所有按钮的文字宽度
[self.titleArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
CGFloat tempWidth = [self SG_widthWithString:obj font:self.configure.titleFont];
self.allBtnTextWidth += tempWidth;
}];
// 所有按钮文字宽度 + 按钮之间的间隔
self.allBtnWidth = self.configure.spacingBetweenButtons * (self.titleArr.count + 1) + self.allBtnTextWidth;
self.allBtnWidth = ceilf(self.allBtnWidth);
NSInteger titleCount = self.titleArr.count;
if (self.allBtnWidth <= self.bounds.size.width) { /// SGPageTitleView 不可滚动
CGFloat btnY = 0;
CGFloat btnW = SGPageTitleViewWidth / self.titleArr.count;
CGFloat btnH = 0;
if (self.configure.indicatorStyle == SGIndicatorStyleDefault) {
btnH = SGPageTitleViewHeight - self.configure.indicatorHeight;
} else {
btnH = SGPageTitleViewHeight;
}
for (NSInteger index = 0; index < titleCount; index++) {
SGPageTitleButton *btn = [[SGPageTitleButton alloc] init];
CGFloat btnX = btnW * index;
btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
btn.tag = index;
btn.titleLabel.font = self.configure.titleFont;
[btn setTitle:self.titleArr[index] forState:(UIControlStateNormal)];
[btn setTitleColor:self.configure.titleColor forState:(UIControlStateNormal)];
[btn setTitleColor:self.configure.titleSelectedColor forState:(UIControlStateSelected)];
[btn addTarget:self action:@selector(P_btn_action:) forControlEvents:(UIControlEventTouchUpInside)];
[self.btnMArr addObject:btn];
[self.scrollView addSubview:btn];
[self setupStartColor:self.configure.titleColor];
[self setupEndColor:self.configure.titleSelectedColor];
}
self.scrollView.contentSize = CGSizeMake(SGPageTitleViewWidth, SGPageTitleViewHeight);
} else { /// SGPageTitleView 可滚动
CGFloat btnX = 0;
CGFloat btnY = 0;
CGFloat btnH = 0;
if (self.configure.indicatorStyle == SGIndicatorStyleDefault) {
btnH = SGPageTitleViewHeight - self.configure.indicatorHeight;
} else {
btnH = SGPageTitleViewHeight;
}
for (NSInteger index = 0; index < titleCount; index++) {
SGPageTitleButton *btn = [[SGPageTitleButton alloc] init];
CGFloat btnW = [self SG_widthWithString:self.titleArr[index] font:self.configure.titleFont] + self.configure.spacingBetweenButtons;
btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
btnX = btnX + btnW;
btn.tag = index;
btn.titleLabel.font = self.configure.titleFont;
[btn setTitle:self.titleArr[index] forState:(UIControlStateNormal)];
[btn setTitleColor:self.configure.titleColor forState:(UIControlStateNormal)];
[btn setTitleColor:self.configure.titleSelectedColor forState:(UIControlStateSelected)];
[btn addTarget:self action:@selector(P_btn_action:) forControlEvents:(UIControlEventTouchUpInside)];
[self.btnMArr addObject:btn];
[self.scrollView addSubview:btn];
[self setupStartColor:self.configure.titleColor];
[self setupEndColor:self.configure.titleSelectedColor];
}
CGFloat scrollViewWidth = CGRectGetMaxX(self.scrollView.subviews.lastObject.frame);
self.scrollView.contentSize = CGSizeMake(scrollViewWidth, SGPageTitleViewHeight);
}
}
#pragma mark - - - 标题按钮的点击事件
- (void)P_btn_action:(UIButton *)button {
// 1、改变按钮的选择状态
[self P_changeSelectedButton:button];
// 2、滚动标题选中居中
if (self.allBtnWidth > SGPageTitleViewWidth) {
[self P_selectedBtnCenter:button];
}
// 3、改变指示器的位置
[self P_changeIndicatorViewLocationWithButton:button];
// 4、pageTitleViewDelegate
if ([self.delegatePageTitleView respondsToSelector:@selector(pageTitleView:selectedIndex:)]) {
[self.delegatePageTitleView pageTitleView:self selectedIndex:button.tag];
}
// 5、标记按钮下标
self.signBtnIndex = button.tag;
}
/// 改变按钮的选择状态
- (void)P_changeSelectedButton:(UIButton *)button {
if (self.tempBtn == nil) {
button.selected = YES;
self.tempBtn = button;
} else if (self.tempBtn != nil && self.tempBtn == button){
button.selected = YES;
} else if (self.tempBtn != button && self.tempBtn != nil){
self.tempBtn.selected = NO;
button.selected = YES;
self.tempBtn = button;
}
// 标题文字缩放属性
if (self.isOpenTitleTextZoom) {
[self.btnMArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
UIButton *btn = obj;
btn.transform = CGAffineTransformMakeScale(1, 1);
}];
button.transform = CGAffineTransformMakeScale(1 + self.titleTextScaling, 1 + self.titleTextScaling);
}
}
/// 滚动标题选中居中
- (void)P_selectedBtnCenter:(UIButton *)centerBtn {
// 计算偏移量
CGFloat offsetX = centerBtn.center.x - SGPageTitleViewWidth * 0.5;
if (offsetX < 0) offsetX = 0;
// 获取最大滚动范围
CGFloat maxOffsetX = self.scrollView.contentSize.width - SGPageTitleViewWidth;
if (offsetX > maxOffsetX) offsetX = maxOffsetX;
// 滚动标题滚动条
[self.scrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}
/// 改变指示器的位置
- (void)P_changeIndicatorViewLocationWithButton:(UIButton *)button {
[UIView animateWithDuration:self.configure.indicatorAnimationTime animations:^{
CGFloat tempIndicatorWidth = self.configure.indicatorAdditionalWidth + [self SG_widthWithString:button.currentTitle font:self.configure.titleFont];
if (tempIndicatorWidth > button.SG_width) {
tempIndicatorWidth = button.SG_width;
}
self.indicatorView.SG_width = tempIndicatorWidth;
self.indicatorView.SG_centerX = button.SG_centerX;
}];
}
#pragma mark - - - 给外界提供的方法
- (void)setPageTitleViewWithProgress:(CGFloat)progress originalIndex:(NSInteger)originalIndex targetIndex:(NSInteger)targetIndex {
// 1、取出 originalBtn/targetBtn
UIButton *originalBtn = self.btnMArr[originalIndex];
UIButton *targetBtn = self.btnMArr[targetIndex];
self.signBtnIndex = targetBtn.tag;
// 2、 滚动标题选中居中
[self P_selectedBtnCenter:targetBtn];
// 3、处理指示器的逻辑
if (self.allBtnWidth <= self.bounds.size.width) { /// SGPageTitleView 不可滚动
if (self.configure.indicatorScrollStyle == SGIndicatorScrollStyleDefault) {
[self P_smallIndicatorScrollStyleDefaultWithProgress:progress originalBtn:originalBtn targetBtn:targetBtn];
} else {
[self P_smallIndicatorScrollStyleHalfEndWithProgress:progress originalBtn:originalBtn targetBtn:targetBtn];
}
} else { /// SGPageTitleView 可滚动
if (self.configure.indicatorScrollStyle == SGIndicatorScrollStyleDefault) {
[self P_indicatorScrollStyleDefaultWithProgress:progress originalBtn:originalBtn targetBtn:targetBtn];
} else {
[self P_indicatorScrollStyleHalfEndWithProgress:progress originalBtn:originalBtn targetBtn:targetBtn];
}
}
// 4、颜色的渐变(复杂)
if (self.isTitleGradientEffect) {
[self P_isTitleGradientEffectWithProgress:progress originalBtn:originalBtn targetBtn:targetBtn];
}
// 5 、标题文字缩放属性
if (self.isOpenTitleTextZoom) {
// 左边缩放
originalBtn.transform = CGAffineTransformMakeScale((1 - progress) * self.titleTextScaling + 1, (1 - progress) * self.titleTextScaling + 1);
// 右边缩放
targetBtn.transform = CGAffineTransformMakeScale(progress * self.titleTextScaling + 1, progress * self.titleTextScaling + 1);
}
}
/**
* 根据下标重置标题文字
*
* @param index 标题所对应的下标
* @param title 新标题名
*/
- (void)resetTitleWithIndex:(NSInteger)index newTitle:(NSString *)title {
if (index < self.btnMArr.count) {
UIButton *button = (UIButton *)self.btnMArr[index];
[button setTitle:title forState:UIControlStateNormal];
if (self.signBtnIndex == index) {
CGFloat tempIndicatorWidth = self.configure.indicatorAdditionalWidth + [self SG_widthWithString:button.currentTitle font:self.configure.titleFont];
if (tempIndicatorWidth > button.SG_width) {
tempIndicatorWidth = button.SG_width;
}
self.indicatorView.SG_width = tempIndicatorWidth;
self.indicatorView.SG_centerX = button.SG_centerX;
}
}
}
#pragma mark - - - 内部方法 --> P_
/// SGPageTitleView 不可滚动 - - - SGIndicatorScrollStyleDefault
- (void)P_smallIndicatorScrollStyleDefaultWithProgress:(CGFloat)progress originalBtn:(UIButton *)originalBtn targetBtn:(UIButton *)targetBtn {
// 1、改变按钮的选择状态
if (progress >= 0.8) { /// 此处取 >= 0.8 而不是 1.0 为的是防止用户滚动过快而按钮的选中状态并没有改变
[self P_changeSelectedButton:targetBtn];
}
CGFloat targetBtnX = CGRectGetMaxX(targetBtn.frame) - [self SG_widthWithString:targetBtn.currentTitle font:self.configure.titleFont] - 0.5 * (self.SG_width / self.titleArr.count - [self SG_widthWithString:targetBtn.currentTitle font:self.configure.titleFont] + self.configure.indicatorAdditionalWidth);
CGFloat originalBtnX = CGRectGetMaxX(originalBtn.frame) - [self SG_widthWithString:originalBtn.currentTitle font:self.configure.titleFont] - 0.5 * (self.SG_width / self.titleArr.count - [self SG_widthWithString:originalBtn.currentTitle font:self.configure.titleFont] + self.configure.indicatorAdditionalWidth);
CGFloat totalOffsetX = targetBtnX - originalBtnX;
/// 计算 targetBtn/originalBtn 宽度的差值
CGFloat targetBtnDistance = (CGRectGetMaxX(targetBtn.frame) - 0.5 * (self.SG_width / self.titleArr.count - [self SG_widthWithString:targetBtn.currentTitle font:self.configure.titleFont]));
CGFloat originalBtnDistance = (CGRectGetMaxX(originalBtn.frame) - 0.5 * (self.SG_width / self.titleArr.count - [self SG_widthWithString:originalBtn.currentTitle font:self.configure.titleFont]));
CGFloat totalDistance = targetBtnDistance - originalBtnDistance;
/// 计算 indicatorView 滚动时 X 的偏移量
CGFloat offsetX;
/// 计算 indicatorView 滚动时宽度的偏移量
CGFloat distance;
offsetX = totalOffsetX * progress;
distance = progress * (totalDistance - totalOffsetX);
/// 计算 indicatorView 新的 frame
self.indicatorView.SG_x = originalBtnX + offsetX;
CGFloat tempIndicatorWidth = self.configure.indicatorAdditionalWidth + [self SG_widthWithString:originalBtn.currentTitle font:self.configure.titleFont] + distance;
if (tempIndicatorWidth >= targetBtn.SG_width) {
CGFloat moveTotalX = targetBtn.SG_origin.x - originalBtn.SG_origin.x;
CGFloat moveX = moveTotalX * progress;
self.indicatorView.SG_centerX = originalBtn.SG_centerX + moveX;
} else {
self.indicatorView.SG_width = tempIndicatorWidth;
}
}
/// SGPageTitleView 可滚动 - - - SGIndicatorScrollStyleDefault
- (void)P_indicatorScrollStyleDefaultWithProgress:(CGFloat)progress originalBtn:(UIButton *)originalBtn targetBtn:(UIButton *)targetBtn {
/// 改变按钮的选择状态
if (progress >= 0.8) { /// 此处取 >= 0.8 而不是 1.0 为的是防止用户滚动过快而按钮的选中状态并没有改变
[self P_changeSelectedButton:targetBtn];
}
/// 计算 targetBtn/originalBtn 之间的距离
CGFloat totalOffsetX = targetBtn.SG_origin.x - originalBtn.SG_origin.x;
/// 计算 targetBtn/originalBtn 宽度的差值
CGFloat totalDistance = CGRectGetMaxX(targetBtn.frame) - CGRectGetMaxX(originalBtn.frame);
/// 计算 indicatorView 滚动时 X 的偏移量
CGFloat offsetX = 0.0;
/// 计算 indicatorView 滚动时宽度的偏移量
CGFloat distance = 0.0;
CGFloat tempIndicatorWidth = self.configure.indicatorAdditionalWidth + [self SG_widthWithString:targetBtn.currentTitle font:self.configure.titleFont];
if (tempIndicatorWidth >= targetBtn.SG_width) {
offsetX = totalOffsetX * progress;
distance = progress * (totalDistance - totalOffsetX);
self.indicatorView.SG_x = originalBtn.SG_origin.x + offsetX;
self.indicatorView.SG_width = originalBtn.SG_width + distance;
} else {
offsetX = totalOffsetX * progress + 0.5 * self.configure.spacingBetweenButtons - 0.5 * self.configure.indicatorAdditionalWidth;
distance = progress * (totalDistance - totalOffsetX) - self.configure.spacingBetweenButtons;
/// 计算 indicatorView 新的 frame
self.indicatorView.SG_x = originalBtn.SG_origin.x + offsetX;
self.indicatorView.SG_width = originalBtn.SG_width + distance + self.configure.indicatorAdditionalWidth;
}
}
/// SGPageTitleView 不可滚动 - - - SGIndicatorScrollStyleHalf - SGIndicatorScrollStyleEnd
- (void)P_smallIndicatorScrollStyleHalfEndWithProgress:(CGFloat)progress originalBtn:(UIButton *)originalBtn targetBtn:(UIButton *)targetBtn {
if (self.configure.indicatorScrollStyle == SGIndicatorScrollStyleHalf) {
if (progress >= 0.5) {
CGFloat tempIndicatorWidth = self.configure.indicatorAdditionalWidth + [self SG_widthWithString:targetBtn.currentTitle font:self.configure.titleFont];
[UIView animateWithDuration:self.configure.indicatorAnimationTime animations:^{
if (tempIndicatorWidth >= targetBtn.SG_width) {
self.indicatorView.SG_width = targetBtn.SG_width;
} else {
self.indicatorView.SG_width = tempIndicatorWidth;
}
self.indicatorView.SG_centerX = targetBtn.SG_centerX;
[self P_changeSelectedButton:targetBtn];
}];
} else {
CGFloat tempIndicatorWidth = self.configure.indicatorAdditionalWidth + [self SG_widthWithString:originalBtn.currentTitle font:self.configure.titleFont];
[UIView animateWithDuration:self.configure.indicatorAnimationTime animations:^{
if (tempIndicatorWidth >= targetBtn.SG_width) {
self.indicatorView.SG_width = originalBtn.SG_width;
} else {
self.indicatorView.SG_width = tempIndicatorWidth;
}
self.indicatorView.SG_centerX = originalBtn.SG_centerX;
[self P_changeSelectedButton:originalBtn];
}];
}
} else {
if (progress == 1.0) {
CGFloat tempIndicatorWidth = self.configure.indicatorAdditionalWidth + [self SG_widthWithString:targetBtn.currentTitle font:self.configure.titleFont];
[UIView animateWithDuration:self.configure.indicatorAnimationTime animations:^{
if (tempIndicatorWidth >= targetBtn.SG_width) {
self.indicatorView.SG_width = targetBtn.SG_width;
} else {
self.indicatorView.SG_width = tempIndicatorWidth;
}
self.indicatorView.SG_centerX = targetBtn.SG_centerX;
[self P_changeSelectedButton:targetBtn];
}];
} else {
CGFloat tempIndicatorWidth = self.configure.indicatorAdditionalWidth + [self SG_widthWithString:originalBtn.currentTitle font:self.configure.titleFont];
[UIView animateWithDuration:self.configure.indicatorAnimationTime animations:^{
if (tempIndicatorWidth >= targetBtn.SG_width) {
self.indicatorView.SG_width = originalBtn.SG_width;
} else {
self.indicatorView.SG_width = tempIndicatorWidth;
}
self.indicatorView.SG_centerX = originalBtn.SG_centerX;
[self P_changeSelectedButton:originalBtn];
}];
}
}
}
/// SGPageTitleView 可滚动 - - - SGIndicatorScrollStyleHalf - SGIndicatorScrollStyleEnd
- (void)P_indicatorScrollStyleHalfEndWithProgress:(CGFloat)progress originalBtn:(UIButton *)originalBtn targetBtn:(UIButton *)targetBtn {
if (self.configure.indicatorScrollStyle == SGIndicatorScrollStyleHalf) {
if (progress >= 0.5) {
CGFloat tempIndicatorWidth = self.configure.indicatorAdditionalWidth + [self SG_widthWithString:targetBtn.currentTitle font:self.configure.titleFont];
[UIView animateWithDuration:self.configure.indicatorAnimationTime animations:^{
if (tempIndicatorWidth >= targetBtn.SG_width) {
self.indicatorView.SG_width = targetBtn.SG_width;
} else {
self.indicatorView.SG_width = tempIndicatorWidth;
}
self.indicatorView.SG_centerX = targetBtn.SG_centerX;
[self P_changeSelectedButton:targetBtn];
}];
} else {
CGFloat tempIndicatorWidth = self.configure.indicatorAdditionalWidth + [self SG_widthWithString:originalBtn.currentTitle font:self.configure.titleFont];
[UIView animateWithDuration:self.configure.indicatorAnimationTime animations:^{
if (tempIndicatorWidth >= originalBtn.SG_width) {
self.indicatorView.SG_width = originalBtn.SG_width;
} else {
self.indicatorView.SG_width = tempIndicatorWidth;
}
self.indicatorView.SG_centerX = originalBtn.SG_centerX;
[self P_changeSelectedButton:originalBtn];
}];
}
} else {
if (progress == 1.0) {
CGFloat tempIndicatorWidth = self.configure.indicatorAdditionalWidth + [self SG_widthWithString:targetBtn.currentTitle font:self.configure.titleFont];
[UIView animateWithDuration:self.configure.indicatorAnimationTime animations:^{
if (tempIndicatorWidth >= targetBtn.SG_width) {
self.indicatorView.SG_width = targetBtn.SG_width;
} else {
self.indicatorView.SG_width = tempIndicatorWidth;
}
self.indicatorView.SG_centerX = targetBtn.SG_centerX;
[self P_changeSelectedButton:targetBtn];
}];
} else {
CGFloat tempIndicatorWidth = self.configure.indicatorAdditionalWidth + [self SG_widthWithString:originalBtn.currentTitle font:self.configure.titleFont];
[UIView animateWithDuration:self.configure.indicatorAnimationTime animations:^{
if (tempIndicatorWidth >= originalBtn.SG_width) {
self.indicatorView.SG_width = originalBtn.SG_width;
} else {
self.indicatorView.SG_width = tempIndicatorWidth;
}
self.indicatorView.SG_centerX = originalBtn.SG_centerX;
[self P_changeSelectedButton:originalBtn];
}];
}
}
}
#pragma mark - - - 颜色渐变方法抽取
- (void)P_isTitleGradientEffectWithProgress:(CGFloat)progress originalBtn:(UIButton *)originalBtn targetBtn:(UIButton *)targetBtn {
// 获取 targetProgress
CGFloat targetProgress = progress;
// 获取 originalProgress
CGFloat originalProgress = 1 - targetProgress;
CGFloat r = self.endR - self.startR;
CGFloat g = self.endG - self.startG;
CGFloat b = self.endB - self.startB;
UIColor *originalColor = [UIColor colorWithRed:self.startR + r * originalProgress green:self.startG + g * originalProgress blue:self.startB + b * originalProgress alpha:1];
UIColor *targetColor = [UIColor colorWithRed:self.startR + r * targetProgress green:self.startG + g * targetProgress blue:self.startB + b * targetProgress alpha:1];
// 设置文字颜色渐变
originalBtn.titleLabel.textColor = originalColor;
targetBtn.titleLabel.textColor = targetColor;
}
#pragma mark - - - set
- (void)setIsNeedBounces:(BOOL)isNeedBounces {
_isNeedBounces = isNeedBounces;
if (isNeedBounces == NO) {
self.scrollView.bounces = NO;
}
}
- (void)setSelectedIndex:(NSInteger)selectedIndex {
_selectedIndex = selectedIndex;
if (selectedIndex) {
_selectedIndex = selectedIndex;
}
}
- (void)setResetSelectedIndex:(NSInteger)resetSelectedIndex {
_resetSelectedIndex = resetSelectedIndex;
[self P_btn_action:self.btnMArr[resetSelectedIndex]];
}
- (void)setIsTitleGradientEffect:(BOOL)isTitleGradientEffect {
_isTitleGradientEffect = isTitleGradientEffect;
}
- (void)setIsOpenTitleTextZoom:(BOOL)isOpenTitleTextZoom {
_isOpenTitleTextZoom = isOpenTitleTextZoom;
}
- (void)setTitleTextScaling:(CGFloat)titleTextScaling {
_titleTextScaling = titleTextScaling;
if (titleTextScaling) {
if (titleTextScaling >= 0.3) {
_titleTextScaling = 0.3;
} else {
_titleTextScaling = 0.1;
}
}
}
- (void)setIsShowIndicator:(BOOL)isShowIndicator {
_isShowIndicator = isShowIndicator;
if (isShowIndicator == NO) {
[self.indicatorView removeFromSuperview];
self.indicatorView = nil;
}
}
- (void)setIsShowBottomSeparator:(BOOL)isShowBottomSeparator {
_isShowBottomSeparator = isShowBottomSeparator;
if (isShowBottomSeparator) {
} else {
[self.bottomSeparator removeFromSuperview];
self.bottomSeparator = nil;
}
}
#pragma mark - - - 颜色设置的计算
/// 开始颜色设置
- (void)setupStartColor:(UIColor *)color {
CGFloat components[3];
[self getRGBComponents:components forColor:color];
self.startR = components[0];
self.startG = components[1];
self.startB = components[2];
}
/// 结束颜色设置
- (void)setupEndColor:(UIColor *)color {
CGFloat components[3];
[self getRGBComponents:components forColor:color];
self.endR = components[0];
self.endG = components[1];
self.endB = components[2];
}
/**
* 指定颜色,获取颜色的RGB值
*
* @param components RGB数组
* @param color 颜色
*/
- (void)getRGBComponents:(CGFloat [3])components forColor:(UIColor *)color {
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char resultingPixel[4];
CGContextRef context = CGBitmapContextCreate(&resultingPixel, 1, 1, 8, 4, rgbColorSpace, 1);
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, CGRectMake(0, 0, 1, 1));
CGContextRelease(context);
CGColorSpaceRelease(rgbColorSpace);
for (int component = 0; component < 3; component++) {
components[component] = resultingPixel[component] / 255.0f;
}
}
@end
SGPageTitleViewConfigure.h
#import<UIKit/UIKit.h>
#import<Foundation/Foundation.h>
typedef enum : NSUInteger {
/// 下划线样式
SGIndicatorStyleDefault,
/// 遮盖样式
SGIndicatorStyleCover,
} SGIndicatorStyle;
typedef enum : NSUInteger {
/// 指示器位置跟随内容滚动而改变
SGIndicatorScrollStyleDefault,
/// 内容滚动一半时指示器位置改变
SGIndicatorScrollStyleHalf,
/// 内容滚动结束时指示器位置改变
SGIndicatorScrollStyleEnd
} SGIndicatorScrollStyle;
@interface SGPageTitleViewConfigure : NSObject
/** 类方法创建 */
+ (instancetype)pageTitleViewConfigure;
/** 按钮之间的间距,默认为 20.0f */
@property (nonatomic, assign) CGFloat spacingBetweenButtons;
/** 标题文字字号大小,默认 15 号字体 */
@property (nonatomic, strong) UIFont *titleFont;
/** 普通状态下标题按钮文字的颜色,默认为黑色 */
@property (nonatomic, strong) UIColor *titleColor;
/** 选中状态下标题按钮文字的颜色,默认为红色 */
@property (nonatomic, strong) UIColor *titleSelectedColor;
/** 指示器高度,默认为 2.0f */
@property (nonatomic, assign) CGFloat indicatorHeight;
/** 指示器颜色,默认为红色 */
@property (nonatomic, strong) UIColor *indicatorColor;
/** 指示器的额外宽度,介于按钮文字宽度与按钮宽度之间 */
@property (nonatomic, assign) CGFloat indicatorAdditionalWidth;
/** 指示器动画时间,默认为 0.1f,取值范围 0 ~ 0.3f */
@property (nonatomic, assign) CGFloat indicatorAnimationTime;
/** 指示器样式,默认为 SGIndicatorStyleDefault */
@property (nonatomic, assign) SGIndicatorStyle indicatorStyle;
/** 指示器遮盖样式下的圆角大小,默认为 0.1f */
@property (nonatomic, assign) CGFloat indicatorCornerRadius;
/** 指示器遮盖样式下的边框宽度,默认为 0.0f */
@property (nonatomic, assign) CGFloat indicatorBorderWidth;
/** 指示器遮盖样式下的边框颜色,默认为 clearColor */
@property (nonatomic, strong) UIColor *indicatorBorderColor;
/** 指示器滚动位置改变样式,默认为 SGIndicatorScrollStyleDefault */
@property (nonatomic, assign) SGIndicatorScrollStyle indicatorScrollStyle;
@end
SGPageTitleViewConfigure.m
#import "SGPageTitleViewConfigure.h"
@implementation SGPageTitleViewConfigure
+ (instancetype)pageTitleViewConfigure {
return [[self alloc] init];
}
- (CGFloat)spacingBetweenButtons {
if (_spacingBetweenButtons <= 0) {
_spacingBetweenButtons = 20;
}
return _spacingBetweenButtons;
}
- (UIFont *)titleFont {
if (!_titleFont) {
_titleFont = [UIFont systemFontOfSize:15];
}
return _titleFont;
}
- (UIColor *)titleColor {
if (!_titleColor) {
_titleColor = [UIColor blackColor];
}
return _titleColor;
}
- (UIColor *)titleSelectedColor {
if (!_titleSelectedColor) {
_titleSelectedColor = [UIColor redColor];
}
return _titleSelectedColor;
}
- (CGFloat)indicatorHeight {
if (_indicatorHeight <= 0) {
_indicatorHeight = 2.0f;
}
return _indicatorHeight;
}
- (UIColor *)indicatorColor {
if (!_indicatorColor) {
_indicatorColor = [UIColor redColor];
}
return _indicatorColor;
}
- (CGFloat)indicatorAdditionalWidth {
if (_indicatorAdditionalWidth <= 0) {
_indicatorAdditionalWidth = 0;
}
return _indicatorAdditionalWidth;
}
- (CGFloat)indicatorAnimationTime {
if (_indicatorAnimationTime <= 0) {
_indicatorAnimationTime = 0.1;
} else if (_indicatorAnimationTime > 0.3) {
_indicatorAnimationTime = 0.3;
}
return _indicatorAnimationTime;
}
- (CGFloat)indicatorCornerRadius {
if (_indicatorCornerRadius <= 0) {
_indicatorCornerRadius = 0;
}
return _indicatorCornerRadius;
}
- (CGFloat)indicatorBorderWidth {
if (_indicatorBorderWidth <= 0) {
_indicatorBorderWidth = 0;
}
return _indicatorBorderWidth;
}
- (UIColor *)indicatorBorderColor {
if (!_indicatorBorderColor) {
_indicatorBorderColor = [UIColor clearColor];
}
return _indicatorBorderColor;
}
@end
SGPagingView.h
#import "SGPageTitleViewConfigure.h"
#import "SGPageTitleView.h"
#import "SGPageContentView.h"
#import "SGPageContentScrollView.h"
yiTableViewCell.h
@interface yiTableViewCell : UITableViewCell
@property(nonatomic,strong)UIButton *btn1;
@property(nonatomic,strong)UIButton *btn2;
@property(nonatomic,strong)UIButton *btn3;
@property(nonatomic,strong)UIButton *btn4;
@property(nonatomic,strong)UILabel *label1;
@property(nonatomic,strong)UILabel *label2;
@property(nonatomic,strong)UILabel *label3;
@property(nonatomic,strong)UILabel *label4;
@end
yiTableViewCell.m
#import "yiTableViewCell.h"
@implementation yiTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
_btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
_btn1.frame = CGRectMake(50, 20, 40, 40);
_btn1.backgroundColor = [UIColor blueColor];
[_btn1 addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
_btn1.layer.masksToBounds = YES;
_btn1.layer.cornerRadius = 20;
_label1 = [[UILabel alloc]init];
_label1.frame = CGRectMake(50, 70, 50, 10);
_label1.font = [UIFont systemFontOfSize:12];
_label1.text = @"正晚点";
_btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
_btn2.frame = CGRectMake(140, 20, 40, 40);
_btn2.backgroundColor = [UIColor greenColor];
[_btn2 addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
_btn2.layer.masksToBounds = YES;
_btn2.layer.cornerRadius = 20;
_label2 = [[UILabel alloc]init];
_label2.frame = CGRectMake(135, 70, 50, 10);
_label2.font = [UIFont systemFontOfSize:12];
_label2.text = @"温馨服务";
_btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
_btn3.frame = CGRectMake(230, 20, 40, 40);
_btn3.backgroundColor = [UIColor orangeColor];
[_btn3 addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
_btn3.layer.masksToBounds = YES;
_btn3.layer.cornerRadius = 20;
_label3 = [[UILabel alloc]init];
_label3.frame = CGRectMake(225, 70, 50, 10);
_label3.font = [UIFont systemFontOfSize:12];
_label3.text = @"订餐服务";
_btn4 = [UIButton buttonWithType:UIButtonTypeCustom];
_btn4.frame = CGRectMake(320, 20, 40, 40);
_btn4.backgroundColor = [UIColor redColor];
[_btn4 addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
_btn4.layer.masksToBounds = YES;
_btn4.layer.cornerRadius = 20;
_label4 = [[UILabel alloc]init];
_label4.frame = CGRectMake(325, 70, 50, 10);
_label4.font = [UIFont systemFontOfSize:12];
_label4.text = @"约车";
[self.contentView addSubview:self.btn1];
[self.contentView addSubview:self.btn2];
[self.contentView addSubview:self.btn3];
[self.contentView addSubview:self.btn4];
[self.contentView addSubview:self.label1];
[self.contentView addSubview:self.label2];
[self.contentView addSubview:self.label3];
[self.contentView addSubview:self.label4];
}
return self;
}
-(void)click
{
NSLog(@"点我");
}
//-(UIButton *)btn1
//{
// if (_btn1) {
// _btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
// _btn1.frame = CGRectMake(20, 20, 40, 40);
// _btn1.backgroundColor = [UIColor blueColor];
// _btn1.layer.masksToBounds = YES;
// _btn1.layer.cornerRadius = 20;
// }
// return _btn1;
//}
//-(UILabel *)label1
//{
// if (_label1) {
// _label1 = [[UILabel alloc]init];
// _label1.frame = CGRectMake(20, 65, 40, 10);
// _label1.text = @"正晚点";
// }
// return _label1;
//}
//-(UIButton *)btn2
//{
// if (_btn2) {
// _btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
// _btn2.frame = CGRectMake(80, 20, 40, 40);
// _btn2.backgroundColor = [UIColor greenColor];
// _btn2.layer.masksToBounds = YES;
// _btn2.layer.cornerRadius = 20;
// }
// return _btn2;
//}
//-(UILabel *)label2
//{
// if (_label2) {
// _label2 = [[UILabel alloc]init];
// _label2.frame = CGRectMake(80, 65, 40, 10);
// _label2.text = @"温馨服务";
// }
// return _label2;
//}
//-(UIButton *)btn3
//{
// if (_btn3) {
// _btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
// _btn3.frame = CGRectMake(140, 20, 40, 40);
// _btn3.backgroundColor = [UIColor orangeColor];
// _btn3.layer.masksToBounds = YES;
// _btn3.layer.cornerRadius = 20;
// }
// return _btn3;
//}
//-(UILabel *)label3
//{
// if (_label3) {
// _label3 = [[UILabel alloc]init];
// _label3.frame = CGRectMake(140, 65, 40, 10);
// _label3.text = @"订餐服务";
// }
// return _label3;
//}
//-(UIButton *)btn4
//{
// if (_btn4) {
// _btn4 = [UIButton buttonWithType:UIButtonTypeCustom];
// _btn4.frame = CGRectMake(200, 20, 40, 40);
// _btn4.backgroundColor = [UIColor redColor];
// _btn4.layer.masksToBounds = YES;
// _btn4.layer.cornerRadius = 20;
// }
// return _btn4;
//}
//-(UILabel *)label4
//{
// if (_label4) {
// _label4 = [[UILabel alloc]init];
// _label4.frame = CGRectMake(200, 65, 40, 10);
// _label4.text = @"约车";
// }
// return _label4;
//}
@end
erTableViewCell.h
#import@interface erTableViewCell : UITableViewCell
@property(nonatomic,strong)UILabel *label1;
@property(nonatomic,strong)UILabel *label2;
@property(nonatomic,strong)UIImageView *image1;
@end
erTableViewCell.m
#import "erTableViewCell.h"
@implementation erTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
_label1 = [[UILabel alloc]init];
_label1.text = @"北京";
_label1.frame = CGRectMake(70, 20, 60, 50);
_label1.font = [UIFont systemFontOfSize:25];
_label2 = [[UILabel alloc]init];
_label2.text = @"上海";
_label2.frame = CGRectMake(290, 20, 60, 50);
_label2.font = [UIFont systemFontOfSize:25];
_image1 = [[UIImageView alloc]init];
_image1.frame = CGRectMake(180, 35, 40, 30);
_image1.image = [UIImage imageNamed:@"切片1副本.png"];
[self.contentView addSubview:self.label1];
[self.contentView addSubview:self.label2];
[self.contentView addSubview:self.image1];
}
return self;
}
//-(UILabel *)label1
//{
// if (!_label1) {
// _label1 = [[UILabel alloc]init];
// _label1.text = @"北京";
// _label1.frame = CGRectMake(50, 20, 50, 30);
// _label1.font = [UIFont systemFontOfSize:20];
// }
// return _label1;
//}
//-(UILabel *)label2
//{
// if (!_label2) {
// _label2 = [[UILabel alloc]init];
// _label2.text = @"上海";
// _label1.frame = CGRectMake(150, 20, 50, 30);
// _label2.font = [UIFont systemFontOfSize:20];
// }
// return _label2;
//}
@end
sanTableViewCell.h
#import@interface sanTableViewCell : UITableViewCell
@property(nonatomic,strong)UILabel *label1;
@property(nonatomic,strong)UILabel *label2;
@property(nonatomic,strong)UILabel *label3;
@property(nonatomic,strong)UILabel *label4;
@property(nonatomic,strong)UILabel *label5;
@property(nonatomic,strong)UILabel *label6;
@property(nonatomic,strong)UILabel *label7;
@end
sanTableViewCell.m
#import "sanTableViewCell.h"
@implementation sanTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
_label1 = [[UILabel alloc]init];
_label1.frame = CGRectMake(50, 30, 70, 20);
_label1.text = @"出发日期";
_label1.textColor = [UIColor grayColor];
_label2 = [[UILabel alloc]init];
_label2.frame = CGRectMake(50, 100, 70, 20);
_label2.text = @"出发时间";
_label2.textColor = [UIColor grayColor];
_label3 = [[UILabel alloc]init];
_label3.frame = CGRectMake(50, 170, 70, 20);
_label3.text = @"席 别";
_label3.textColor = [UIColor grayColor];
_label4 = [[UILabel alloc]init];
_label4.frame = CGRectMake(150, 30, 100, 20);
_label4.text = @"2017-10-17";
_label5 = [[UILabel alloc]init];
_label5.frame = CGRectMake(150, 100, 100, 20);
_label5.text = @"00.00-24.00";
_label6 = [[UILabel alloc]init];
_label6.frame = CGRectMake(150, 170, 100, 20);
_label6.text = @"不限";
_label7 = [[UILabel alloc]init];
_label7.frame = CGRectMake(320, 30, 70, 20);
_label7.text = @"学生";
[self addSubview:self.label1];
[self addSubview:self.label2];
[self addSubview:self.label3];
[self addSubview:self.label4];
[self addSubview:self.label5];
[self addSubview:self.label6];
[self addSubview:self.label7];
}
return self;
}
@end
siTableViewCell.h
#import#import "SGPagingView.h"@interface siTableViewCell : UITableViewCell@property(nonatomic,strong)UIButton *btn1;
@property(nonatomic,strong)UIButton *btn2;
@property(nonatomic,strong)UILabel *label1;
@property(nonatomic,strong)UILabel *label2;
@property(nonatomic,strong)UILabel *label3;
@property (nonatomic, strong) SGPageTitleView *pageTitleView;
@end
siTableViewCell.m
#import "siTableViewCell.h"
@implementation siTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
NSArray *titleArr = [@[@"全部", @"G/D/C", @"Z字头", @"T字头", @"K字头", @"其他"]mutableCopy];
SGPageTitleViewConfigure *configure = [SGPageTitleViewConfigure pageTitleViewConfigure];
configure.indicatorAdditionalWidth = 120;
configure.spacingBetweenButtons = 20;
configure.titleFont = [UIFont systemFontOfSize:17];
configure.titleSelectedColor = [UIColor whiteColor];
configure.indicatorColor = [UIColor colorWithRed:0/250.0 green:137/250.0 blue:211/250.0 alpha:1];
configure.indicatorScrollStyle = SGIndicatorScrollStyleDefault;
configure.indicatorStyle = SGIndicatorStyleCover;
_pageTitleView.isOpenTitleTextZoom = YES;
self.pageTitleView = [SGPageTitleView pageTitleViewWithFrame:CGRectMake(10, 0, 394, 44) delegate:self titleNames:titleArr configure:configure];
[self addSubview:_pageTitleView];
_btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
_btn1.frame = CGRectMake(20, 50, 35, 40);
_btn1.tintColor = [UIColor orangeColor];
[_btn1 setTitle:@"➕" forState:UIControlStateNormal];
[_btn1 addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
_label1 = [[UILabel alloc]init];
_label1.text = @"乘客";
_label1.textColor = [UIColor orangeColor];
_label1.frame = CGRectMake(60, 50, 100, 40);
_btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
_btn2.frame = CGRectMake(10, 100, 394, 50);
_btn2.backgroundColor = [UIColor colorWithRed:0/250.0 green:137/250.0 blue:211/250.0 alpha:1];
_btn2.layer.cornerRadius = 10;
[_btn2 setTitle:@"查询" forState:UIControlStateNormal];
// [_btn2 addTarget:self action:@selector(clicks) forControlEvents:UIControlEventTouchUpInside];
_label2 = [[UILabel alloc]init];
_label2.frame = CGRectMake(10, 160, 394, 40);
_label2.text = @"----------------- -----------------";
_label2.textColor = [UIColor colorWithRed:0/250.0 green:137/250.0 blue:211/250.0 alpha:1];
_label3 = [[UILabel alloc]init];
_label3.frame = CGRectMake(155, 160, 130, 40);
_label3.text = @"最近常用路线";
_label3.textColor = [UIColor grayColor];
[self addSubview:self.btn1];
[self addSubview:self.btn2];
[self addSubview:self.label1];
[self addSubview:self.label2];
[self addSubview:self.label3];
}
return self;
}
-(void)click
{
NSLog(@"乘客");
}
@end
wuTableViewCell.h
#import@interface wuTableViewCell : UITableViewCell
@property(nonatomic,strong)UILabel * label1;
@property(nonatomic,strong)UILabel * label2;
@property(nonatomic,strong)UILabel * label3;
@end
wuTableViewCell.m
#import "wuTableViewCell.h"
@implementation wuTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
_label1 = [[UILabel alloc]init];
_label1.frame = CGRectMake(0, 0, 100, 50);
_label1.text = @" 前一天";
_label1.textColor = [UIColor whiteColor];
_label1.backgroundColor = [UIColor colorWithRed:90/255.0 green:175/255.0 blue:225/255.0 alpha:1];
_label2 = [[UILabel alloc]init];
_label2.frame = CGRectMake(100, 0, 214, 50);
_label2.text = @" 2017-10-17";
_label2.font = [UIFont systemFontOfSize:18];
_label2.textColor = [UIColor whiteColor];
_label2.backgroundColor = [UIColor colorWithRed:55/255.0 green:145/255.0 blue:205/255.0 alpha:1];
_label3 = [[UILabel alloc]init];
_label3.frame = CGRectMake(314, 0, 100, 50);
_label3.text = @" 后一天";
_label3.textColor = [UIColor whiteColor];
_label3.backgroundColor = [UIColor colorWithRed:90/255.0 green:175/255.0 blue:225/255.0 alpha:1];
[self addSubview:self.label1];
[self addSubview:self.label2];
[self addSubview:self.label3];
}
return self;
}
//-(UILabel *)label1
//{
// if (!_label1) {
//
// }
// return _label1;
//}
@end
cheViewController.m
#import "cheViewController.h"#import "yiTableViewCell.h"#import "erTableViewCell.h"#import "sanTableViewCell.h"#import "siTableViewCell.h"#import "chaxunViewController.h"static NSString *celli = @"yide";static NSString *cellID = @"adad";static NSString *cellIDs = @"adads";static NSString *cellIDse = @"adadse";@interface cheViewController (){
UITableView *table;
}
@end
@implementation cheViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0/250.0 green:137/250.0 blue:211/250.0 alpha:1];
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
table.delegate = self;
table.dataSource = self;
[table registerClass:[yiTableViewCell class] forCellReuseIdentifier:celli];
[table registerClass:[erTableViewCell class] forCellReuseIdentifier:cellID];
[table registerClass:[sanTableViewCell class] forCellReuseIdentifier:cellIDs];
[table registerClass:[siTableViewCell class] forCellReuseIdentifier:cellIDse];
[self.view addSubview:table];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 6;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 5) {
return 5;
}
return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
return 100;
}else if (indexPath.section == 2) {
return 230;
}else if (indexPath.section == 3) {
return 220;
}
return 100;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
switch (indexPath.section) {
case 0:
{
yiTableViewCell *cells = [tableView dequeueReusableCellWithIdentifier:celli];
if (!cells) {
cells = [[yiTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:celli];
}
return cells;
}
break;
case 1:
{
erTableViewCell *cellse = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cellse) {
cellse = [[erTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
return cellse;
}
break;
case 2:
{
sanTableViewCell *cellses = [tableView dequeueReusableCellWithIdentifier:cellIDs];
if (!cellses) {
cellses = [[sanTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIDs];
}
return cellses;
}
break;
case 3:
{
siTableViewCell *cellsesa = [tableView dequeueReusableCellWithIdentifier:cellIDse];
if (!cellsesa) {
cellsesa = [[siTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIDse];
}
[cellsesa.btn2 addTarget:self action:@selector(clicks) forControlEvents:UIControlEventTouchUpInside];
return cellsesa;
}
break;
default:
cell = [tableView dequeueReusableCellWithIdentifier:@""];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
}
break;
}
return cell;
}
-(void)clicks
{
NSLog(@"查询");
chaxunViewController *chaxun = [[chaxunViewController alloc]init];
chaxun.navigationItem.hidesBackButton = YES;
[self.navigationController pushViewController:chaxun animated:YES];
}
@end
chaxunViewController.m
#import "chaxunViewController.h"#define Screenwidth [UIScreen mainScreen].bounds.size.width#define ScreennHeight [UIScreen mainScreen].bounds.size.height@interface chaxunViewController (){
BOOL close[30];
}
@property (nonatomic,strong)UILabel *checiLab;
@property (nonatomic,strong)UILabel *kaishiLab;
@property (nonatomic,strong)UILabel *kaishitimeLab;
@property (nonatomic,strong)UILabel *photoLab;
@property (nonatomic,strong)UILabel *jiantouLab;
@property (nonatomic,strong)UILabel *sumtimeLab;
@property (nonatomic,strong)UILabel *jieshuLab;
@property (nonatomic,strong)UILabel *jieshutimeLab;
@property (nonatomic,strong)UILabel *shangwuLab;
@property (nonatomic,strong)UILabel *OneLab;
@property (nonatomic,strong)UILabel *TwoLab;
@property (nonatomic,strong)UIButton *bottomBtn;
@property (nonatomic,strong) UITableView *WBTableView;
@property (nonatomic,strong) NSArray *Close;
@property(nonatomic,strong)UILabel * label1;
@property(nonatomic,strong)UILabel * label2;
@property(nonatomic,strong)UILabel * label3;
@end
@implementation chaxunViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"北京<>上海";
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0/250.0 green:137/250.0 blue:211/250.0 alpha:1];
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
UIButton *leftbtn = [UIButton buttonWithType:UIButtonTypeCustom];
[leftbtn setTitle:@"<" forState:UIControlStateNormal];
leftbtn.tintColor = [UIColor whiteColor];
[leftbtn addTarget:self action:@selector(clickleft) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftbtnitem = [[UIBarButtonItem alloc]initWithCustomView:leftbtn];
self.navigationItem.leftBarButtonItem = leftbtnitem;
UIButton *rightbtn = [UIButton buttonWithType:UIButtonTypeCustom];
[rightbtn setTitle:@"匚" forState:UIControlStateNormal];
rightbtn.tintColor = [UIColor whiteColor];
[rightbtn addTarget:self action:@selector(clickright) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightbtnitem = [[UIBarButtonItem alloc]initWithCustomView:rightbtn];
self.navigationItem.rightBarButtonItem = rightbtnitem;
UIView *view = [[UIView alloc]init];
view.frame = CGRectMake(0, 64, self.view.frame.size.width, 50);
view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:view];
_label1 = [[UILabel alloc]init];
_label1.frame = CGRectMake(0, 0, 100, 50);
_label1.text = @" 前一天";
_label1.textColor = [UIColor whiteColor];
_label1.backgroundColor = [UIColor colorWithRed:90/255.0 green:175/255.0 blue:225/255.0 alpha:1];
[view addSubview:_label1];
_label2 = [[UILabel alloc]init];
_label2.frame = CGRectMake(100, 0, 214, 50);
_label2.text = @" 2017-10-17";
_label2.font = [UIFont systemFontOfSize:18];
_label2.textColor = [UIColor whiteColor];
_label2.backgroundColor = [UIColor colorWithRed:55/255.0 green:145/255.0 blue:205/255.0 alpha:1];
[view addSubview:_label2];
_label3 = [[UILabel alloc]init];
_label3.frame = CGRectMake(314, 0, 100, 50);
_label3.text = @" 后一天";
_label3.textColor = [UIColor whiteColor];
_label3.backgroundColor = [UIColor colorWithRed:90/255.0 green:175/255.0 blue:225/255.0 alpha:1];
[view addSubview:_label3];
//这个的目的是为了使得启动app时,单元格是收缩的
for (int i=0; i<30; i++) {
close[i] = YES;
}
//创建
_WBTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 114, Screenwidth, ScreennHeight) style:UITableViewStylePlain];
_WBTableView.dataSource = self;
_WBTableView.delegate = self;
[self.view addSubview:self.WBTableView];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 5;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (close[section]) {
return 0;
}
return 5;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 85;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
cell.textLabel.text = [NSString stringWithFormat:@"第%ld组 第%ld行",indexPath.section,indexPath.row];
return cell;
}
//创建组头视图
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIControl *view = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, Screenwidth, 85)];
view.tag = 1000 + section;
[view addTarget:self action:@selector(sectionClick:) forControlEvents:UIControlEventTouchUpInside];
_checiLab = [[UILabel alloc]initWithFrame:CGRectMake(8, 8, 50, 30)];
_checiLab.font = [UIFont systemFontOfSize:21];
_checiLab.textColor = [UIColor blackColor];
_kaishiLab = [[UILabel alloc]initWithFrame:CGRectMake(65, 8, 70, 25)];
_kaishiLab.font = [UIFont systemFontOfSize:13];
_kaishiLab.textColor = [UIColor blackColor];
_kaishitimeLab = [[UILabel alloc]initWithFrame:CGRectMake(65, 30, 50, 20)];
_kaishitimeLab.font = [UIFont systemFontOfSize:11];
_kaishitimeLab.textColor = [UIColor grayColor];
_photoLab = [[UILabel alloc]initWithFrame:CGRectMake(210, 5, 50, 25)];
_photoLab.text = @"🇨🇳";
_jiantouLab = [[UILabel alloc]initWithFrame:CGRectMake(195, 20, 100, 10)];
_jiantouLab.text = @"------->";
_sumtimeLab = [[UILabel alloc]initWithFrame:CGRectMake(195, 29, 70, 15)];
_sumtimeLab.font = [UIFont systemFontOfSize:10];
_sumtimeLab.textColor = [UIColor grayColor];
_jieshuLab = [[UILabel alloc]initWithFrame:CGRectMake(280, 8, 100, 25)];
_jieshuLab.font = [UIFont systemFontOfSize:13];
_jieshuLab.textColor = [UIColor blackColor];
_jieshutimeLab = [[UILabel alloc]initWithFrame:CGRectMake(280, 30, 50, 20)];
_jieshutimeLab.font = [UIFont systemFontOfSize:11];
_jieshutimeLab.textColor = [UIColor grayColor];
_shangwuLab = [[UILabel alloc]initWithFrame:CGRectMake(8, 55, 60, 20)];
_shangwuLab.font = [UIFont systemFontOfSize:11];
_shangwuLab.textColor = [UIColor blackColor];
_OneLab = [[UILabel alloc]initWithFrame:CGRectMake(108, 55, 60, 20)];
_OneLab.font = [UIFont systemFontOfSize:11];
_OneLab.textColor = [UIColor blackColor];
_TwoLab = [[UILabel alloc]initWithFrame:CGRectMake(208, 55, 60, 20)];
_TwoLab.font = [UIFont systemFontOfSize:11];
_TwoLab.textColor = [UIColor blackColor];
_bottomBtn = [[UIButton alloc]initWithFrame:CGRectMake(364, 11, 50, 50)];
[_bottomBtn setImage:[UIImage imageNamed:@"xiangxiajiantou_down_9x9_"] forState:UIControlStateNormal];
_checiLab.text = @"G101";
_kaishiLab.text = @"🚋北京南";
_kaishitimeLab.text = @"06:43";
_sumtimeLab.text = @"05小时56分";
_jieshuLab.text = @"🏁上海虹桥";
_jieshutimeLab.text = @"12:39";
_shangwuLab.text = @"商务: 4张";
_OneLab.text = @"一等: 有";
_TwoLab.text = @"二等: 有";
[view addSubview:self.checiLab];
[view addSubview:self.kaishiLab];
[view addSubview:self.kaishitimeLab];
[view addSubview:self.photoLab];
[view addSubview:self.jiantouLab];
[view addSubview:self.sumtimeLab];
[view addSubview:self.jieshuLab];
[view addSubview:self.jieshutimeLab];
[view addSubview:self.shangwuLab];
[view addSubview:self.OneLab];
[view addSubview:self.TwoLab];
[view addSubview:self.bottomBtn];
UIView *vv = [[UIView alloc]initWithFrame:CGRectMake(0, 85, Screenwidth, 1)];
vv.backgroundColor = [UIColor lightGrayColor];
[view addSubview:vv];
return view;
}
/**
* cell收缩/展开 刷新
*
* @param view <#view description#>
*/
-(void)sectionClick:(UIControl *)view{
//获取点击的组
NSInteger i = view.tag - 1000;
//取反
close[i] = !close[i];
//刷新列表
NSIndexSet * index = [NSIndexSet indexSetWithIndex:i];
[_WBTableView reloadSections:index withRowAnimation:UITableViewRowAnimationAutomatic];
}
-(void)clickleft
{
[self.navigationController popViewControllerAnimated:YES];
}
-(void)clickright
{
NSLog(@"啥");
}
@end
shangViewController.m
#import "shangViewController.h"
@interface shangViewController ()
@end
@implementation shangViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0/250.0 green:137/250.0 blue:211/250.0 alpha:1];
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
dingViewController.m
#import "dingViewController.h"
@interface dingViewController ()
@end
@implementation dingViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0/250.0 green:137/250.0 blue:211/250.0 alpha:1];
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
woViewController.m
#import "woViewController.h"
@interface woViewController ()
@end
@implementation woViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0/250.0 green:137/250.0 blue:211/250.0 alpha:1];
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
ViewController.h
#import@interface ViewController : UITabBarController
@end
ViewController.m
#import "ViewController.h"
#import "cheViewController.h"
#import "shangViewController.h"
#import "dingViewController.h"
#import "woViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
cheViewController *che = [[cheViewController alloc]init];
che.title = @"车票预订";
UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:che];
nav1.tabBarItem.title = @"车票预订";
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, [UIFont fontWithName:@"Helvetica" size:12.0f],NSFontAttributeName,nil] forState:UIControlStateSelected];
[[UITabBar appearance] setBarTintColor:[UIColor blackColor]];
shangViewController *shang = [[shangViewController alloc]init];
shang.title = @"商旅查询";
UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:shang];
nav2.tabBarItem.title = @"商旅服务";
dingViewController *ding = [[dingViewController alloc]init];
ding.title = @"订单查询";
UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:ding];
nav3.tabBarItem.title = @"订单查询";
woViewController *wo = [[woViewController alloc]init];
wo.title = @"我的12306";
UINavigationController *nav4 = [[UINavigationController alloc]initWithRootViewController:wo];
nav4.tabBarItem.title = @"我的12306";
self.viewControllers = @[nav1,nav2,nav3,nav4];
}
@end