您所在的位置:小祥子 » 编程 » IOS » 正文

左右侧滑菜单功能的实现

时间:2015-08-10 编辑:踏浪帅 来源:Cnblogs

左右侧滑功能是比较常见的效果,此实例实现如下的效果:

这边使用到的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;

@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:vc
                                                             withSlideOutAnimation:self.slideOutAnimationEnabled
                                                                     andCompletion:nil];
}
@end

注意:这边主要是进行导航跳转时要注意,popToRootViewControllerAnimated跟popToRootAndSwitchToViewController

4:右边的rightViewController

#import "rightViewController.h"

@implementation rightViewController
-(void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor blueColor];
}
@end

源代码下载地址:左右侧滑菜单源代码

关键词:菜单 功能