左右侧滑菜单功能的实现
生活随笔
收集整理的這篇文章主要介紹了
左右侧滑菜单功能的实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
左右側滑功能是比較常見的效果,此實例實現如下的效果:
這邊使用到的SlideNavigationController開源類(引入源代碼中的Source),其為NavigationController子類,在運用程序AppDelegate就設置為其根視圖;主要代碼如下:
1:AppDelegate主要代碼如下:
#import "AppDelegate.h" #import "SlideNavigationController.h" #import "leftViewController.h" #import "rightViewController.h" #import "ViewController.h"- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];self.window.backgroundColor = [UIColor whiteColor];//設置根導航視圖ViewController *homeVc = [[ViewController alloc] init];[self.window setRootViewController:[[SlideNavigationController alloc] initWithRootViewController:homeVc]];
//設置左右視圖leftViewController* leftController=[[leftViewController alloc]init];rightViewController* rightController=[[rightViewController alloc]init];[SlideNavigationController sharedInstance].rightMenu = rightController;[SlideNavigationController sharedInstance].leftMenu = leftController;[SlideNavigationController sharedInstance].menuRevealAnimationDuration = .18;[self.window makeKeyAndVisible];return YES; }
2:主頁面ViewController代碼:
#import <UIKit/UIKit.h> #import "SlideNavigationController.h" @interface ViewController : UIViewController<SlideNavigationControllerDelegate>@end #import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor=[UIColor yellowColor];self.title=@"首頁";UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];[button setTitle:@"右邊" forState:UIControlStateNormal];[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];[button addTarget:[SlideNavigationController sharedInstance] action:@selector(toggleRightMenu) forControlEvents:UIControlEventTouchUpInside];UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];[SlideNavigationController sharedInstance].rightBarButtonItem = rightBarButtonItem; }- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated. }#pragma mark - SlideNavigationController Methods -- (BOOL)slideNavigationControllerShouldDisplayLeftMenu {return YES; }- (BOOL)slideNavigationControllerShouldDisplayRightMenu {return YES; }@end注意要實現SlideNavigationControllerDelegate的兩個是否有左跟右的菜單,還可以設置其導航欄的按鍵樣式,如果沒有設置會像左邊出現的這種默認的;
3:左邊視圖leftViewController
#import <UIKit/UIKit.h> #import "SlideNavigationController.h" #import "OneViewController.h" #import "TwoViewController.h"@interface leftViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>@end #import "leftViewController.h"@interface leftViewController() @property(nonatomic,strong)UITableView *tableView; @property(strong,nonatomic) NSArray *listData;@property(assign,nonatomic) bool slideOutAnimationEnabled; @end@implementation leftViewController-(void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor=[UIColor redColor];[self ininLoadTable]; }-(void)ininLoadTable {self.tableView=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];self.tableView.delegate=self;self.tableView.dataSource=self;[self.view addSubview:self.tableView];self.listData=[[NSArray alloc] initWithObjects:@"朋友圈",@"個人好友",@"最近聯系人", nil]; }#pragma mark - UITableView Delegate & Datasrouce -- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return self.listData.count; }- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 20)];view.backgroundColor = [UIColor clearColor];return view; }- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {return 20; }- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];if (cell==nil) {cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];}cell.textLabel.text=self.listData[indexPath.row];cell.backgroundColor = [UIColor clearColor];return cell; }//跳轉 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {UIViewController *vc ;switch (indexPath.row){case 0:vc = [[OneViewController alloc]init];break;case 1:vc = [[TwoViewController alloc]init];break;case 2:vc = [[OneViewController alloc]init];break;case 3:[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];[[SlideNavigationController sharedInstance] popToRootViewControllerAnimated:YES];return;break;}[[SlideNavigationController sharedInstance] popToRootAndSwitchToViewController:vcwithSlideOutAnimation:self.slideOutAnimationEnabledandCompletion:nil]; } @end
注意:這邊主要是進行導航跳轉時要注意,popToRootViewControllerAnimated跟popToRootAndSwitchToViewController
4:右邊的rightViewController
#import "rightViewController.h"@implementation rightViewController -(void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor=[UIColor blueColor]; } @end?
附:另外二個插件也實現更好的效果,地址如下(https://github.com/JVillella/JVFloatingDrawer)效果圖如下:
?另一個地址如下:(https://github.com/hujewelz/HUSliderMenu)效果圖如下:
源代碼下載地址:左右側滑菜單源代碼
轉載于:https://www.cnblogs.com/wujy/p/4718002.html
總結
以上是生活随笔為你收集整理的左右侧滑菜单功能的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大约xib连接错误bug正确
- 下一篇: @清晰掉 GNU C __attribu