diff --git a/README.md b/README.md index 1462175..903648b 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,6 @@ * QQ群:825339547 -* 1.6.0 开始支持 autolayout 创建 - * [SGTagsView 设计来源于本框架中的 SGPageTitleView](https://github.com/kingsic/SGCrayfish) @@ -53,7 +51,7 @@ SGPageContentCollectionView(内部由 UICollectionView 实现) ## Installation -* 1、CocoaPods 导入 pod 'SGPagingView', '~> 1.6.7' +* 1、CocoaPods 导入 pod 'SGPagingView', '~> 1.6.9' * 2、下载、拖拽 “SGPagingView” 文件夹到工程中 @@ -154,37 +152,35 @@ b. 实现 SGPageContentScrollView 的 pageContentScrollView:index:代理方法 * 2016-10-07 :初始版本的创建 -* 2017-04-13 :版本升级(根据标题内容自动识别 SGPageTitleView 是静止还是滚动样式) - -* 2017-07-21 :v1.1.7 加入 CocoaPods 管理以及新增 SGPageContentScrollView 类 +* 2017-07-21 :v1.1.7 加入 CocoaPods 管理及新增 SGPageContentScrollView 类 -* 2017-10-17 :v1.3.0 版本升级(新增 SGPageTitleViewConfigure 类并提供更多属性设置以及支持指示器遮盖样式) +* 2017-10-17 :v1.3.0 版本升级:新增 SGPageTitleViewConfigure 类及支持指示器遮盖样式 -* 2018-05-08 :v1.3.7 修复 1.3.6 版本选中标题重复点击恢复默认状态以及 SGPageTitleViewConfigure 新增配置属性 +* 2018-05-08 :v1.3.7 修复 1.3.6 版本选中标题重复点击恢复默认状态及新增相关配置属性 -* 2018-07-09 :v1.5.0 版本升级(具体相关信息请查看 [releases](https://github.com/kingsic/SGPagingView/releases) 中版本介绍) +* 2018-07-09 :v1.5.0 版本升级:具体相关信息请查看 [releases](https://github.com/kingsic/SGPagingView/releases) 中版本介绍 * 2018-08-28 :v1.5.2 SGPageContentScrollView 内部代码优化处理(感谢 [petyou](https://github.com/petyou) 提供的优化方案) -* 2018-09-01 :v1.5.3 SGPageTitleView 内部代码优化以及新增 SGPagingViewPopGestureVC 用于解决侧滑返回手势 +* 2018-09-01 :v1.5.3 新增 SGPagingViewPopGestureVC 用于解决侧滑返回手势 * 2018-09-26 :v1.5.5 标题文字缩放效果由文字缩放调整为控件缩放 * 2018-12-01 :v1.5.6 SGPageTitleView 新增重置标题、指示器颜色方法 -* 2019-01-09 :v1.6.0 支持 autolayout 创建 +* 2019-01-09 :v1.6.0 版本升级:支持 autolayout 布局 -* 2019-03-27 :v1.6.1 修复 1.6.0 版本 addBadgeForIndex 方法内 badge 布局问题 +* 2019-03-27 :v1.6.1 修复 addBadgeForIndex 方法内 badge 布局问题 -* 2019-07-17 :v1.6.3 修复设置图片样式下图片布局问题以及对内部代码的优化处理 +* 2019-07-17 :v1.6.3 修复设置图片样式下图片布局问题及内部代码优化处理 -* 2019-07-20 :v1.6.5 滚动样式下 titleTextZoom 属性支持指示器下划线及遮盖样式下的滚动 +* 2019-07-20 :v1.6.5 滚动样式下 titleTextZoom 属性支持指示器下划线及遮盖样式的滚动 * 2019-07-22 :v1.6.6 SGPageTitleViewConfigure 新增 equivalence 属性支持静止样式下标题从左到右自动布局 -* 2019-07-27 :v1.6.7 支持标题文字上下行样式以及 titleGradientEffect 属性与 resetTitleColor:titleSelectedColor: 方法的兼容 +* 2019-07-27 :v1.6.7 标题支持上下行及 titleGradientEffect 属性与 resetTitleColor:titleSelectedColor: 方法兼容 -* 2019-07-27 :v1.6.8 SGPageTitleViewConfigure 添加 bounce 属性 +* 2020-11-01 :v1.6.9 Badge 新增配置属性,支持网络图片,修复无指示器滚动内容视图时标题文字不切换 ## License diff --git a/SGPagingView.podspec b/SGPagingView.podspec index 864ae2c..2485afd 100644 --- a/SGPagingView.podspec +++ b/SGPagingView.podspec @@ -1,8 +1,8 @@ Pod::Spec.new do |s| s.name = 'SGPagingView' - s.version = '1.6.8' - s.summary = 'A powerful and easy to use segment control' + s.version = '1.6.9' + s.summary = 'A powerful and easy to use segment view' s.homepage = 'https://github.com/kingsic/SGPagingView' s.license = 'MIT' s.authors = {'kingsic' => 'kingsic@126.com'} diff --git a/SGPagingView/Category/UIButton+SGPagingView.h b/SGPagingView/Category/UIButton+SGPagingView.h index 3b6e743..38319d2 100755 --- a/SGPagingView/Category/UIButton+SGPagingView.h +++ b/SGPagingView/Category/UIButton+SGPagingView.h @@ -19,6 +19,8 @@ typedef enum : NSUInteger { SGImagePositionStyleBottom, } SGImagePositionStyle; +typedef void(^LoadImageCompleteBlock)(UIImage *image); + @interface UIButton (SGPagingView) /** * 设置图片与文字样式 @@ -29,4 +31,7 @@ typedef enum : NSUInteger { */ - (void)SG_imagePositionStyle:(SGImagePositionStyle)imagePositionStyle spacing:(CGFloat)spacing imagePositionBlock:(void (^)(UIButton *button))imagePositionBlock; +/** 加载网络图片 */ +- (void)SG_loadImageWithUrlString:(NSString *)urlString complete:(LoadImageCompleteBlock)complete; + @end diff --git a/SGPagingView/Category/UIButton+SGPagingView.m b/SGPagingView/Category/UIButton+SGPagingView.m index b48b628..9b6469b 100755 --- a/SGPagingView/Category/UIButton+SGPagingView.m +++ b/SGPagingView/Category/UIButton+SGPagingView.m @@ -61,4 +61,19 @@ - (void)SG_imagePositionStyle:(SGImagePositionStyle)imagePositionStyle spacing:( } } +/** 加载网络图片 */ +- (void)SG_loadImageWithUrlString:(NSString *)urlString complete:(LoadImageCompleteBlock)complete { + NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{ + NSURL *url = [NSURL URLWithString:urlString]; + NSData *imageData = [NSData dataWithContentsOfURL:url]; + UIImage *image = [UIImage imageWithData:imageData]; + [[NSOperationQueue mainQueue] addOperationWithBlock:^{ + if (complete) { + complete(image); + } + }]; + }]; + [[[NSOperationQueue alloc] init] addOperation:blockOperation]; +} + @end diff --git a/SGPagingView/SGPageContent/SGPageContentCollectionView.h b/SGPagingView/SGPageContent/SGPageContentCollectionView.h old mode 100755 new mode 100644 index dad32f3..9ec7cb1 --- a/SGPagingView/SGPageContent/SGPageContentCollectionView.h +++ b/SGPagingView/SGPageContent/SGPageContentCollectionView.h @@ -12,7 +12,7 @@ @protocol SGPageContentCollectionViewDelegate @optional /** - * 联动 SGPageTitleView 的方法 + * 联动 SGPageTitleView 的 setPageTitleViewWithProgress:originalIndex:targetIndex: 方法使用 * * @param pageContentCollectionView SGPageContentCollectionView * @param progress SGPageContentCollectionView 内部视图滚动时的偏移量 @@ -58,7 +58,7 @@ /** 点击标题触发动画切换滚动内容,默认为 NO */ @property (nonatomic, assign) BOOL isAnimated; -/** 给外界提供的方法,根据 SGPageTitleView 标题选中时的下标并显示相应的子控制器 */ +/** 联动 SGPageTitleView 的代理方法 pageTitleView:selectedIndex: 一起使用 */ - (void)setPageContentCollectionViewCurrentIndex:(NSInteger)currentIndex; @end diff --git a/SGPagingView/SGPageContent/SGPageContentCollectionView.m b/SGPagingView/SGPageContent/SGPageContentCollectionView.m old mode 100755 new mode 100644 diff --git a/SGPagingView/SGPageContent/SGPageContentScrollView.h b/SGPagingView/SGPageContent/SGPageContentScrollView.h old mode 100755 new mode 100644 index db90238..5e50cde --- a/SGPagingView/SGPageContent/SGPageContentScrollView.h +++ b/SGPagingView/SGPageContent/SGPageContentScrollView.h @@ -12,7 +12,7 @@ @protocol SGPageContentScrollViewDelegate @optional /** - * 联动 SGPageTitleView 的方法 + * 联动 SGPageTitleView 的 setPageTitleViewWithProgress:originalIndex:targetIndex: 方法使用 * * @param pageContentScrollView SGPageContentScrollView * @param progress SGPageContentScrollView 内部视图滚动时的偏移量 @@ -58,7 +58,7 @@ /** 点击标题触发动画切换滚动内容,默认为 NO */ @property (nonatomic, assign) BOOL isAnimated; -/** 给外界提供的方法,根据 SGPageTitleView 标题选中时的下标并显示相应的子控制器 */ +/** 联动 SGPageTitleView 的代理方法 pageTitleView:selectedIndex: 一起使用 */ - (void)setPageContentScrollViewCurrentIndex:(NSInteger)currentIndex; @end diff --git a/SGPagingView/SGPageContent/SGPageContentScrollView.m b/SGPagingView/SGPageContent/SGPageContentScrollView.m old mode 100755 new mode 100644 diff --git a/SGPagingView/SGPageTitle/SGPageTitleView.h b/SGPagingView/SGPageTitle/SGPageTitleView.h old mode 100755 new mode 100644 index 46e0f5c..8c9a8f6 --- a/SGPagingView/SGPageTitle/SGPageTitleView.h +++ b/SGPagingView/SGPageTitle/SGPageTitleView.h @@ -22,7 +22,7 @@ typedef enum : NSUInteger { @protocol SGPageTitleViewDelegate /** - * 联动 pageContent 的方法 + * 联动 PageContent 的 setPageContentViewCurrentIndex: 方法使用 * * @param pageTitleView SGPageTitleView * @param selectedIndex 选中按钮的下标 @@ -34,23 +34,16 @@ typedef enum : NSUInteger { /** * 对象方法创建 SGPageTitleView * - * @param frame frame - * @param delegate delegate - * @param titleNames 标题数组 + * @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 信息配置 - */ +/** 类方法创建 SGPageTitleView */ + (instancetype)pageTitleViewWithFrame:(CGRect)frame delegate:(id)delegate titleNames:(NSArray *)titleNames configure:(SGPageTitleViewConfigure *)configure; -/** 给外界提供的方法,获取 PageContent 的 progress/originalIndex/targetIndex, 必须实现 */ +/** 联动 PageContent 的代理方法 pageContentScrollView:progress:originalIndex:targetIndex: 一起使用 */ - (void)setPageTitleViewWithProgress:(CGFloat)progress originalIndex:(NSInteger)originalIndex targetIndex:(NSInteger)targetIndex; /** 选中标题按钮下标,默认为 0 */ @@ -58,40 +51,35 @@ typedef enum : NSUInteger { /** 重置选中标题按钮下标(用于子控制器内的点击事件改变标题的选中下标)*/ @property (nonatomic, assign) NSInteger resetSelectedIndex; -/** 根据标题下标值添加 badge */ -- (void)addBadgeForIndex:(NSInteger)index; -/** 根据标题下标值移除 badge */ -- (void)removeBadgeForIndex:(NSInteger)index; - /** 根据标题下标值重置标题文字 */ - (void)resetTitle:(NSString *)title forIndex:(NSInteger)index; -/** 重置指示器颜色 */ -- (void)resetIndicatorColor:(UIColor *)color; -/** 重置标题普通状态、选中状态下文字颜色 */ -- (void)resetTitleColor:(UIColor *)color titleSelectedColor:(UIColor *)selectedColor; -/** 重置标题普通状态、选中状态下文字颜色及指示器颜色方法 */ -- (void)resetTitleColor:(UIColor *)color titleSelectedColor:(UIColor *)selectedColor indicatorColor:(UIColor *)indicatorColor; - /** 根据标题下标值设置标题的 attributedTitle 属性 */ - (void)setAttributedTitle:(NSMutableAttributedString *)attributedTitle selectedAttributedTitle:(NSMutableAttributedString *)selectedAttributedTitle forIndex:(NSInteger)index; + /** * 设置标题图片及位置样式 * - * @param images 默认图片名数组 - * @param selectedImages 选中图片名数组 - * @param imagePositionType 图片位置样式 - * @param spacing 图片与标题文字之间的间距 + * @param images 默认图片名数组 + * @param selectedImages 选中图片名数组 + * @param imagePositionType 图片位置样式 + * @param spacing 图片与标题文字之间的间距 */ - (void)setImages:(NSArray *)images selectedImages:(NSArray *)selectedImages imagePositionType:(SGImagePositionType)imagePositionType spacing:(CGFloat)spacing; -/** - * 根据标题下标设置标题图片及位置样式 - * - * @param image 默认图片名 - * @param selectedImage 选中图片名 - * @param imagePositionType 图片位置样式 - * @param spacing 图片与标题文字之间的间距 - * @param index 标题对应下标值 - */ +/** 根据标题下标设置标题图片及位置样式 */ - (void)setImage:(NSString *)image selectedImage:(NSString *)selectedImage imagePositionType:(SGImagePositionType)imagePositionType spacing:(CGFloat)spacing forIndex:(NSInteger)index; +/** 根据标题下标值添加 badge */ +- (void)addBadgeForIndex:(NSInteger)index; +/** 根据标题下标值添加 badge */ +- (void)addBadgeWithText:(NSString *)text forIndex:(NSInteger)index; +/** 根据标题下标值移除 badge */ +- (void)removeBadgeForIndex:(NSInteger)index; + +/** 重置指示器颜色 */ +- (void)resetIndicatorColor:(UIColor *)color; +/** 重置标题普通状态、选中状态下文字颜色 */ +- (void)resetTitleColor:(UIColor *)color titleSelectedColor:(UIColor *)selectedColor; +/** 重置标题普通状态、选中状态下文字颜色及指示器颜色 */ +- (void)resetTitleColor:(UIColor *)color titleSelectedColor:(UIColor *)selectedColor indicatorColor:(UIColor *)indicatorColor; + @end diff --git a/SGPagingView/SGPageTitle/SGPageTitleView.m b/SGPagingView/SGPageTitle/SGPageTitleView.m old mode 100755 new mode 100644 index 646238c..5d05faa --- a/SGPagingView/SGPageTitle/SGPageTitleView.m +++ b/SGPagingView/SGPageTitle/SGPageTitleView.m @@ -58,6 +58,7 @@ @interface SGPageTitleView () @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]; @@ -469,7 +470,7 @@ - (void)P_changeIndicatorWithButton:(UIButton *)button { }]; } -#pragma mark - - - 给外界提供的方法 +#pragma mark - - - 给 SGPageContentView 的代理方法 pageContentScrollView:progress:originalIndex:targetIndex: 使用 - (void)setPageTitleViewWithProgress:(CGFloat)progress originalIndex:(NSInteger)originalIndex targetIndex:(NSInteger)targetIndex { // 1、取出 originalBtn、targetBtn UIButton *originalBtn = self.btnMArr[originalIndex]; @@ -506,6 +507,12 @@ - (void)setPageTitleViewWithProgress:(CGFloat)progress originalIndex:(NSInteger) [self P_indicatorScrollStyleHalfEndWithProgress:progress originalBtn:originalBtn targetBtn:targetBtn]; } } + } else { + if (self.configure.indicatorScrollStyle == SGIndicatorScrollStyleHalf) { + [self P_indicatorScrollStyleHalfWithProgress:progress originalBtn:originalBtn targetBtn:targetBtn]; + } else { + [self P_indicatorScrollStyleOtherWithProgress:progress originalBtn:originalBtn targetBtn:targetBtn]; + } } // 4、颜色的渐变(复杂) if (self.configure.titleGradientEffect) { @@ -527,210 +534,26 @@ - (void)setPageTitleViewWithProgress:(CGFloat)progress originalIndex:(NSInteger) }; } -/** 根据下标值添加 badge */ -- (void)addBadgeForIndex:(NSInteger)index { - /// 这里使用GCD延迟函数的目的:是将 addBadgeForIndex 方法内部的 badge 布局在 layoutSubviews 之后调用,这里的 badge 是添加在标题(按钮),badge 的布局也可在 layoutSubviews 中在标题(按钮)布局之后布局。这里采取了GCD延迟函数 - dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.02 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ - UIButton *btn = self.btnMArr[index]; - UIView *badge = [[UIView alloc] init]; - CGFloat btnTextWidth = [self P_sizeWithString:btn.currentTitle font:self.configure.titleFont].width; - CGFloat btnTextHeight = [self P_sizeWithString:btn.currentTitle font:self.configure.titleFont].height; - CGFloat badgeX = 0.5 * (btn.SG_width - btnTextWidth) + btnTextWidth + self.configure.badgeOff.x; - CGFloat badgeY = 0.5 * (btn.SG_height - btnTextHeight) + self.configure.badgeOff.y - self.configure.badgeSize; - CGFloat badgeWidth = self.configure.badgeSize; - CGFloat badgeHeight = badgeWidth; - badge.frame = CGRectMake(badgeX, badgeY, badgeWidth, badgeHeight); - badge.layer.backgroundColor = self.configure.badgeColor.CGColor; - badge.layer.cornerRadius = 0.5 * self.configure.badgeSize; -// badge.tag = 2018 + index; - [btn addSubview:badge]; - }); -} -/** 根据下标值移除 badge */ -- (void)removeBadgeForIndex:(NSInteger)index { - UIButton *btn = self.btnMArr[index]; - [btn.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { -// if (obj.tag != 0) { - /// 之前使用 tag 进行 btn 内部子视图区分(UIImageView、UIButtonLabel的 tag 值都是 0) - /// isMemberOfClass 这个方法明确某个类 - if ([obj isMemberOfClass:[UIView class]]) { - [obj removeFromSuperview]; - obj = nil; - } - }]; -} - -/** - * 根据标题下标值重置标题文字 - * - * @param title 标题名 - * @param index 标题所对应的下标 - */ -- (void)resetTitle:(NSString *)title forIndex:(NSInteger)index { - UIButton *button = (UIButton *)self.btnMArr[index]; - [button setTitle:title forState:UIControlStateNormal]; - - if (self.configure.showIndicator && _signBtnIndex == index) { - if (self.configure.indicatorStyle == SGIndicatorStyleDefault || self.configure.indicatorStyle == SGIndicatorStyleCover) { - CGSize tempSize = [self P_sizeWithString:button.currentTitle font:self.configure.titleFont]; - CGFloat tempIndicatorWidth = self.configure.indicatorAdditionalWidth + tempSize.width; - if (tempIndicatorWidth > button.SG_width) { - tempIndicatorWidth = button.SG_width; - } - _indicatorView.SG_width = tempIndicatorWidth; - _indicatorView.SG_centerX = button.SG_centerX; - } - } -} - -/** 重置指示器颜色方法 */ -- (void)resetIndicatorColor:(UIColor *)color { - _indicatorView.backgroundColor = color; -} -/** - * 重置标题普通状态、选中状态下文字颜色方法 - * - * @param color 普通状态下标题文字颜色 - * @param selectedColor 选中状态下标题文字颜色 - */ -- (void)resetTitleColor:(UIColor *)color titleSelectedColor:(UIColor *)selectedColor { - [self.btnMArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { - UIButton *btn = obj; - [btn setTitleColor:color forState:(UIControlStateNormal)]; - [btn setTitleColor:selectedColor forState:(UIControlStateSelected)]; - }]; - - if (self.configure.titleGradientEffect) { - self.configure.titleColor = color; - self.configure.titleSelectedColor = selectedColor; - [self setupStartColor:self.configure.titleColor]; - [self setupEndColor:self.configure.titleSelectedColor]; - } -} -/** - * 重置标题普通状态、选中状态下文字颜色及指示器颜色方法 - * - * @param color 普通状态下标题文字颜色 - * @param selectedColor 选中状态下标题文字颜色 - * @param indicatorColor 指示器颜色 - */ -- (void)resetTitleColor:(UIColor *)color titleSelectedColor:(UIColor *)selectedColor indicatorColor:(UIColor *)indicatorColor { - [self resetTitleColor:color titleSelectedColor:selectedColor]; - [self resetIndicatorColor:indicatorColor]; -} - -/** - * 根据标题下标值设置标题的 attributedTitle 属性 - * - * @param attributedTitle attributedTitle 属性 - * @param selectedAttributedTitle 选中状态下 attributedTitle 属性 - * @param index 标题所对应的下标 - */ -- (void)setAttributedTitle:(NSMutableAttributedString *)attributedTitle selectedAttributedTitle:(NSMutableAttributedString *)selectedAttributedTitle forIndex:(NSInteger)index { - UIButton *button = (UIButton *)self.btnMArr[index]; - [button setAttributedTitle:attributedTitle forState:(UIControlStateNormal)]; - [button setAttributedTitle:selectedAttributedTitle forState:(UIControlStateSelected)]; -} - -/** - * 设置标题图片及位置样式 - * - * @param images 默认图片名数组 - * @param selectedImages 选中图片名数组 - * @param imagePositionType 图片位置样式 - * @param spacing 图片与标题文字之间的间距 - */ -- (void)setImages:(NSArray *)images selectedImages:(NSArray *)selectedImages imagePositionType:(SGImagePositionType)imagePositionType spacing:(CGFloat)spacing { - dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.02 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ - NSInteger imagesCount = images.count; - NSInteger selectedImagesCount = selectedImages.count; - NSInteger titlesCount = self.titleArr.count; - if (imagesCount < selectedImagesCount) { - NSLog(@"温馨提示:SGPageTitleView -> [setImages:selectedImages:imagePositionType:spacing] 方法中 images 必须大于或者等于selectedImages,否则 imagePositionTypeDefault 以外的其他样式图片及文字布局将会出现问题"); - } - - if (imagesCount < titlesCount) { - [self.btnMArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { - UIButton *btn = obj; - if (idx >= imagesCount - 1) { - *stop = YES; - } - [self P_btn:btn imageName:images[idx] imagePositionType:imagePositionType spacing:spacing btnControlState:(UIControlStateNormal)]; - }]; - } else { - [self.btnMArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { - UIButton *btn = obj; - [self P_btn:btn imageName:images[idx] imagePositionType:imagePositionType spacing:spacing btnControlState:(UIControlStateNormal)]; - }]; - } - - if (selectedImagesCount < titlesCount) { - [self.btnMArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { - UIButton *btn = obj; - if (idx >= selectedImagesCount - 1) { - *stop = YES; - } - [self P_btn:btn imageName:selectedImages[idx] imagePositionType:imagePositionType spacing:spacing btnControlState:(UIControlStateSelected)]; - }]; - } else { - [self.btnMArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { - UIButton *btn = obj; - [self P_btn:btn imageName:selectedImages[idx] imagePositionType:imagePositionType spacing:spacing btnControlState:(UIControlStateSelected)]; - }]; - } - }); -} -/** - * 根据标题下标设置标题图片及位置样式 - * - * @param image 默认图片名 - * @param selectedImage 选中时图片名 - * @param imagePositionType 图片位置样式 - * @param spacing 图片与标题文字之间的间距 - * @param index 标题对应下标值 - */ -- (void)setImage:(NSString *)image selectedImage:(NSString *)selectedImage imagePositionType:(SGImagePositionType)imagePositionType spacing:(CGFloat)spacing forIndex:(NSInteger)index { - dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.02 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ - UIFont *configureTitleFont = self.configure.titleFont; - UIFont *configureTitleSelectedFont = self.configure.titleSelectedFont; - if ([configureTitleFont.fontName isEqualToString:configureTitleSelectedFont.fontName] && configureTitleFont.pointSize == configureTitleSelectedFont.pointSize) { - UIButton *btn = self.btnMArr[index]; - if (image != nil) { - [self P_btn:btn imageName:image imagePositionType:imagePositionType spacing:spacing btnControlState:(UIControlStateNormal)]; - } - if (selectedImage != nil) { - [self P_btn:btn imageName:selectedImage imagePositionType:imagePositionType spacing:spacing btnControlState:(UIControlStateSelected)]; - } - return; - } - - NSLog(@"配置属性 titleFont 必须与配置属性 titleSelectedFont 一致,否则 setImage:selectedImage:imagePositionType:spacing:forIndex 方法将不起任何作用"); - }); -} - -/// imagePositionType 样式设置方法抽取 -- (void)P_btn:(UIButton *)btn imageName:(NSString *)imageName imagePositionType:(SGImagePositionType)imagePositionType spacing:(CGFloat)spacing btnControlState:(UIControlState)btnControlState { - if (imagePositionType == SGImagePositionTypeDefault) { - [btn SG_imagePositionStyle:SGImagePositionStyleDefault spacing:spacing imagePositionBlock:^(UIButton *button) { - [btn setImage:[UIImage imageNamed:imageName] forState:btnControlState]; +#pragma mark - - - 隐藏指示器时,处理标题颜色改变 +- (void)P_indicatorScrollStyleHalfWithProgress:(CGFloat)progress originalBtn:(UIButton *)originalBtn targetBtn:(UIButton *)targetBtn { + if (progress >= 0.5) { + [UIView animateWithDuration:self.configure.indicatorAnimationTime animations:^{ + [self P_changeSelectedButton:targetBtn]; }]; - return; - } - if (imagePositionType == SGImagePositionTypeRight) { - [btn SG_imagePositionStyle:SGImagePositionStyleRight spacing:spacing imagePositionBlock:^(UIButton *button) { - [btn setImage:[UIImage imageNamed:imageName] forState:btnControlState]; + } else { + [UIView animateWithDuration:self.configure.indicatorAnimationTime animations:^{ + [self P_changeSelectedButton:originalBtn]; }]; - return; } - if (imagePositionType == SGImagePositionTypeTop) { - [btn SG_imagePositionStyle:SGImagePositionStyleTop spacing:spacing imagePositionBlock:^(UIButton *button) { - [btn setImage:[UIImage imageNamed:imageName] forState:btnControlState]; +} +- (void)P_indicatorScrollStyleOtherWithProgress:(CGFloat)progress originalBtn:(UIButton *)originalBtn targetBtn:(UIButton *)targetBtn { + if (progress == 1.0) { + [UIView animateWithDuration:self.configure.indicatorAnimationTime animations:^{ + [self P_changeSelectedButton:targetBtn]; }]; - return; - } - if (imagePositionType == SGImagePositionTypeBottom) { - [btn SG_imagePositionStyle:SGImagePositionStyleBottom spacing:spacing imagePositionBlock:^(UIButton *button) { - [btn setImage:[UIImage imageNamed:imageName] forState:btnControlState]; + } else { + [UIView animateWithDuration:self.configure.indicatorAnimationTime animations:^{ + [self P_changeSelectedButton:originalBtn]; }]; } } @@ -1140,18 +963,6 @@ - (void)P_isTitleGradientEffectWithProgress:(CGFloat)progress originalBtn:(UIBut targetBtn.titleLabel.textColor = targetColor; } -#pragma mark - - - set -- (void)setResetSelectedIndex:(NSInteger)resetSelectedIndex { - _resetSelectedIndex = 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 { @@ -1189,4 +1000,281 @@ - (void)P_getRGBComponents:(CGFloat [3])components forColor:(UIColor *)color { } } +#pragma mark - - - - - - - >> 给外界提供的相关属性及方法的实现 BEGIN<< - - - - - - - +#pragma mark - - - set +- (void)setResetSelectedIndex:(NSInteger)resetSelectedIndex { + _resetSelectedIndex = resetSelectedIndex; + [self P_btn_action:self.btnMArr[resetSelectedIndex]]; +} + +#pragma mark - - - 设置标题相关方法的实现 +/** + * 根据标题下标值重置标题文字 + * + * @param title 标题名 + * @param index 标题所对应的下标 + */ +- (void)resetTitle:(NSString *)title forIndex:(NSInteger)index { + UIButton *button = (UIButton *)self.btnMArr[index]; + [button setTitle:title forState:UIControlStateNormal]; + + if (self.configure.showIndicator && _signBtnIndex == index) { + if (self.configure.indicatorStyle == SGIndicatorStyleDefault || self.configure.indicatorStyle == SGIndicatorStyleCover) { + CGSize tempSize = [self P_sizeWithString:button.currentTitle font:self.configure.titleFont]; + CGFloat tempIndicatorWidth = self.configure.indicatorAdditionalWidth + tempSize.width; + if (tempIndicatorWidth > button.SG_width) { + tempIndicatorWidth = button.SG_width; + } + _indicatorView.SG_width = tempIndicatorWidth; + _indicatorView.SG_centerX = button.SG_centerX; + } + } +} +/** + * 根据标题下标值设置标题的 attributedTitle 属性 + * + * @param attributedTitle attributedTitle 属性 + * @param selectedAttributedTitle 选中状态下 attributedTitle 属性 + * @param index 标题所对应的下标 + */ +- (void)setAttributedTitle:(NSMutableAttributedString *)attributedTitle selectedAttributedTitle:(NSMutableAttributedString *)selectedAttributedTitle forIndex:(NSInteger)index { + UIButton *button = (UIButton *)self.btnMArr[index]; + [button setAttributedTitle:attributedTitle forState:(UIControlStateNormal)]; + [button setAttributedTitle:selectedAttributedTitle forState:(UIControlStateSelected)]; +} + + +#pragma mark - - - 设置标题图片及位置样式相关方法的实现 +/** + * 设置标题图片及位置样式 + * + * @param images 默认图片名数组 + * @param selectedImages 选中图片名数组 + * @param imagePositionType 图片位置样式 + * @param spacing 图片与标题文字之间的间距 + */ +- (void)setImages:(NSArray *)images selectedImages:(NSArray *)selectedImages imagePositionType:(SGImagePositionType)imagePositionType spacing:(CGFloat)spacing { + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.02 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + NSInteger imagesCount = images.count; + NSInteger selectedImagesCount = selectedImages.count; + NSInteger titlesCount = self.titleArr.count; + if (imagesCount < selectedImagesCount) { + NSLog(@"温馨提示:SGPageTitleView -> [setImages:selectedImages:imagePositionType:spacing] 方法中 images 必须大于或者等于selectedImages,否则 imagePositionTypeDefault 以外的其他样式图片及文字布局将会出现问题"); + } + + if (imagesCount < titlesCount) { + [self.btnMArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { + UIButton *btn = obj; + if (idx >= imagesCount - 1) { + *stop = YES; + } + [self P_btn:btn imageName:images[idx] imagePositionType:imagePositionType spacing:spacing btnControlState:(UIControlStateNormal)]; + }]; + } else { + [self.btnMArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { + UIButton *btn = obj; + [self P_btn:btn imageName:images[idx] imagePositionType:imagePositionType spacing:spacing btnControlState:(UIControlStateNormal)]; + }]; + } + + if (selectedImagesCount < titlesCount) { + [self.btnMArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { + UIButton *btn = obj; + if (idx >= selectedImagesCount - 1) { + *stop = YES; + } + [self P_btn:btn imageName:selectedImages[idx] imagePositionType:imagePositionType spacing:spacing btnControlState:(UIControlStateSelected)]; + }]; + } else { + [self.btnMArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { + UIButton *btn = obj; + [self P_btn:btn imageName:selectedImages[idx] imagePositionType:imagePositionType spacing:spacing btnControlState:(UIControlStateSelected)]; + }]; + } + }); +} +/** 根据标题下标设置标题图片及位置样式 */ +- (void)setImage:(NSString *)image selectedImage:(NSString *)selectedImage imagePositionType:(SGImagePositionType)imagePositionType spacing:(CGFloat)spacing forIndex:(NSInteger)index { + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.02 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + UIFont *configureTitleFont = self.configure.titleFont; + UIFont *configureTitleSelectedFont = self.configure.titleSelectedFont; + if ([configureTitleFont.fontName isEqualToString:configureTitleSelectedFont.fontName] && configureTitleFont.pointSize == configureTitleSelectedFont.pointSize) { + UIButton *btn = self.btnMArr[index]; + if (image != nil) { + [self P_btn:btn imageName:image imagePositionType:imagePositionType spacing:spacing btnControlState:(UIControlStateNormal)]; + } + if (selectedImage != nil) { + [self P_btn:btn imageName:selectedImage imagePositionType:imagePositionType spacing:spacing btnControlState:(UIControlStateSelected)]; + } + return; + } + + NSLog(@"配置属性 titleFont 必须与配置属性 titleSelectedFont 一致,否则 setImage:selectedImage:imagePositionType:spacing:forIndex 方法将不起任何作用"); + }); +} + +/// imagePositionType 样式设置方法抽取 +- (void)P_btn:(UIButton *)btn imageName:(NSString *)imageName imagePositionType:(SGImagePositionType)imagePositionType spacing:(CGFloat)spacing btnControlState:(UIControlState)btnControlState { + if (imagePositionType == SGImagePositionTypeDefault) { + [btn SG_imagePositionStyle:SGImagePositionStyleDefault spacing:spacing imagePositionBlock:^(UIButton *button) { + if ([imageName hasPrefix:@"http"]) { + [btn SG_loadImageWithUrlString:imageName complete:^(UIImage *image) { + [btn setImage:image forState:btnControlState]; + }]; + } else { + [btn setImage:[UIImage imageNamed:imageName] forState:btnControlState]; + } + }]; + return; + } + if (imagePositionType == SGImagePositionTypeRight) { + [btn SG_imagePositionStyle:SGImagePositionStyleRight spacing:spacing imagePositionBlock:^(UIButton *button) { + if ([imageName hasPrefix:@"http"]) { + [btn SG_loadImageWithUrlString:imageName complete:^(UIImage *image) { + [btn setImage:image forState:btnControlState]; + }]; + } else { + [btn setImage:[UIImage imageNamed:imageName] forState:btnControlState]; + } + }]; + return; + } + if (imagePositionType == SGImagePositionTypeTop) { + [btn SG_imagePositionStyle:SGImagePositionStyleTop spacing:spacing imagePositionBlock:^(UIButton *button) { + if ([imageName hasPrefix:@"http"]) { + [btn SG_loadImageWithUrlString:imageName complete:^(UIImage *image) { + [btn setImage:image forState:btnControlState]; + }]; + } else { + [btn setImage:[UIImage imageNamed:imageName] forState:btnControlState]; + } + }]; + return; + } + if (imagePositionType == SGImagePositionTypeBottom) { + [btn SG_imagePositionStyle:SGImagePositionStyleBottom spacing:spacing imagePositionBlock:^(UIButton *button) { + if ([imageName hasPrefix:@"http"]) { + [btn SG_loadImageWithUrlString:imageName complete:^(UIImage *image) { + [btn setImage:image forState:btnControlState]; + }]; + } else { + [btn setImage:[UIImage imageNamed:imageName] forState:btnControlState]; + } + }]; + } +} + + +#pragma mark - - - badge 相关方法的实现 +/** 根据下标值添加 badge */ +- (void)addBadgeForIndex:(NSInteger)index { + /// 这里使用GCD延迟函数的目的:是将 addBadgeForIndex 方法内部的 badge 布局在 layoutSubviews 方法之后调用, badge 的布局也可在 layoutSubviews 方法中标题(按钮)布局完成之后布局,这里只是为了方便采取了GCD延迟函数 + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.02 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + UIButton *btn = self.btnMArr[index]; + + CGFloat btnTextWidth = [self P_sizeWithString:btn.currentTitle font:self.configure.titleFont].width; + CGFloat btnTextHeight = [self P_sizeWithString:btn.currentTitle font:self.configure.titleFont].height; + CGFloat badgeX = 0.5 * (btn.SG_width - btnTextWidth) + btnTextWidth + self.configure.badgeOff.x; + CGFloat badgeY = 0.5 * (btn.SG_height - btnTextHeight) + self.configure.badgeOff.y - self.configure.badgeHeight; + CGFloat badgeWidth = self.configure.badgeHeight; + CGFloat badgeHeight = badgeWidth; + + UIView *badge = [[UIView alloc] init]; + badge.frame = CGRectMake(badgeX, badgeY, badgeWidth, badgeHeight); + badge.layer.backgroundColor = self.configure.badgeColor.CGColor; + badge.layer.cornerRadius = 0.5 * self.configure.badgeHeight; +// badge.tag = 2018 + index; + [btn addSubview:badge]; + }); +} +/** 根据标题下标值添加 badge */ +- (void)addBadgeWithText:(NSString *)text forIndex:(NSInteger)index { + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.02 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + UIButton *btn = self.btnMArr[index]; + + CGFloat btnTextWidth = [self P_sizeWithString:btn.currentTitle font:self.configure.titleFont].width; + CGFloat btnTextHeight = [self P_sizeWithString:btn.currentTitle font:self.configure.titleFont].height; + + CGFloat badgeX = 0.5 * (btn.SG_width - btnTextWidth) + btnTextWidth + self.configure.badgeOff.x; + CGFloat badgeY = 0.5 * (btn.SG_height - btnTextHeight) + self.configure.badgeOff.y - self.configure.badgeHeight; + CGFloat badgeWidth = [self P_sizeWithString:text font:self.configure.badgeTextFont].width + self.configure.badgeAdditionalWidth; + CGFloat badgeHeight = self.configure.badgeHeight; + + UILabel *badge = [[UILabel alloc] init]; + badge.frame = CGRectMake(badgeX, badgeY, badgeWidth, badgeHeight); + if ([text isEqualToString:@"0"]) { + badge.text = @""; + } else { + badge.text = text; + } + badge.textColor = self.configure.badgeTextColor; + badge.font = self.configure.badgeTextFont; + badge.textAlignment = NSTextAlignmentCenter; + badge.layer.cornerRadius = self.configure.badgeCornerRadius; + badge.layer.backgroundColor = self.configure.badgeColor.CGColor; + badge.layer.borderWidth = self.configure.badgeBorderWidth; + badge.layer.borderColor = self.configure.badgeBorderColor.CGColor; + [btn addSubview:badge]; + }); +} +/** 根据下标值移除 badge */ +- (void)removeBadgeForIndex:(NSInteger)index { + UIButton *btn = self.btnMArr[index]; + [btn.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { +// if (obj.tag != 0) { + /// 之前使用 tag 进行 btn 内部子视图区分(UIImageView、UIButtonLabel的 tag 值都是 0) + /// isMemberOfClass 这个方法明确某个类 + if ([obj isMemberOfClass:[UILabel class]] || [obj isMemberOfClass:[UIView class]]) { + [obj removeFromSuperview]; + obj = nil; + } + }]; +} + + +#pragma mark - - - 指示器相关方法的实现 +/** 重置指示器颜色方法 */ +- (void)resetIndicatorColor:(UIColor *)color { + _indicatorView.backgroundColor = color; +} +/** + * 重置标题普通状态、选中状态下文字颜色方法 + * + * @param color 普通状态下标题文字颜色 + * @param selectedColor 选中状态下标题文字颜色 + */ +- (void)resetTitleColor:(UIColor *)color titleSelectedColor:(UIColor *)selectedColor { + [self.btnMArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { + UIButton *btn = obj; + [btn setTitleColor:color forState:(UIControlStateNormal)]; + [btn setTitleColor:selectedColor forState:(UIControlStateSelected)]; + }]; + + if (self.configure.titleGradientEffect) { + self.configure.titleColor = color; + self.configure.titleSelectedColor = selectedColor; + [self setupStartColor:self.configure.titleColor]; + [self setupEndColor:self.configure.titleSelectedColor]; + } +} +/** + * 重置标题普通状态、选中状态下文字颜色及指示器颜色方法 + * + * @param color 普通状态下标题文字颜色 + * @param selectedColor 选中状态下标题文字颜色 + * @param indicatorColor 指示器颜色 + */ +- (void)resetTitleColor:(UIColor *)color titleSelectedColor:(UIColor *)selectedColor indicatorColor:(UIColor *)indicatorColor { + [self resetTitleColor:color titleSelectedColor:selectedColor]; + [self resetIndicatorColor:indicatorColor]; +} + +#pragma mark - - - - - - - >> 给外界提供的相关属性及方法的实现 END << - - - - - - - + +#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; +} + @end diff --git a/SGPagingView/SGPageTitle/SGPageTitleViewConfigure.h b/SGPagingView/SGPageTitle/SGPageTitleViewConfigure.h old mode 100755 new mode 100644 index 5005d04..7b3844b --- a/SGPagingView/SGPageTitle/SGPageTitleViewConfigure.h +++ b/SGPagingView/SGPageTitle/SGPageTitleViewConfigure.h @@ -44,7 +44,7 @@ typedef enum : NSUInteger { /** SGPageTitleView 底部分割线颜色,默认为 lightGrayColor */ @property (nonatomic, strong) UIColor *bottomSeparatorColor; -#pragma mark - - 标题属性 +#pragma mark - - SGPageTitleView 标题属性 /** 标题文字字号大小,默认 15 号字体 */ @property (nonatomic, strong) UIFont *titleFont; /** 标题文字选中字号大小,默认 15 号字体。 @@ -62,23 +62,24 @@ typedef enum : NSUInteger { /** 标题文字缩放比,默认为 0.0f,取值范围 0.0 ~ 1.0f。 * 请与 titleTextZoom = YES 时结合使用,否则不起作用 */ @property (nonatomic, assign) CGFloat titleTextZoomRatio; -/** 标题额外增加的宽度,默认为 20.0f */ +/** 标题额外需要增加的宽度,默认为 20.0f */ @property (nonatomic, assign) CGFloat titleAdditionalWidth; -#pragma mark - - 指示器属性 -/** 是否显示指示器,默认为 YES */ +#pragma mark - - SGPageTitleView 指示器属性 +/** 是否显示指示器,默认为 YES。 + * 为 NO 时,可与 SGIndicatorScrollStyle = SGIndicatorScrollStyleHalf 一起使用 */ @property (nonatomic, assign) BOOL showIndicator; /** 指示器颜色,默认为红色 */ @property (nonatomic, strong) UIColor *indicatorColor; /** 指示器高度,默认为 2.0f */ @property (nonatomic, assign) CGFloat indicatorHeight; -/** 指示器动画时间,默认为 0.1f,取值范围 0 ~ 0.3f */ +/** 指示器动画时间,默认为 0.1f,取值范围 0.0 ~ 0.3f */ @property (nonatomic, assign) CGFloat indicatorAnimationTime; /** 指示器样式,默认为 SGIndicatorStyleDefault */ @property (nonatomic, assign) SGIndicatorStyle indicatorStyle; /** 指示器圆角大小,默认为 0.0f */ @property (nonatomic, assign) CGFloat indicatorCornerRadius; -/** 指示器遮盖样式外的其他样式下指示器与底部之间的距离,默认为 0f */ +/** 指示器遮盖样式外的其他样式下指示器与底部之间的距离,默认为 0.0f */ @property (nonatomic, assign) CGFloat indicatorToBottomDistance; /** 指示器遮盖样式下的边框宽度,默认为 0.0f */ @property (nonatomic, assign) CGFloat indicatorBorderWidth; @@ -86,14 +87,14 @@ typedef enum : NSUInteger { @property (nonatomic, strong) UIColor *indicatorBorderColor; /** 指示器遮盖、下划线样式下额外增加的宽度,默认为 0.0f;介于标题文字宽度与按钮宽度之间 */ @property (nonatomic, assign) CGFloat indicatorAdditionalWidth; -/** 指示器固定样式下宽度,默认为 20.0f;最大宽度并没有做限制,请根据实际情况妥善设置 */ +/** 固定样式下指示器的宽度,默认为 20.0f;最大宽度并没有做限制,请根据实际情况妥善设置 */ @property (nonatomic, assign) CGFloat indicatorFixedWidth; -/** 指示器动态样式下宽度,默认为 20.0f;最大宽度并没有做限制,请根据实际情况妥善设置 */ +/** 动态样式下指示器的宽度,默认为 20.0f;最大宽度并没有做限制,请根据实际情况妥善设置 */ @property (nonatomic, assign) CGFloat indicatorDynamicWidth; -/** 指示器滚动位置改变样式,默认为 SGIndicatorScrollStyleDefault */ +/** 滚动内容视图时,指示器位置改变样式,默认为 SGIndicatorScrollStyleDefault */ @property (nonatomic, assign) SGIndicatorScrollStyle indicatorScrollStyle; -#pragma mark - - 标题间分割线属性 +#pragma mark - - SGPageTitleView 标题间分割线属性 /** 是否显示标题间分割线,默认为 NO */ @property (nonatomic, assign) BOOL showVerticalSeparator; /** 标题间分割线颜色,默认为红色 */ @@ -101,12 +102,24 @@ typedef enum : NSUInteger { /** 标题间分割线额外减少的高度,默认为 0.0f */ @property (nonatomic, assign) CGFloat verticalSeparatorReduceHeight; -#pragma mark - - badge 相关属性 +#pragma mark - - SGPageTitleView badge 属性,默认所在位置以标题文字右上角为起点 /** badge 颜色,默认红色 */ @property (nonatomic, strong) UIColor *badgeColor; -/** badge 尺寸大小,默认为 7.0f */ -@property (nonatomic, assign) CGFloat badgeSize; -/** badge 偏移量,默认(0,0)*/ +/** badge 的高,默认为 7.0f */ +@property (nonatomic, assign) CGFloat badgeHeight; +/** badge 的偏移量,默认为 CGPointZero */ @property (nonatomic, assign) CGPoint badgeOff; +/** badge 的字体颜色,默认为 whiteColor(只针对:addBadgeWithText:forIndex: 方法有效)*/ +@property (nonatomic, strong) UIColor *badgeTextColor; +/** badge 的字体大小,默认为 [UIFont systemFontOfSize:10](只针对:addBadgeWithText:forIndex: 方法有效)*/ +@property (nonatomic, strong) UIFont *badgeTextFont; +/** badge 额外需要增加的宽度,默认为 10.0f(只针对:addBadgeWithText:forIndex: 方法有效)*/ +@property (nonatomic, assign) CGFloat badgeAdditionalWidth; +/** badge 边框的宽度,默认为 nil(只针对:addBadgeWithText:forIndex: 方法有效)*/ +@property (nonatomic, assign) CGFloat badgeBorderWidth; +/** badge 边框的颜色,默认为 nil(只针对:addBadgeWithText:forIndex: 方法有效)*/ +@property (nonatomic, strong) UIColor *badgeBorderColor; +/** badge 圆角设置,默认为 5.0f(只针对:addBadgeWithText:forIndex: 方法有效)*/ +@property (nonatomic, assign) CGFloat badgeCornerRadius; @end diff --git a/SGPagingView/SGPageTitle/SGPageTitleViewConfigure.m b/SGPagingView/SGPageTitle/SGPageTitleViewConfigure.m old mode 100755 new mode 100644 index abb400c..8cd0aac --- a/SGPagingView/SGPageTitle/SGPageTitleViewConfigure.m +++ b/SGPagingView/SGPageTitle/SGPageTitleViewConfigure.m @@ -177,18 +177,38 @@ - (UIColor *)badgeColor { return _badgeColor; } -- (CGFloat)badgeSize { - if (!_badgeSize) { - _badgeSize = 7.0f; +- (CGFloat)badgeHeight { + if (!_badgeHeight) { + _badgeHeight = 7.0f; } - return _badgeSize; + return _badgeHeight; } -- (CGPoint)badgeOff { - if (!_badgeOff.x && !_badgeOff.y) { - _badgeOff = CGPointMake(0, 0); +- (UIColor *)badgeTextColor { + if (!_badgeTextColor) { + _badgeTextColor = [UIColor whiteColor]; } - return _badgeOff; + return _badgeTextColor; +} +- (UIFont *)badgeTextFont { + if (!_badgeTextFont) { + _badgeTextFont = [UIFont systemFontOfSize:10]; + } + return _badgeTextFont; +} + +- (CGFloat)badgeAdditionalWidth { + if (_badgeAdditionalWidth <= 0) { + _badgeAdditionalWidth = 10.0f; + } + return _badgeAdditionalWidth; +} + +- (CGFloat)badgeCornerRadius { + if (!_badgeCornerRadius) { + _badgeCornerRadius = 5.0f; + } + return _badgeCornerRadius; } @end diff --git a/SGPagingView/SGPagingView.h b/SGPagingView/SGPagingView.h index 0323d4c..161dc67 100755 --- a/SGPagingView/SGPagingView.h +++ b/SGPagingView/SGPagingView.h @@ -1,6 +1,6 @@ // // SGPagingView.h -// Version 1.6.8 +// Version 1.6.9 // GitHub:https://github.com/kingsic/SGPagingView // // Created by kingsic on 2016/10/6. diff --git a/SGPagingViewExample/MainVC/DefaultGradientEffectVC.m b/SGPagingViewExample/MainVC/DefaultGradientEffectVC.m index 09ed519..e81bff9 100755 --- a/SGPagingViewExample/MainVC/DefaultGradientEffectVC.m +++ b/SGPagingViewExample/MainVC/DefaultGradientEffectVC.m @@ -49,7 +49,11 @@ - (void)setupPageView { /// pageTitleView self.pageTitleView = [SGPageTitleView pageTitleViewWithFrame:CGRectMake(0, pageTitleViewY, self.view.frame.size.width, 44) delegate:self titleNames:titleArr configure:configure]; [self.view addSubview:_pageTitleView]; - + configure.badgeHeight = 12; + configure.badgeOff = CGPointMake(- 5, 5); + [_pageTitleView addBadgeWithText:@"2" forIndex:1]; + [_pageTitleView addBadgeWithText:@"新版本" forIndex:2]; + ChildVCOne *oneVC = [[ChildVCOne alloc] init]; ChildVCTwo *twoVC = [[ChildVCTwo alloc] init]; ChildVCThree *threeVC = [[ChildVCThree alloc] init]; @@ -70,6 +74,12 @@ - (void)pageContentCollectionView:(SGPageContentCollectionView *)pageContentColl [self.pageTitleView setPageTitleViewWithProgress:progress originalIndex:originalIndex targetIndex:targetIndex]; } +- (void)pageContentCollectionView:(SGPageContentScrollView *)pageContentScrollView index:(NSInteger)index { + if (index == 1 || index == 2) { + [_pageTitleView removeBadgeForIndex:index]; + } +} + - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. diff --git a/SGPagingViewExample/ViewController.m b/SGPagingViewExample/ViewController.m index 2692e6c..d5666ef 100755 --- a/SGPagingViewExample/ViewController.m +++ b/SGPagingViewExample/ViewController.m @@ -35,7 +35,7 @@ - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. - self.TitleDataList = @[@"静止样式", @"滚动样式", @"系统样式", @"图片样式", @"文字上下样式(富文本)", @"文字渐变效果", @"文字缩放效果", @"指示器固定样式", @"指示器动态样式", @"指示器遮盖样式一", @"指示器遮盖样式二(从左到右自动布局)", @"指示器遮盖样式三", @"侧滑返回手势案例", @"滚动内容动画案例", @"富文本案例", @"导航栏样式案例"]; + self.TitleDataList = @[@"静止样式", @"滚动样式", @"系统样式", @"图片样式", @"文字上下样式(富文本)", @"文字渐变效果(badge)", @"文字缩放效果", @"指示器固定样式", @"指示器动态样式", @"指示器遮盖样式一", @"指示器遮盖样式二(从左到右自动布局)", @"指示器遮盖样式三", @"侧滑返回手势案例", @"滚动内容动画案例", @"富文本案例", @"导航栏样式案例"]; [self foundTableView]; }