Skip to content

Commit

Permalink
[Releases 1.6.6] SGPageTitleViewConfigure 新增 equivalence 属性支持静止样式下标题从…
Browse files Browse the repository at this point in the history
…左到右自动布局
  • Loading branch information
kingsic committed Jul 22, 2019
1 parent 6950fb0 commit f1f4f00
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 102 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ SGPageContentCollectionView(内部由 UICollectionView 实现)


## Installation
* 1、CocoaPods 导入 pod 'SGPagingView', '~> 1.6.5'
* 1、CocoaPods 导入 pod 'SGPagingView', '~> 1.6.6'
* 2、下载、拖拽 “SGPagingView” 文件夹到工程中


Expand Down Expand Up @@ -178,6 +178,8 @@ b. 实现 SGPageContentScrollView 的 pageContentScrollView:index:代理方法

* 2019-07-20 :v1.6.5 滚动样式下 titleTextZoom 属性支持指示器下划线及遮盖样式下的滚动

* 2019-07-22 :v1.6.6 SGPageTitleViewConfigure 新增 equivalence 属性支持静止样式下标题从左到右自动布局


## License
SGPagingView is released under the MIT license. See [LICENSE](https://github.com/kingsic/SGPagingView/blob/master/LICENSE) for details.
2 changes: 1 addition & 1 deletion SGPagingView.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Pod::Spec.new do |s|
s.name = 'SGPagingView'
s.version = '1.6.5'
s.version = '1.6.6'
s.summary = 'A powerful and easy to use segment control'
s.homepage = 'https://github.com/kingsic/SGPagingView'
s.license = 'MIT'
Expand Down
189 changes: 95 additions & 94 deletions SGPagingView/SGPageTitle/SGPageTitleView.m
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ - (void)layoutSubviews {

// 布局子控制器的 frame
[self P_layoutSubviews];

// 选中按钮下标初始值
[self P_btn_action:self.btnMArr[_selectedIndex]];
}
Expand All @@ -124,69 +123,41 @@ - (void)P_layoutSubviews {
// 2、布局标题按钮的 frame
NSInteger titleCount = self.titleArr.count;
if (self.allBtnWidth <= self.bounds.size.width) { // SGPageTitleView 静止样式
CGFloat btnY = 0;
CGFloat btnW = self.frame.size.width / titleCount;
CGFloat btnH = 0;
if (self.configure.indicatorStyle == SGIndicatorStyleDefault) {
btnH = selfHeight - self.configure.indicatorHeight;
} else {
btnH = selfHeight;
}
[self.btnMArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
CGFloat btnX = btnW * idx;
UIButton *btn = obj;
btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
}];
self.scrollView.contentSize = CGSizeMake(self.frame.size.width, selfHeight);
// 2.2、布局标题间分割线的 frame
if (self.configure.showVerticalSeparator) {
CGFloat VSeparatorW = 1;
CGFloat VSeparatorH = selfHeight - self.configure.verticalSeparatorReduceHeight;
if (VSeparatorH <= 0) {
VSeparatorH = selfHeight;
if (self.configure.equivalence) {
CGFloat btnY = 0;
CGFloat btnW = self.frame.size.width / titleCount;
CGFloat btnH = 0;
if (self.configure.indicatorStyle == SGIndicatorStyleDefault) {
btnH = selfHeight - self.configure.indicatorHeight;
} else {
btnH = selfHeight;
}
CGFloat VSeparatorY = 0.5 * (selfHeight - VSeparatorH);
[self.VSeparatorMArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
CGFloat VSeparatorX = btnW * (idx + 1) - 0.5 * VSeparatorW;
UIView *VSeparator = obj;
VSeparator.frame = CGRectMake(VSeparatorX, VSeparatorY, VSeparatorW, VSeparatorH);
[self.btnMArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
CGFloat btnX = btnW * idx;
UIButton *btn = obj;
btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
}];
}

} else { // SGPageTitleView 滚动样式
__block CGFloat btnX = 0;
CGFloat btnY = 0;
CGFloat btnH = 0;
if (self.configure.indicatorStyle == SGIndicatorStyleDefault) {
btnH = selfHeight - self.configure.indicatorHeight;
} else {
btnH = selfHeight;
}
[self.btnMArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
CGSize tempSize = [self P_sizeWithString:self.titleArr[idx] font:self.configure.titleFont];
CGFloat btnW = tempSize.width + self.configure.titleAdditionalWidth;
UIButton *btn = obj;
btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
btnX = btnX + btnW;
}];
UIButton *lastBtn = self.btnMArr.lastObject;
CGFloat scrollViewWidth = CGRectGetMaxX(lastBtn.frame);
self.scrollView.contentSize = CGSizeMake(scrollViewWidth, selfHeight);
// 2.2、布局标题间分割线的 frame
if (self.configure.showVerticalSeparator) {
CGFloat VSeparatorW = 1;
CGFloat VSeparatorH = selfHeight - self.configure.verticalSeparatorReduceHeight;
if (VSeparatorH <= 0) {
VSeparatorH = selfHeight;
self.scrollView.contentSize = CGSizeMake(self.frame.size.width, selfHeight);
// 2.2、布局标题间分割线的 frame
if (self.configure.showVerticalSeparator) {
CGFloat VSeparatorW = 1;
CGFloat VSeparatorH = selfHeight - self.configure.verticalSeparatorReduceHeight;
if (VSeparatorH <= 0) {
VSeparatorH = selfHeight;
}
CGFloat VSeparatorY = 0.5 * (selfHeight - VSeparatorH);
[self.VSeparatorMArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
CGFloat VSeparatorX = btnW * (idx + 1) - 0.5 * VSeparatorW;
UIView *VSeparator = obj;
VSeparator.frame = CGRectMake(VSeparatorX, VSeparatorY, VSeparatorW, VSeparatorH);
}];
}
CGFloat VSeparatorY = 0.5 * (selfHeight - VSeparatorH);
[self.VSeparatorMArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
UIButton *btn = self.btnMArr[idx];
CGFloat VSeparatorX = CGRectGetMaxX(btn.frame) - 0.5 * VSeparatorW;
UIView *VSeparator = obj;
VSeparator.frame = CGRectMake(VSeparatorX, VSeparatorY, VSeparatorW, VSeparatorH);
}];

} else {
[self P_fromLeftToRightLayoutTitle];
}
} else { // SGPageTitleView 滚动样式
[self P_fromLeftToRightLayoutTitle];
}
// 3、布局底部分割线的 frame
if (self.configure.showBottomSeparator) {
Expand Down Expand Up @@ -224,11 +195,42 @@ - (void)P_layoutSubviews {
}
}
}
- (void)P_fromLeftToRightLayoutTitle {
CGFloat selfHeight = self.frame.size.height;

#pragma mark - - - 计算字符串尺寸
- (CGSize)P_sizeWithString:(NSString *)string font:(UIFont *)font {
NSDictionary *attrs = @{NSFontAttributeName : font};
return [string boundingRectWithSize:CGSizeMake(0, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
__block CGFloat btnX = 0;
CGFloat btnY = 0;
CGFloat btnH = 0;
if (self.configure.indicatorStyle == SGIndicatorStyleDefault) {
btnH = selfHeight - self.configure.indicatorHeight;
} else {
btnH = selfHeight;
}
[self.btnMArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
CGSize tempSize = [self P_sizeWithString:self.titleArr[idx] font:self.configure.titleFont];
CGFloat btnW = tempSize.width + self.configure.titleAdditionalWidth;
UIButton *btn = obj;
btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
btnX = btnX + btnW;
}];
UIButton *lastBtn = self.btnMArr.lastObject;
CGFloat scrollViewWidth = CGRectGetMaxX(lastBtn.frame);
self.scrollView.contentSize = CGSizeMake(scrollViewWidth, selfHeight);
// 2.2、布局标题间分割线的 frame
if (self.configure.showVerticalSeparator) {
CGFloat VSeparatorW = 1;
CGFloat VSeparatorH = selfHeight - self.configure.verticalSeparatorReduceHeight;
if (VSeparatorH <= 0) {
VSeparatorH = selfHeight;
}
CGFloat VSeparatorY = 0.5 * (selfHeight - VSeparatorH);
[self.VSeparatorMArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
UIButton *btn = self.btnMArr[idx];
CGFloat VSeparatorX = CGRectGetMaxX(btn.frame) - 0.5 * VSeparatorW;
UIView *VSeparator = obj;
VSeparator.frame = CGRectMake(VSeparatorX, VSeparatorY, VSeparatorW, VSeparatorH);
}];
}
}

#pragma mark - - - 懒加载
Expand Down Expand Up @@ -295,32 +297,18 @@ - (void)setupTitleButtons {
// 所有按钮文字宽度 + 所有按钮额外增加的宽度
self.allBtnWidth = self.allBtnTextWidth + self.configure.titleAdditionalWidth * titleCount;
self.allBtnWidth = ceilf(self.allBtnWidth);
if (self.allBtnWidth <= self.bounds.size.width) { // SGPageTitleView 静止样式
for (NSInteger index = 0; index < titleCount; index++) {
SGPageTitleButton *btn = [[SGPageTitleButton alloc] init];
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];
}

} else { // SGPageTitleView 滚动样式
for (NSInteger index = 0; index < titleCount; index++) {
// 1、添加按钮
SGPageTitleButton *btn = [[SGPageTitleButton alloc] init];
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];
}

// 创建标题按钮
for (NSInteger index = 0; index < titleCount; index++) {
SGPageTitleButton *btn = [[SGPageTitleButton alloc] init];
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];
}
// 标题文字渐变效果下对标题文字默认、选中状态下颜色的记录
if (self.configure.titleGradientEffect) {
Expand Down Expand Up @@ -472,7 +460,6 @@ - (void)P_changeIndicatorWithButton:(UIButton *)button {
self.indicatorView.SG_width = tempIndicatorWidth;
self.indicatorView.SG_centerX = button.SG_centerX;
}

}];
}

