iOS开源库PKRevealController的使用
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
自學(xué)iOS也有一段時(shí)間了,期間發(fā)現(xiàn)iOS和Android一樣,有很多非常優(yōu)秀的開源庫(kù)可以使用。但無(wú)奈國(guó)內(nèi)幾乎沒(méi)有太多關(guān)于此方面資料,唯有在Github上摸索。今天就寫一篇關(guān)于PKRevealController的使用。本文章假定你已經(jīng)具有一定的Objective-C開發(fā)技術(shù)技術(shù),若需要入門教程請(qǐng)咨詢谷歌君。
PKRevealController是什么
PKRevealController是由ZUUIRevealController改進(jìn)而來(lái),是一個(gè)簡(jiǎn)單、漂亮的開源庫(kù)。實(shí)現(xiàn)了Facebook iOS客戶端左右兩邊側(cè)邊菜單欄的效果(如下圖)
??
其實(shí),在Google上搜索『facebook like side menu』可以搜到一大堆可以實(shí)現(xiàn)的方案,其中有不少非常不錯(cuò)的實(shí)現(xiàn)方式。但我這篇文章中選擇PKRevealController來(lái)演示。因?yàn)榭戳藥追N不同的實(shí)現(xiàn)方案之后,發(fā)現(xiàn)還是PKRevealController的實(shí)現(xiàn)方式比較簡(jiǎn)單、易用,而且最終效果和Facebook的效果高度一致。
其Github主頁(yè)上對(duì)于它的說(shuō)明如下:
PKRevealController (ex. ZUUIRevealController) is a delightful view controller container for iOS, enabling you to present multiple controllers on top of one another.
其主要的特點(diǎn)有:
- Proper view controller containment usage
- Elegant block API
- Supports both left and right sided view presentation
- Works on both iPhones & iPads
- Supports landscape & portrait orientations
PKRevealController使用概要
從Github下載該項(xiàng)目,找到其中的PKRevealController/Controller文件夾,把它拖到項(xiàng)目中去就可以了
在任何一個(gè)你想要使用它的地方記得導(dǎo)入?#import "PKRevealController.h"
在你的項(xiàng)目AppDelegate.h里面聲明一個(gè)PKRevealController對(duì)象
@property (nonatomic, strong) PKRevealController *revealController;之后在AppDelegate.m中適當(dāng)?shù)某跏蓟?#xff08;后面詳解)
self.revealController = [PKRevealController revealControllerWithFrontViewController:frontViewController leftViewController:leftViewController options:nil];在左邊或者右邊菜單欄里面現(xiàn)實(shí)內(nèi)容,并在其中實(shí)現(xiàn)點(diǎn)擊以后的切換效果即可
PKRevealController使用詳解
為了能夠演示如何使用PKRevealController,簡(jiǎn)單新建一個(gè)如下圖所示的項(xiàng)目。運(yùn)行之后可以看到主界面,在其主界面左上角導(dǎo)航欄具有一個(gè)按鈕,點(diǎn)擊之后即可查看菜單。菜單種有兩項(xiàng)分別位:Home、Profile。點(diǎn)擊Home返回主頁(yè)面,點(diǎn)擊Profile則顯示個(gè)人信息頁(yè)面。
??
打開Xcode并新建一個(gè)EmptyApplication,將其命名為PKRevealControllerDemo
下載PKRevealController項(xiàng)目并將其中的PKRevealController/Controller文件夾復(fù)制到項(xiàng)目中
打開RevealControllerAppDelegate.h文件,在其中引入#import "PKRevealController.h",之后聲明一個(gè)PKRevealController類型的對(duì)象命名為revealController
#import <UIKit/UIKit.h> #import "PKRevealController.h" @interface RevealControllerAppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @property (strong,nonatomic) PKRevealController *revealController; @end新建用以顯示主界面的Controller,將其命名為MainFaceController。為了簡(jiǎn)單起見,界面設(shè)計(jì)等使用xib布局就好。在界面上隨便拖動(dòng)一個(gè)組建,并顯示相關(guān)數(shù)據(jù)。為了簡(jiǎn)便,我在界面上放置一個(gè)UILabel組件,并在其中顯示問(wèn)候信息。核心代碼如下:
- (void)viewDidLoad {[super viewDidLoad]; //設(shè)置當(dāng)前標(biāo)題 [self setTitle:@"Home"]; //設(shè)置標(biāo)題欄上左邊的按鈕 UIBarButtonItem *btnLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showLeftView)]; self.navigationItem.leftBarButtonItem = btnLeft; } //按鈕點(diǎn)擊事件 - (void) showLeftView {[self.navigationController.revealController showViewController:self.navigationController.revealController.leftViewController]; }新建用以顯示左邊菜單欄的Controller,將其命名為L(zhǎng)eftFaceController。一般情況下,菜單欄可以使用UITableView實(shí)現(xiàn)。表格數(shù)據(jù)填充參考:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; }- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 2; }- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CellReuseIndentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) {cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];} switch (indexPath.row) { case 0:[cell.textLabel setText:@"Home"]; break; case 1:[cell.textLabel setText:@"Profile"]; break;} return cell; }表格中點(diǎn)擊事件參考:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UINavigationController *frontViewController = nil; switch (indexPath.row){ case 0: //home frontViewController = [[UINavigationController alloc] initWithRootViewController:self.mainFaceController]; break; case 1: //profile frontViewController = [[UINavigationController alloc] initWithRootViewController:self.profileViewController]; break;}[self.revealController setFrontViewController:frontViewController];[self.revealController showViewController:self.revealController.frontViewController];[tableView deselectRowAtIndexPath:indexPath animated:YES]; }新建用以顯示個(gè)人信息頁(yè)面的Controller,將其命名為ProfileViewController,其內(nèi)容大致于MainFaceController類似,因此就不再詳細(xì)描述。(請(qǐng)看代碼)
回到RevealControllerAppDelegate.m,在其中并初始化主界面MainFaceController、左邊菜單欄LeftFaceController、PKRevealController等對(duì)象,并將其作為rootViewController展示給用戶
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; //主界面 MainFaceController* mainFaceController = [[MainFaceController alloc] init]; //菜單欄 LeftFaceController* leftFaceController = [[LeftFaceController alloc] init]; //構(gòu)造PKRevealController對(duì)象 UINavigationController *frontViewController = [[UINavigationController alloc] initWithRootViewController:mainFaceController]; self.revealController = [PKRevealController revealControllerWithFrontViewController:frontViewController leftViewController:leftFaceController options:nil]; //將其PKRevealController對(duì)象作為RootViewController self.window.backgroundColor = [UIColor whiteColor]; self.window.rootViewController = self.revealController;[self.window makeKeyAndVisible]; return YES; }完整代碼下載:
https://git.oschina.net/barrywey/PKRevealController-Demo
https://github.com/barrywey/PKRevealController-Demo
轉(zhuǎn)載于:https://my.oschina.net/u/1244672/blog/538983
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的iOS开源库PKRevealController的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 《从零开始学Swift》学习笔记(Day
- 下一篇: 设置ArcGIS的外观改回到出厂