iOS中侧边栏的添加

1.添加系统框架
[转载]iOS中侧边栏的添加

2.添加三方类库
[转载]iOS中侧边栏的添加

3.创建一个MenuViewController作为侧边滑动时候显示的视图

//  MenuViewController.h

//  sideTableView

//

//  Created by Dong on 13-9-26.

//  Copyright (c) 2013 dong. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface MenuViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>



@end


 

//

//  MenuViewController.m

//  sideTableView

//

//  Created by Dong on 13-9-26.

//  Copyright (c) 2013 dong. All rights reserved.

//


#import "MenuViewController.h"

#import "UIApplication+util.h"

@interface MenuViewController ()

@property (nonatomic,retain) UITableView *tableView;

@property (nonatomic,retain) NSArray *datasource;

@end


@implementation MenuViewController


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 1;

}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return self.datasource.count;

}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *reuseID = @"reuseId";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];

    

    if (!cell) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseID];

    }

    

    cell.textLabel.text = [self.datasource objectAtIndex:indexPath.row];

    return cell;

}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    

    NSString *rootViewControllerName = NSStringFromClass([[[UIApplication sharedApplication] sideViewController].rootViewController class]);

    

    if ([[self.datasource objectAtIndex:indexPath.row] isEqualToString:rootViewControllerName]) {

        [[[UIApplication sharedApplication] sideViewController] popViewControllerAnimated:YES];

        NSLog(@"直接弹出");

        return;

    }

    

    UIViewController *vc = [[NSClassFromString([self.datasource objectAtIndex:indexPath.row]) alloc] init];

    [[[UIApplication sharedApplication] sideViewController] popViewControllerWithNewCenterController:vc animated:YES];

}


#pragma mark -

#pragma mark init methods


-(NSArray *)datasource

{

    if (!_datasource) {

        _datasource = [[NSArray alloc] initWithObjects:@"MainViewController",

                       @"HUDViewController",

                       @"ActivityBarViewController",

                       @"AudioViewController",

                       nil];

    }

    return _datasource;

}


-(UITableView *)tableView

{

    if (!_tableView) {

        _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];

        _tableView.dataSource = self;

        _tableView.delegate = self;

    }

    return _tableView;

}


-(void)buildLayout

{

    [self.view addSubview:self.tableView];

}


#pragma mark -

#pragma mark NESMenuViewController lifecycle


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}



- (void)viewDidLoad

