這一篇記錄的是iOS開發(fā)中第三方庫WMPageController控件的使用方法敌蜂,主要是用來分頁顯示內(nèi)容的箩兽,可以通過手勢滑動來切換頁面,也可以通過點擊標題部分來切換頁面紊册,如下圖所示:
使用方法:
新建工程DemoTest1比肄,然后通過cocoapods引入WMPageController到項目中,Podfile文件的內(nèi)容如下:
platform :ios,'7.0'
target 'DemoTest1' do
pod 'WMPageController', '~> 1.6.4'
end
方法一:
(1)創(chuàng)建幾個ViewController繼承自UIViewController測試用:
(2)打開AppDelegate.m文件囊陡,在其中加入下面一個方法:
#import "WMPageController.h"
#import "OneViewController.h"
#import "ViewController.h"
#import "TwoViewController.h"
@interface AppDelegate ()
@property(nonatomic,strong) WMPageController *createPages;
@end
@implementation AppDelegate
- (WMPageController *)createPages {
//WMPageController中包含的頁面數(shù)組
NSArray *controllers = [NSArray arrayWithObjects:[ViewController class], [OneViewController class],[TwoViewController class], nil];
//WMPageController控件的標題數(shù)組
NSArray *titles = [NSArray arrayWithObjects:@"體育新聞", @"娛樂新聞",@"直播新聞", nil];
//用上面兩個數(shù)組初始化WMPageController對象
WMPageController *pageController = [[WMPageController alloc] initWithViewControllerClasses:controllers andTheirTitles:titles];
pageController.menuViewStyle = WMMenuViewStyleLine;
pageController.titleColorSelected = [UIColor whiteColor];
pageController.titleColorNormal = [UIColor colorWithRed:168.0/255.0 green:20.0/255.0 blue:4/255.0 alpha:1];
pageController.progressColor = [UIColor colorWithRed:168.0/255.0 green:20.0/255.0 blue:4/255.0 alpha:1];
// pageController.itemsWidths = @[@(70),@(100),@(70)]; // 這里可以設(shè)置不同的寬度
//設(shè)置WMPageController每個標題的寬度
pageController.menuItemWidth = 375/3;
//設(shè)置WMPageController標題欄的高度
pageController.menuHeight = 35;
//設(shè)置WMPageController選中的標題的顏色
pageController.titleColorSelected = [UIColor colorWithRed:200 green:0 blue:0 alpha:1];
return pageController;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
WMPageController *page = [self createPages];
UINavigationController *na = [[UINavigationController alloc] initWithRootViewController:page];
self.window.rootViewController = na;
[self.window makeKeyAndVisible];
return YES;
}
方法二:
(1)創(chuàng)建個ViewController繼承自WMPageController
(2)編輯ViewController.m文件芳绩,代碼如下:
初始化設(shè)置:
#import "TwoViewController.h"
#import "OneViewController.h"
#import "TableViewController.h"
#import "ThreeViewController.h"
@interface ThreeViewController ()
@property (nonatomic, strong) NSArray *titleData;
@end
@implementation ThreeViewController
#pragma mark 標題數(shù)組
- (NSArray *)titleData {
if (!_titleData) {
_titleData = @[@"單曲", @"詳情", @"歌詞",@"歌詞"];
}
return _titleData;
}
#pragma mark 初始化代碼
- (instancetype)init {
if (self = [super init]) {
self.titleSizeNormal = 15;
self.titleSizeSelected = 15;
self.menuViewStyle = WMMenuViewStyleLine;
self.menuItemWidth = [UIScreen mainScreen].bounds.size.width / self.titleData.count;
self.menuHeight = 50;
}
return self;
}
WMPageController --Datasource & Delegate
#pragma mark - Datasource & Delegate
#pragma mark 返回子頁面的個數(shù)
- (NSInteger)numbersOfChildControllersInPageController:(WMPageController *)pageController {
return self.titleData.count;
}
#pragma mark 返回某個index對應的頁面
- (UIViewController *)pageController:(WMPageController *)pageController viewControllerAtIndex:(NSInteger)index {
switch (index) {
case 0:{
TwoViewController *vcClass = [[TwoViewController alloc] init];
vcClass.title = @"1";
return vcClass;
}
break;
case 1:{
OneViewController *vcClass = [OneViewController new];
vcClass.title = @"2";
return vcClass;
}
break;
case 2:{
TableViewController *vcClass = [TableViewController new];
vcClass.title = @"3";
return vcClass;
}
break;
default:{
OneViewController *vcClass = [OneViewController new];
vcClass.title = @"4";
return vcClass;
}
break;
}
}
#pragma mark 返回index對應的標題
- (NSString *)pageController:(WMPageController *)pageController titleAtIndex:(NSInteger)index {
return self.titleData[index];
}
相關(guān)設(shè)置:
/**
* Values and keys can set properties when initialize child controlelr (it's KVC)
* values keys 屬性可以用于初始化控制器的時候為控制器傳值(利用 KVC 來設(shè)置)
使用時請確保 key 與控制器的屬性名字一致!撞反!(例如:控制器有需要設(shè)置的屬性 type妥色,那么 keys 所放的就是字符串 @"type")
*/
@property (nonatomic, strong) NSMutableArray<id> *values;
@property (nonatomic, strong) NSMutableArray<NSString *> *keys;
/**
* 各個控制器的 class, 例如:[UITableViewController class]
* Each controller's class, example:[UITableViewController class]
*/
@property (nonatomic, copy) NSArray<Class> *viewControllerClasses;
/**
* 各個控制器標題
* Titles of view controllers in page controller.
*/
@property (nonatomic, copy) NSArray<NSString *> *titles;
@property (nonatomic, strong, readonly) UIViewController *currentViewController;
/**
* 設(shè)置選中幾號 item
* To select item at index
*/
@property (nonatomic, assign) int selectIndex;
/**
* 點擊相鄰的 MenuItem 是否觸發(fā)翻頁動畫 (當當前選中與點擊Item相差大于1是不觸發(fā))
* Whether to animate when press the MenuItem, if distant between the selected and the pressed is larger than 1,never animate.
*/
@property (nonatomic, assign) BOOL pageAnimatable;
/**
* 選中時的標題尺寸
* The title size when selected (animatable)
*/
@property (nonatomic, assign) CGFloat titleSizeSelected;
/**
* 非選中時的標題尺寸
* The normal title size (animatable)
*/
@property (nonatomic, assign) CGFloat titleSizeNormal;
/**
* 標題選中時的顏色, 顏色是可動畫的.
* The title color when selected, the color is animatable.
*/
@property (nonatomic, strong) UIColor *titleColorSelected;
/**
* 標題非選擇時的顏色, 顏色是可動畫的.
* The title's normal color, the color is animatable.
*/
@property (nonatomic, strong) UIColor *titleColorNormal;
/**
* 標題的字體名字
* The name of title's font
*/
@property (nonatomic, copy) NSString *titleFontName;
/**
* 導航欄高度
* The menu view's height
*/
@property (nonatomic, assign) CGFloat menuHeight;
// 當所有item的寬度加起來小于屏幕寬時,PageController會自動幫助排版遏片,添加每個item之間的間隙以填充整個寬度
// When the sum of all the item's width is smaller than the screen's width, pageController will add gap to each item automatically, in order to fill the width.
/**
* 每個 MenuItem 的寬度
* The item width,when all are same,use this property
*/
@property (nonatomic, assign) CGFloat menuItemWidth;
/**
* 各個 MenuItem 的寬度嘹害,可不等,數(shù)組內(nèi)為 NSNumber.
* Each item's width, when they are not all the same, use this property, Put `NSNumber` in this array.
*/
@property (nonatomic, copy) NSArray<NSNumber *> *itemsWidths;
/**
* 導航欄背景色
* The background color of menu view
*/
@property (nonatomic, strong) UIColor *menuBGColor;
/**
* Menu view 的樣式吮便,默認為無下劃線
* Menu view's style, now has two different styles, 'Line','default'
*/
@property (nonatomic, assign) WMMenuViewStyle menuViewStyle;
@property (nonatomic, assign) WMMenuViewLayoutMode menuViewLayoutMode;
/**
* 進度條的顏色笔呀,默認和選中顏色一致(如果 style 為 Default,則該屬性無用)
* The progress's color,the default color is same with `titleColorSelected`.If you want to have a different color, set this property.
*/
@property (nonatomic, strong) UIColor *progressColor;
/**
* 定制進度條在各個 item 下的寬度
*/
@property (nonatomic, strong) NSArray *progressViewWidths;
/// 定制進度條髓需,若每個進度條長度相同许师,可設(shè)置該屬性
@property (nonatomic, assign) CGFloat progressWidth;
/// 調(diào)皮效果,用于實現(xiàn)騰訊視頻新效果僚匆,請設(shè)置一個較小的 progressWidth
@property (nonatomic, assign) BOOL progressViewIsNaughty;
/**
* 是否發(fā)送在創(chuàng)建控制器或者視圖完全展現(xiàn)在用戶眼前時通知觀察者微渠,默認為不開啟,如需利用通知請開啟
* Whether notify observer when finish init or fully displayed to user, the default is NO.
* See `WMPageConst.h` for more information.
*/
@property (nonatomic, assign) BOOL postNotification;
/**
* 是否記錄 Controller 的位置咧擂,并在下次回來的時候回到相應位置逞盆,默認為 NO (若當前緩存中存在不會觸發(fā))
* Whether to remember controller's positon if it's a kind of scrollView controller,like UITableViewController,The default value is NO.
* 比如 `UITabelViewController`, 當然你也可以在自己的控制器中自行設(shè)置, 如果將 Controller.view 替換為 scrollView 或者在Controller.view 上添加了一個和自身 bounds 一樣的 scrollView 也是OK的
*/
@property (nonatomic, assign) BOOL rememberLocation __deprecated_msg("Because of the cache policy,this property can abondon now.");
/** 緩存的機制,默認為無限制 (如果收到內(nèi)存警告, 會自動切換) */
@property (nonatomic, assign) WMPageControllerCachePolicy cachePolicy;
/** 預加載機制松申,在停止滑動的時候預加載 n 頁 */
@property (nonatomic, assign) WMPageControllerPreloadPolicy preloadPolicy;
/** Whether ContentView bounces */
@property (nonatomic, assign) BOOL bounces;
/**
* 是否作為 NavigationBar 的 titleView 展示云芦,默認 NO
* Whether to show on navigation bar, the default value is `NO`
*/
@property (assign, nonatomic) BOOL showOnNavigationBar;
/**
* 用代碼設(shè)置 contentView 的 contentOffset 之前俯逾,請設(shè)置 startDragging = YES
* Set startDragging = YES before set contentView.contentOffset = xxx;
*/
@property (nonatomic, assign) BOOL startDragging;
/** 下劃線進度條的高度 */
@property (nonatomic, assign) CGFloat progressHeight;
/** WMPageController View' frame */
@property (nonatomic, assign) CGRect viewFrame;
/**
* Menu view items' margin / make sure it's count is equal to (controllers' count + 1),default is 0
頂部菜單欄各個 item 的間隙,因為包括頭尾兩端焕数,所以確保它的數(shù)量等于控制器數(shù)量 + 1, 默認間隙為 0
*/
@property (nonatomic, copy) NSArray<NSNumber *> *itemsMargins;
/**
* set itemMargin if all margins are the same, default is 0
如果各個間隙都想同纱昧,設(shè)置該屬性刨啸,默認為 0
*/
@property (nonatomic, assign) CGFloat itemMargin;
/** 頂部 menuView 和 scrollView 之間的間隙 */
@property (nonatomic, assign) CGFloat menuViewBottomSpace;
/** progressView 到 menuView 底部的距離 */
@property (nonatomic, assign) CGFloat progressViewBottomSpace;
/** progressView's cornerRadius */
@property (nonatomic, assign) CGFloat progressViewCornerRadius;
/** 頂部導航欄 */
@property (nonatomic, weak) WMMenuView *menuView;
/** 內(nèi)部容器 */
@property (nonatomic, weak) WMScrollView *scrollView;
/** MenuView 內(nèi)部視圖與左右的間距 */
@property (nonatomic, assign) CGFloat menuViewContentMargin;
/**
* 左滑時同時啟用其他手勢堡赔,比如系統(tǒng)左滑牢酵、sidemenu左滑沮稚。默認 NO
(會引起一個小問題敦第,第一個和最后一個控制器會變得可以斜滑, 還未解決)
*/
@property (assign, nonatomic) BOOL otherGestureRecognizerSimultaneously;
/**
* 構(gòu)造方法荤牍,請使用該方法創(chuàng)建控制器. 或者實現(xiàn)數(shù)據(jù)源方法. /
* Init method县耽,recommend to use this instead of `-init`. Or you can implement datasource by yourself.
*
* @param classes 子控制器的 class礼预,確保數(shù)量與 titles 的數(shù)量相等
* @param titles 各個子控制器的標題把还,用 NSString 描述
*
* @return instancetype
*/
- (instancetype)initWithViewControllerClasses:(NSArray<Class> *)classes andTheirTitles:(NSArray<NSString *> *)titles;
/**
* A method in order to reload MenuView and child view controllers. If you had set `itemsMargins` or `itemsWidths` `values` and `keys` before, make sure you have update them also before you call this method. And most important, PAY ATTENTION TO THE COUNT OF THOSE ARRAY.
該方法用于重置刷新父控制器校焦,該刷新包括頂部 MenuView 和 childViewControllers.如果之前設(shè)置過 `itemsMargins` 和 `itemsWidths` `values` 以及 `keys` 屬性宫蛆,請確保在調(diào)用 reload 之前也同時更新了這些屬性艘包。并且,最最最重要的耀盗,注意數(shù)組的個數(shù)以防止溢出想虎。
*/
- (void)reloadData;
/**
* Update designated item's title
更新指定序號的控制器的標題
*
* @param title 新的標題
* @param index 目標序號
*/
- (void)updateTitle:(NSString *)title atIndex:(NSInteger)index;
/**
* Update designated item's title and width
更新指定序號的控制器的標題以及他的寬度
*
* @param title 新的標題
* @param index 目標序號
* @param width 對應item的新寬度
*/
- (void)updateTitle:(NSString *)title andWidth:(CGFloat)width atIndex:(NSInteger)index;
/** 當 app 即將進入后臺接收到的通知 */
- (void)willResignActive:(NSNotification *)notification;
/** 當 app 即將回到前臺接收到的通知 */
- (void)willEnterForeground:(NSNotification *)notification;