Expand All @@ -492,10 +479,18 @@ - (void)setPageTitleViewWithProgress:(CGFloat)progress originalIndex:(NSInteger)
// 3、处理指示器的逻辑
if (self.configure.showIndicator) {
if (self.allBtnWidth <= self.bounds.size.width) { /// SGPageTitleView 静止样式
if (self.configure.indicatorScrollStyle == SGIndicatorScrollStyleDefault) {
[self P_staticIndicatorScrollStyleDefaultWithProgress:progress originalBtn:originalBtn targetBtn:targetBtn];
if (self.configure.equivalence) {
if (self.configure.indicatorScrollStyle == SGIndicatorScrollStyleDefault) {
[self P_staticIndicatorScrollStyleDefaultWithProgress:progress originalBtn:originalBtn targetBtn:targetBtn];
} else {
[self P_staticIndicatorScrollStyleHalfEndWithProgress:progress originalBtn:originalBtn targetBtn:targetBtn];
}
} else {
[self P_staticIndicatorScrollStyleHalfEndWithProgress:progress originalBtn:originalBtn targetBtn:targetBtn];
if (self.configure.indicatorScrollStyle == SGIndicatorScrollStyleDefault) {
[self P_indicatorScrollStyleDefaultWithProgress:progress originalBtn:originalBtn targetBtn:targetBtn];
} else {
[self P_indicatorScrollStyleHalfEndWithProgress:progress originalBtn:originalBtn targetBtn:targetBtn];
}
}

} else { /// SGPageTitleView 可滚动
Expand Down Expand Up @@ -1138,6 +1133,12 @@ - (void)setResetSelectedIndex:(NSInteger)resetSelectedIndex {
[self P_btn_action:self.btnMArr[resetSelectedIndex]];
}

#pragma mark - - - 计算字符串尺寸
- (CGSize)P_sizeWithString:(NSString *)string font:(UIFont *)font {
NSDictionary *attrs = @{NSFontAttributeName : font};
return [string boundingRectWithSize:CGSizeMake(0, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}

#pragma mark - - - 颜色设置的计算
/// 开始颜色设置
- (void)setupStartColor:(UIColor *)color {
Expand Down
8 changes: 5 additions & 3 deletions SGPagingView/SGPageTitle/SGPageTitleViewConfigure.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ typedef enum : NSUInteger {
+ (instancetype)pageTitleViewConfigure;

#pragma mark - - SGPageTitleView 属性
/** SGPageTitleView 是否需要弹性效果,默认为 YES */
/** SGPageTitleView 是否需要弹性效果,默认为 NO */
@property (nonatomic, assign) BOOL bounces;
/** 是否显示底部分割线,默认为 YES */
/** SGPageTitleView 静止样式下标题是否均分布局,默认为 YES */
@property (nonatomic, assign) BOOL equivalence;
/** SGPageTitleView 是否显示底部分割线,默认为 YES */
@property (nonatomic, assign) BOOL showBottomSeparator;
/* SGPageTitleView 底部分割线颜色,默认为 lightGrayColor */
/** SGPageTitleView 底部分割线颜色,默认为 lightGrayColor */
@property (nonatomic, strong) UIColor *bottomSeparatorColor;

#pragma mark - - 标题属性
Expand Down
1 change: 1 addition & 0 deletions SGPagingView/SGPageTitle/SGPageTitleViewConfigure.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ - (instancetype)init {
- (void)initialization {
_showBottomSeparator = YES;
_showIndicator = YES;
_equivalence = YES;
}

+ (instancetype)pageTitleViewConfigure {
Expand Down
2 changes: 1 addition & 1 deletion SGPagingView/SGPagingView.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// SGPagingView.h
// Version 1.6.5
// Version 1.6.6
// GitHub:https://github.com/kingsic/SGPagingView
//
// Created by kingsic on 2016/10/6.
Expand Down
13 changes: 13 additions & 0 deletions SGPagingViewExample/MainVC/DefaultSystemVC.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ - (void)setupSystem {
}

- (void)setupPageView {
NSArray *titleArr = @[@"精选", @"电影"];
SGPageTitleViewConfigure *configure = [SGPageTitleViewConfigure pageTitleViewConfigure];
configure.indicatorStyle = SGIndicatorStyleCover;
configure.indicatorCornerRadius = 15;
configure.indicatorHeight = 30;
configure.indicatorAdditionalWidth = 120;
configure.titleSelectedColor = [UIColor whiteColor];
/// pageTitleView
SGPageTitleView *pageTitleView = [SGPageTitleView pageTitleViewWithFrame:CGRectMake(0, 0, 150, 30) delegate:self titleNames:titleArr configure:configure];
pageTitleView.layer.cornerRadius = 15;
pageTitleView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2];
self.navigationItem.titleView = pageTitleView;

NSArray *titleArrS = @[@"精选", @"电影", @"OC", @"Swift"];
SGPageTitleViewConfigure *configureS = [SGPageTitleViewConfigure pageTitleViewConfigure];
configureS.indicatorAdditionalWidth = 20;
Expand Down
4 changes: 3 additions & 1 deletion SGPagingViewExample/MainVC/DefaultTwoCoverVC.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ - (void)setupPageView {
configure.indicatorBorderColor = [UIColor lightGrayColor];
configure.indicatorCornerRadius = 20;
configure.indicatorHeight = 25;
configure.equivalence = NO;
configure.titleAdditionalWidth = 50;

/// pageTitleView
self.pageTitleView = [SGPageTitleView pageTitleViewWithFrame:CGRectMake(0, pageTitleViewY, self.view.frame.size.width / 2, 44) delegate:self titleNames:titleArr configure:configure];
self.pageTitleView = [SGPageTitleView pageTitleViewWithFrame:CGRectMake(0, pageTitleViewY, self.view.frame.size.width, 44) delegate:self titleNames:titleArr configure:configure];
[self.view addSubview:_pageTitleView];

ChildVCOne *oneVC = [[ChildVCOne alloc] init];
Expand Down
2 changes: 1 addition & 1 deletion SGPagingViewExample/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

self.TitleDataList = @[@"静止样式", @"滚动样式", @"系统样式", @"图片样式", @"文字渐变效果", @"文字缩放效果", @"指示器固定样式", @"指示器动态样式", @"指示器遮盖样式一", @"指示器遮盖样式二", @"指示器遮盖样式三", @"侧滑返回手势案例", @"滚动内容动画案例", @"AttributedTitle 属性案例", @"导航栏样式案例"];
self.TitleDataList = @[@"静止样式", @"滚动样式", @"系统样式", @"图片样式", @"文字渐变效果", @"文字缩放效果", @"指示器固定样式", @"指示器动态样式", @"指示器遮盖样式一", @"指示器遮盖样式二(从左到右自动布局)", @"指示器遮盖样式三", @"侧滑返回手势案例", @"滚动内容动画案例", @"AttributedTitle 属性案例", @"导航栏样式案例"];

[self foundTableView];
}
Expand Down

0 comments on commit f1f4f00

Please sign in to comment.