{

    [super viewDidLoad];

    [self buildLayout];

// Do any additional setup after loading the view.

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


4.创建一个baseViewController作为所有侧边栏点击时候出现视图的父类

//  BaseViewController.h

//  sideTableView

//

//  Created by Dong on 13-9-26.

//  Copyright (c) 2013 dong. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface BaseViewController : UIViewController


@end


 

//

//  BaseViewController.m

//  sideTableView

//

//  Created by Dong on 13-9-26.

//  Copyright (c) 2013 dong. All rights reserved.

//

#import "UIApplication+util.h"

#import "BaseViewController.h"

#import "MenuViewController.h"

@interface BaseViewController ()


@end


@implementation BaseViewController


-(void)showSideViewHandler:(id)sender

{

    MenuViewController *menu = [[MenuViewController alloc] init];

    [[[UIApplication sharedApplication] sideViewController] pushViewController:menu onDirection:PPRevealSideDirectionLeft animated:YES];

}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    

    UISwipeGestureRecognizer *gesture = [[UISwipeGestureRecognizer alloc] init];

    [gesture addTarget:self action:@selector(showSideViewHandler:)];

    gesture.direction = UISwipeGestureRecognizerDirectionRight;

    [self.view addGestureRecognizer:gesture];

    

    self.view.backgroundColor = [UIColor whiteColor];

// Do any additional setup after loading the view.

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


5.创建四个子视图继承baseViewController用于点击左边的侧边栏时候显示的视图(此处视图名称和database初始化的时候一致!!!)
(1)MainViewController

//  MainViewController.h

//  sideTableView

//

//  Created by Dong on 13-9-26.

//  Copyright (c) 2013 dong. All rights reserved.

//


#import "BaseViewController.h"


@interface MainViewController : BaseViewController


@end


 

//

//  MainViewController.m

//  sideTableView

//

//  Created by Dong on 13-9-26.

//  Copyright (c) 2013 dong. All rights reserved.

//

#import "UIApplication+util.h"

#import "MainViewController.h"

#import "MenuViewController.h"

@interface MainViewController ()


@end


@implementation MainViewController

-(void)btnClicked:(id)sender

{

    MenuViewController *menu = [[MenuViewController alloc] init];

    [[[UIApplication sharedApplication] sideViewController] pushViewController:menu onDirection:PPRevealSideDirectionLeft animated:YES];

}


#pragma mark -

#pragma mark init methods


-(void)buildLayout

{

    self.view.backgroundColor = [UIColor whiteColor];

    

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn.frame = CGRectMake(10, 30, 300, 30);

    [btn setTitle:@"显示侧边栏" forState:UIControlStateNormal];

    [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    

    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(10, 70, 300, 30)];

    lab.text = @"在屏幕上右划也可打开侧边栏";

    lab.textAlignment = NSTextAlignmentCenter;

    [self.view addSubview:lab];

    

}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    [self buildLayout];

// Do any additional setup after loading the view.

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


(2)HUDViewController(用于显示系统转圈圈那个东西)

#import "BaseViewController.h"


@interface HUDViewController : BaseViewController


@end



 

//

//  HUDViewController.m

//  sideTableView

//

//  Created by Dong on 13-9-26.

//  Copyright (c) 2013 dong. All rights reserved.

//


#import "HUDViewController.h"

#import "SVProgressHUD.h"

@interface HUDViewController ()


@end


@implementation HUDViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


-(void)btnClicked:(UIButton *)btn

{

    switch (btn.tag) {

        case 10:

            [SVProgressHUD show];

            break;

        case 20:

            [SVProgressHUD showWithStatus:@"操作信息"];

            break;

        case 30:

            [SVProgressHUD showWithStatus:@"加了蒙板就可以重新启动项目了,这种操作需要使用代理方法关闭,刷新,下载什么的操作需要锁定用户界面操作时经常用" maskType:SVProgressHUDMaskTypeBlack];

            break;

        case 40:

            [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeGradient];

            break;

        case 50:

            [SVProgressHUD dismiss];

            break;

        case 60:

            [SVProgressHUD dismissWithSuccess:@"操作成功"];

            break;

        case 70:

            [SVProgressHUD dismissWithSuccess:@"操作成功,延迟3秒关闭" afterDelay:3];

            break;

        default:

            [SVProgressHUD dismissWithError:@"操作失败"];

            break;

    }

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn.frame = CGRectMake(10, 30, 300, 30);

    [btn setTitle:@"显示HUD" forState:UIControlStateNormal];

    btn.tag = 10;

    [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    

    btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn.frame = CGRectMake(10, 70, 300, 30);

    [btn setTitle:@"显示HUD带信息" forState:UIControlStateNormal];

    btn.tag = 20;

    [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    

    btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn.frame = CGRectMake(10, 110, 300, 30);

    [btn setTitle:@"显示HUD带信息蒙板" forState:UIControlStateNormal];

    btn.tag = 30;

    [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    

    btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn.frame = CGRectMake(10, 150, 300, 30);

    [btn setTitle:@"显示HUD蒙板" forState:UIControlStateNormal];

    btn.tag = 40;

    [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    

    btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn.frame = CGRectMake(10, 300, 300, 30);

    [btn setTitle:@"关闭HUD" forState:UIControlStateNormal];

    btn.tag = 50;

    [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    

    btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn.frame = CGRectMake(10, 340, 300, 30);

    [btn setTitle:@"关闭HUD带信息" forState:UIControlStateNormal];

    btn.tag = 60;

    [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    

    btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn.frame = CGRectMake(10, 380, 300, 30);

    [btn setTitle:@"关闭HUD带信息,延迟" forState:UIControlStateNormal];

    btn.tag = 70;

    [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    

    btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn.frame = CGRectMake(10, 420, 300, 30);

    [btn setTitle:@"关闭HUD带失败信息" forState:UIControlStateNormal];

    btn.tag = 80;

    [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

// Do any additional setup after loading the view.

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end



(3)ActivityBarViewController(用于显示系统转圈圈那个东西)

//  ActivityBarViewController.h

//  sideTableView

//

//  Created by Dong on 13-9-26.

//  Copyright (c) 2013 dong. All rights reserved.

//


#import "BaseViewController.h"


@interface ActivityBarViewController : BaseViewController


@end


 

//

//  ActivityBarViewController.m

//  sideTableView

//

//  Created by Dong on 13-9-26.

//  Copyright (c) 2013 dong. All rights reserved.

//


#import "ActivityBarViewController.h"

#import "ZAActivityBar.h"

@interface ActivityBarViewController ()


@end


@implementation ActivityBarViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}

#define ZBAR_ACTION_1 @"action1"

#define ZBAR_ACTION_2 @"action2"


-(void)btnClicked:(UIButton *)btn

{

    switch (btn.tag) {

        case 10:

            //第一个是参数,第二个是key,zbar本身支持多线程,可以同时存在多个消息,action用来标记某一个消息,作用相当于tag

            [ZAActivityBar showWithStatus:ZBAR_ACTION_1 forAction:ZBAR_ACTION_1];

            break;

        case 20:

            [ZAActivityBar showWithStatus:ZBAR_ACTION_2 forAction:ZBAR_ACTION_2];

            break;

        case 30:

            [ZAActivityBar showSuccessWithStatus:ZBAR_ACTION_1@" success" forAction:ZBAR_ACTION_1];

            break;

        case 40:

            [ZAActivityBar showErrorWithStatus:ZBAR_ACTION_1@" fail" forAction:ZBAR_ACTION_1];

            break;

        case 50:

            [ZAActivityBar showSuccessWithStatus:ZBAR_ACTION_2@" success" forAction:ZBAR_ACTION_2];

            break;

        case 60:

            [ZAActivityBar showErrorWithStatus:ZBAR_ACTION_2@" fail" forAction:ZBAR_ACTION_2];

            break;

        case 70:

            [ZAActivityBar dismissForAction:ZBAR_ACTION_1];

            break;

        default:

            [ZAActivityBar dismiss];

            break;

    }

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn.frame = CGRectMake(10, 30, 300, 30);

    [btn setTitle:@"显示Zbar1" forState:UIControlStateNormal];

    btn.tag = 10;

    [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    

    btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn.frame = CGRectMake(10, 70, 300, 30);

    [btn setTitle:@"显示Zbar2" forState:UIControlStateNormal];

    btn.tag = 20;

    [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    

    btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn.frame = CGRectMake(10, 110, 300, 30);

    [btn setTitle:@"Zbar1成功" forState:UIControlStateNormal];

    btn.tag = 30;

    [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    

    btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn.frame = CGRectMake(10, 150, 300, 30);

    [btn setTitle:@"Zbar1失败" forState:UIControlStateNormal];

    btn.tag = 40;

    [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    

    btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn.frame = CGRectMake(10, 190, 300, 30);

    [btn setTitle:@"Zbar2成功" forState:UIControlStateNormal];

    btn.tag = 50;

    [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    

    btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn.frame = CGRectMake(10, 230, 300, 30);

    [btn setTitle:@"Zbar2失败" forState:UIControlStateNormal];

    btn.tag = 60;

    [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    

    btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn.frame = CGRectMake(10, 270, 300, 30);

    [btn setTitle:@"取消Zbar1" forState:UIControlStateNormal];

    btn.tag = 70;

    [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    

    btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn.frame = CGRectMake(10, 310, 300, 30);

    [btn setTitle:@"全部取消" forState:UIControlStateNormal];

    btn.tag = 80;

    [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    

    

// Do any additional setup after loading the view.

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



@end


(4)ActivityBarViewController(音频播放)


//  AudioViewController.h

//  sideTableView

//

//  Created by Dong on 13-9-26.

//  Copyright (c) 2013 dong. All rights reserved.

//


#import "BaseViewController.h"

#import <AVFoundation/AVFoundation.h>

@interface AudioViewController : BaseViewController<AVAudioPlayerDelegate>


@end



//

//  AudioViewController.m

//  sideTableView

//

//  Created by Dong on 13-9-26.

//  Copyright (c) 2013 dong. All rights reserved.

//


#import "AudioViewController.h"


@interface AudioViewController ()

@property (nonatomic,retain) AVAudioPlayer *player;

@property (nonatomic,retain) NSTimer *timer;

@property (nonatomic,retain) UILabel *timeLab;

@property (nonatomic,retain) UISlider *slider;

@property (nonatomic,assign) BOOL isPlaying;

@end


@implementation AudioViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


-(void)btnClicked:(UIButton *)sender

{

    if (self.isPlaying) {

        [self.timer invalidate];

        [sender setTitle:@"播放" forState:UIControlStateNormal];

        [self.player pause];

        self.isPlaying = NO;

        self.slider.enabled = NO;

    }

    else

    {

        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(playCount:) userInfo:nil repeats:YES];

        [sender setTitle:@"暂停" forState:UIControlStateNormal];

        [self.player play];

        self.isPlaying = YES;

        self.slider.enabled = YES;

    }

}


-(void)playCount:(NSTimer *)timer

{

    NSUInteger currentTime = self.player.currentTime;

    NSUInteger totalTime = self.player.duration;

    

    self.slider.value = currentTime / (CGFloat)totalTime;

    

    // 1min = 60sec

    // totalTime单位是秒

    // 60*60 = 3600

    

    //    NSUInteger h = totalTime/3600;

    NSUInteger m = totalTime%(60*60)/60;

    NSUInteger s = totalTime%(60*60)%60%60;

    

    NSUInteger cm = currentTime%(60*60)/60;

    NSUInteger cs = currentTime%(60*60)%60%60;

    

    self.timeLab.text = [NSString stringWithFormat:@"%lu:%lu/%lu:%lu",cm,cs,m,s];

    

}


-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag

{

    NSLog(@"播放完成");

    self.isPlaying = NO;

    self.slider.value = 0;

    self.slider.enabled = NO;

    UIButton *btn = (UIButton *)[self.view viewWithTag:10];

    [btn setTitle:@"播放" forState:UIControlStateNormal];

    [self.timer invalidate];

}


-(void)initAudioPlayer

{

    NSString *music = [[NSBundle mainBundle] pathForResource:@"HelpMe" ofType:@"mp3"];

    NSURL *url = [NSURL fileURLWithPath:music];

    NSError *error;

    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];

    

    if (error) {

        NSLog(@"播放器加载失败:%@",[error localizedDescription]);

        return;

    }

    [self.player prepareToPlay];

    //所有的代理方法都是可选的

    self.player.delegate = self;

}


-(void)changeCurrentTime:(UISlider *)sender

{

    NSUInteger total = self.player.duration;

    self.player.currentTime = total * sender.value;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    [self initAudioPlayer];

    

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn.frame = CGRectMake(10, 30, 300, 30);

    btn.tag = 10;

    [btn setTitle:@"播放" forState:UIControlStateNormal];

    [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    

    self.slider = [[UISlider alloc] initWithFrame:CGRectMake(10, 70, 300, 0)];

    [self.slider addTarget:self action:@selector(changeCurrentTime:) forControlEvents:UIControlEventValueChanged];

    self.slider.enabled = NO;

    [self.view addSubview:self.slider];

    

    self.timeLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 110, 300, 30)];

    self.timeLab.backgroundColor = [UIColor clearColor];

    self.timeLab.textAlignment = NSTextAlignmentCenter;

    [self.view addSubview:self.timeLab];

    

    [self playCount:nil];

    

// Do any additional setup after loading the view.

}



- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


呼。。。若干好多代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值