最近發(fā)現(xiàn)很多項目中都有用到父子控制器切換界面的功能号坡,就著手封裝一個簡易的小框架CDDPagerController吨凑,本著能簡單就簡單的原則耕捞,實現(xiàn)功能如下:
Demo簡介
1.集成方式1
- 去GitHub上下載我的開源項目:CDDPagerController
- 打開文件夾找到CDDPagerController文件负溪,拖到自己項目中
- 繼承框架內頭文件
#import "DCPagerController.h"
- 根據(jù)需求調用
DCPagerController.h
的方法
集成方式2
- 集成pods 使用方式 在Podfile中加入 pod 'CDDPagerController'即可~
/**
根據(jù)角標,跳轉到對應的控制器(viewWillAppear方法里實現(xiàn))
*/
@property (nonatomic, assign) NSInteger selectIndex;
/**
字體縮放
*/
- (void)setUpTitleScale:(void(^)(CGFloat *titleScale))titleScaleBlock;
/**
progress設置
*progressLength 設置progress長度
*progressHeight 設置progress高度
*/
- (void)setUpProgressAttribute:(void(^)(CGFloat *progressLength, CGFloat *progressHeight))settingProgressBlock;
/**
初始化
*titleScrollViewBgColor 標題背景色
*norColor 標題字體未選中狀態(tài)下顏色
*selColor 標題字體選中狀態(tài)下顏色
*proColor 字體下方指示器顏色
*titleFont 標題字體大小
*isShowPregressView 是否開啟字體下方指示器
*isOpenStretch 是否開啟指示器拉伸效果
*isOpenShade 是否開啟字體漸變效果
@param BaseSettingBlock 設置基本屬性
*/
- (void)setUpDisplayStyle:(void(^)(UIColor **titleScrollViewBgColor,UIColor **norColor,UIColor **selColor,UIColor **proColor,UIFont **titleFont,BOOL *isShowPregressView,BOOL *isOpenStretch,BOOL *isOpenShade))BaseSettingBlock;
2.Demo事列(含GIF圖)
type01
a.繼承
#import "DCPagerController.h"
@interface DCDemo01ViewController : DCPagerController
b.實現(xiàn)
#pragma mark - LazyLoad
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// self.selectIndex = 3; //默認選擇第幾個設置(不設置則默認選擇第0個)
}
#pragma mark - LifeCyle
- (void)viewDidLoad {
[super viewDidLoad];
[self setUpAllChildViewController];
[self setUpDisplayStyle:^(UIColor *__autoreleasing *titleScrollViewBgColor, UIColor *__autoreleasing *norColor, UIColor *__autoreleasing *selColor, UIColor *__autoreleasing *proColor, UIFont *__autoreleasing *titleFont, BOOL *isShowProgressView, BOOL *isOpenStretch, BOOL *isOpenShade) {
*titleScrollViewBgColor = [UIColor whiteColor]; //標題View背景色(默認標題背景色為白色)
*norColor = [UIColor darkGrayColor]; //標題未選中顏色(默認未選中狀態(tài)下字體顏色為黑色)
*selColor = [UIColor orangeColor]; //標題選中顏色(默認選中狀態(tài)下字體顏色為紅色)
*proColor = [UIColor purpleColor]; //滾動條顏色(默認為標題選中顏色)
*titleFont = [UIFont systemFontOfSize:16]; //字體尺寸 (默認fontSize為15)
/*
以下BOOL值默認都為NO
*/
*isShowProgressView = YES; //是否開啟標題下部Pregress指示器
*isOpenStretch = YES; //是否開啟指示器拉伸效果
*isOpenShade = YES; //是否開啟字體漸變
}];
[self setUpTitleScale:^(CGFloat *titleScale) { //titleScale范圍在0到1之間 <0 或者 > 1 則默認不縮放 默認設置titleScale就開啟縮放婴渡,不設置則關閉
*titleScale = 0.1;
}];
[self setUpProgressAttribute:^(CGFloat *progressLength, CGFloat *progressHeight) {//progressLength 設置底部progress指示器的長度幻锁,有默認值為按鈕的寬度的百分之56 progressHeight默認高度4(并且不能大于10)
*progressLength = 40;
*progressHeight = 5;
}];
}
#pragma mark - 添加所有子控制器
- (void)setUpAllChildViewController
{
NSArray *titles = @[@"測試01",@"測試02",@"測試03",@"測試04",@"測試05"];
for (NSInteger i = 0; i < titles.count; i++) {
UIViewController *vc = [UIViewController new];
vc.title = titles[i];
vc.view.backgroundColor = RandColor; //隨機色
[self addChildViewController:vc];
}
}
Demo中其他幾種Type
- (void)viewDidLoad {
[super viewDidLoad];
[self setUpAllChildViewController];
[self setUpDisplayStyle:^(UIColor *__autoreleasing *titleScrollViewBgColor, UIColor *__autoreleasing *norColor, UIColor *__autoreleasing *selColor, UIColor *__autoreleasing *proColor, UIFont *__autoreleasing *titleFont, BOOL *isShowProgressView, BOOL *isOpenStretch, BOOL *isOpenShade) {
*titleFont = [UIFont systemFontOfSize:16];
*isShowProgressView = YES; //是否開啟標題下部Pregress指示器
*isOpenStretch = YES; //是否開啟指示器拉伸效果
*isOpenShade = YES; //是否開啟字體漸變
}];
}
#pragma mark - 添加所有子控制器
- (void)setUpAllChildViewController
{
NSArray *titles = @[@"測試01",@"測試02",@"測試03",@"測試04"];
for (NSInteger i = 0; i < titles.count; i++) {
UIViewController *vc = [UIViewController new];
vc.title = titles[i];
vc.view.backgroundColor = RandColor; //隨機色
[self addChildViewController:vc];
}
}
#pragma mark - LifeCyle
- (void)viewDidLoad {
[super viewDidLoad];
[self setUpAllChildViewController];
}
#pragma mark - 添加所有子控制器
- (void)setUpAllChildViewController
{
NSArray *titles = @[@"測試01",@"測試02",@"測試03"];
for (NSInteger i = 0; i < titles.count; i++) {
UIViewController *vc = [UIViewController new];
vc.title = titles[i];
vc.view.backgroundColor = RandColor; //隨機色
[self addChildViewController:vc];
}
}
#pragma mark - LifeCyle
- (void)viewDidLoad {
[super viewDidLoad];
[self setUpAllChildViewController];
[self setUpDisplayStyle:^(UIColor *__autoreleasing *titleScrollViewBgColor, UIColor *__autoreleasing *norColor, UIColor *__autoreleasing *selColor, UIColor *__autoreleasing *proColor, UIFont *__autoreleasing *titleFont, BOOL *isShowProgressView, BOOL *isOpenStretch, BOOL *isOpenShade) {
*selColor = [UIColor orangeColor];
*isOpenShade = YES;
}];
[self setUpTitleScale:^(CGFloat *titleScale) { //titleScale范圍在0到1之間 <0 或者 > 1 則默認不縮放 默認設置titleScale就開啟縮放,不設置則關閉
*titleScale = 0.3;
}];
}
#pragma mark - 添加所有子控制器
- (void)setUpAllChildViewController
{
NSArray *titles = @[@"測試01",@"測試02",@"測試03",@"測試04",@"測試05",@"測試06",@"測試07",@"測試08",@"測試09"];
for (NSInteger i = 0; i < titles.count; i++) {
UIViewController *vc = [UIViewController new];
vc.title = titles[i];
vc.view.backgroundColor = RandColor; //隨機色
[self addChildViewController:vc];
}
}
-
參考 :快速集成App中頂部標題滾動條在封裝技巧上上學到了很多缩搅,有需要可以點擊地址學習下
框架已上傳到GitHub,歡迎下載触幼,喜歡的話點個Star
Git地址:https://github.com/RocketsChen/CDDPagerController