iOS中侧边栏的添加

来源:互联网 发布:注册域名免费 编辑:程序博客网 时间:2024/05/19 12:37
原文地址:iOS中侧边栏的添加作者:伤心的小果冻
1.添加系统框架
[转载]iOS中侧边栏的添加

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

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

// MenuViewController.h

// sideTableView

//

//  Created by Dongon 13-9-26.

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

//


#import<UIKit/UIKit.h>


@interface MenuViewController :UIViewController<UITableViewDataSource,UITableViewDelegate>



@end


 

//

// MenuViewController.m

//  sideTableView

//

//  Created by Dong on13-9-26.

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

//


#import"MenuViewController.h"

#import"UIApplication+util.h"

@interfaceMenuViewController ()

@property(nonatomic,retain) UITableView *tableView;

@property(nonatomic,retain) NSArray *datasource;

@end


@implementation MenuViewController


-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView

{

    return1;

}


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

{

   return self.datasource.count;

}


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

{

    staticNSString *reuseID =@"reuseId";

   UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:reuseID];

   

    if (!cell){

       cell =[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:reuseID];

   }

   

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

    returncell;

}


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

{

   [tableViewdeselectRowAtIndexPath:indexPath animated:YES];

   

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

   

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

      [[[UIApplicationsharedApplication] 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.framestyle:UITableViewStyleGrouped];

       _tableView.dataSource = self;

       _tableView.delegate = self;

   }

   return _tableView;

}


-(void)buildLayout

{

   [self.view addSubview:self.tableView];

}


#pragma mark -

#pragma mark NESMenuViewControllerlifecycle


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

{

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

    if (self){

       //Custom initialization

   }

   return self;

}



-(void)viewDidLoad

{

   [super viewDidLoad];

   [selfbuildLayout];

// Do any additional setup after loading theview.

}


-(void)didReceiveMemoryWarning

{

   [super didReceiveMemoryWarning];

   // Dispose of any resources that can berecreated.

}


@end


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

// BaseViewController.h

// sideTableView

//

//  Created by Dongon 13-9-26.

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

//


#import<UIKit/UIKit.h>


@interface BaseViewController :UIViewController


@end


 

//

// BaseViewController.m

//  sideTableView

//

//  Created by Dong on13-9-26.

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

//

#import"UIApplication+util.h"

#import"BaseViewController.h"

#import"MenuViewController.h"

@interfaceBaseViewController ()


@end


@implementation BaseViewController


-(void)showSideViewHandler:(id)sender

{

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

   [[[UIApplication sharedApplication] sideViewController] pushViewController:menu onDirection:PPRevealSideDirectionLeftanimated:YES];

}

- (id)initWithNibName:(NSString *)nibNameOrNilbundle:(NSBundle*)nibBundleOrNil

{

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

    if (self){

       //Custom initialization

   }

   return self;

}


- (void)viewDidLoad

{

   [super viewDidLoad];

   

   UISwipeGestureRecognizer *gesture = [[UISwipeGestureRecognizeralloc] init];

    [gestureaddTarget:self action:@selector(showSideViewHandler:)];

   gesture.direction =UISwipeGestureRecognizerDirectionRight;

   [self.viewaddGestureRecognizer:gesture];

   

   self.view.backgroundColor = [UIColor whiteColor];

// Do any additional setup after loading theview.

}


- (void)didReceiveMemoryWarning

{

   [super didReceiveMemoryWarning];

   // Dispose of any resources that can berecreated.

}


@end


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

// MainViewController.h

//  sideTableView

//

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

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

//


#import"BaseViewController.h"


@interface MainViewController : BaseViewController


@end


 

//

// MainViewController.m

// sideTableView

//

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

// Copyright (c) 2013dong. 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];

   [[[UIApplicationsharedApplication] sideViewController] pushViewController:menu onDirection:PPRevealSideDirectionLeftanimated:YES];

}


#pragma mark -

#pragma mark initmethods


-(void)buildLayout

{

   self.view.backgroundColor = [UIColor whiteColor];

    

   UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

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

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

    [btnaddTarget: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 *)nibNameOrNilbundle:(NSBundle*)nibBundleOrNil

{

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

    if(self) {

       // Custominitialization

    }

   return self;

}


- (void)viewDidLoad

{

   [superviewDidLoad];

   [selfbuildLayout];

// Do any additionalsetup after loading the view.

}


- (void)didReceiveMemoryWarning

{

   [superdidReceiveMemoryWarning];

    //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) 2013dong. All rights reserved.

//


#import "HUDViewController.h"

#import "SVProgressHUD.h"

@interface HUDViewController ()


@end


@implementation HUDViewController


- (id)initWithNibName:(NSString *)nibNameOrNilbundle:(NSBundle*)nibBundleOrNil

{

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

    if(self) {

       // Custominitialization

    }

   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:

          [SVProgressHUDshowWithMaskType:SVProgressHUDMaskTypeGradient];

          break;

       case 50:

          [SVProgressHUD dismiss];

          break;

       case 60:

          [SVProgressHUDdismissWithSuccess:@"操作成功"];

          break;

       case 70:

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

          break;

       default:

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

          break;

    }

}


- (void)viewDidLoad

{

   [superviewDidLoad];

    

   UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

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

    [btnsetTitle:@"显示HUD"forState:UIControlStateNormal];

   btn.tag = 10;

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

   [self.view addSubview:btn];

    

    btn =[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

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

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

   btn.tag = 20;

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

   [self.view addSubview:btn];

    

    btn =[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

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

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

   btn.tag = 30;

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

   [self.view addSubview:btn];

    

    btn =[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

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

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

   btn.tag = 40;

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

   [self.view addSubview:btn];

    

    btn =[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

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

    [btnsetTitle:@"关闭HUD"forState:UIControlStateNormal];

   btn.tag = 50;

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

   [self.view addSubview:btn];

    

    btn =[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

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

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

   btn.tag = 60;

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

   [self.view addSubview:btn];

    

    btn =[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

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

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

   btn.tag = 70;

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

   [self.view addSubview:btn];

    

    btn =[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

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

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

   btn.tag = 80;

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

   [self.view addSubview:btn];

// Do any additionalsetup after loading the view.

}


- (void)didReceiveMemoryWarning

{

   [superdidReceiveMemoryWarning];

    //Dispose of any resources that can be recreated.

}


@end



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

// ActivityBarViewController.h

//  sideTableView

//

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

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

//


#import"BaseViewController.h"


@interface ActivityBarViewController : BaseViewController


@end


 

//

// ActivityBarViewController.m

// sideTableView

//

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

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

//


#import "ActivityBarViewController.h"

#import "ZAActivityBar.h"

@interface ActivityBarViewController()


@end


@implementationActivityBarViewController


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

{

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

    if (self) {

       // Custominitialization

    }

   return self;

}

#defineZBAR_ACTION_1 @"action1"

#defineZBAR_ACTION_2 @"action2"


-(void)btnClicked:(UIButton *)btn

{

    switch (btn.tag) {

       case 10:

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

          [ZAActivityBar showWithStatus:ZBAR_ACTION_1forAction:ZBAR_ACTION_1];

           break;

       case 20:

          [ZAActivityBar showWithStatus:ZBAR_ACTION_2forAction:ZBAR_ACTION_2];

           break;

       case 30:

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

           break;

       case 40:

          [ZAActivityBarshowErrorWithStatus:ZBAR_ACTION_1@"fail" forAction:ZBAR_ACTION_1];

           break;

       case 50:

          [ZAActivityBarshowSuccessWithStatus:ZBAR_ACTION_2@" success" forAction:ZBAR_ACTION_2];

           break;

       case 60:

          [ZAActivityBarshowErrorWithStatus:ZBAR_ACTION_2@"fail" forAction:ZBAR_ACTION_2];

           break;

       case 70:

          [ZAActivityBar dismissForAction:ZBAR_ACTION_1];

           break;

       default:

          [ZAActivityBar dismiss];

           break;

    }

}


-(void)viewDidLoad

{

   [superviewDidLoad];

    

   UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

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

    [btnsetTitle:@"显示Zbar1"forState:UIControlStateNormal];

    btn.tag = 10;

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

    [self.view addSubview:btn];

    

    btn =[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

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

    [btnsetTitle:@"显示Zbar2"forState:UIControlStateNormal];

    btn.tag = 20;

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

    [self.view addSubview:btn];

    

    btn =[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

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

    [btnsetTitle:@"Zbar1成功"forState:UIControlStateNormal];

    btn.tag = 30;

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

    [self.view addSubview:btn];

    

    btn =[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

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

    [btnsetTitle:@"Zbar1失败"forState:UIControlStateNormal];

    btn.tag = 40;

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

    [self.view addSubview:btn];

    

    btn =[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

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

    [btnsetTitle:@"Zbar2成功"forState:UIControlStateNormal];

    btn.tag = 50;

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

    [self.view addSubview:btn];

    

    btn =[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

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

    [btnsetTitle:@"Zbar2失败"forState:UIControlStateNormal];

    btn.tag = 60;

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

    [self.view addSubview:btn];

    

    btn =[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

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

    [btnsetTitle:@"取消Zbar1"forState:UIControlStateNormal];

    btn.tag = 70;

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

    [self.view addSubview:btn];

    

    btn =[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

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

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

    btn.tag = 80;

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

    [self.view addSubview:btn];

    

    

// Do any additionalsetup after loading the view.

}


-(void)didReceiveMemoryWarning

{

   [superdidReceiveMemoryWarning];

    //Dispose of any resources that can be recreated.

}



@end


(4)ActivityBarViewController(音频播放)


//  AudioViewController.h

//  sideTableView

//

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

//  Copyright (c) 2013dong. 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) 2013dong. 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


@implementationAudioViewController


-(id)initWithNibName:(NSString *)nibNameOrNilbundle:(NSBundle*)nibBundleOrNil

{

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

    if (self) {

       // Custominitialization

    }

   return self;

}


-(void)btnClicked:(UIButton *)sender

{

    if (self.isPlaying) {

       [self.timer invalidate];

       [sendersetTitle:@"播放"forState:UIControlStateNormal];

       [self.player pause];

       self.isPlaying = NO;

       self.slider.enabled = NO;

    }

    else

    {

       self.timer= [NSTimerscheduledTimerWithTimeInterval:1target:self selector:@selector(playCount:) userInfo:nilrepeats:YES];

       [sendersetTitle:@"暂停"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.timerinvalidate];

}


-(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(@"播放器加载失败:%@",[errorlocalizedDescription]);

       return;

    }

   [self.playerprepareToPlay];

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

    self.player.delegate = self;

}


-(void)changeCurrentTime:(UISlider *)sender

{

   NSUInteger total = self.player.duration;

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

}


-(void)viewDidLoad

{

   [superviewDidLoad];

   [selfinitAudioPlayer];

    

   UIButton *btn= [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

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

    btn.tag = 10;

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

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

    [self.view addSubview:btn];

    

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

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

    self.slider.enabled = NO;

    [self.view addSubview:self.slider];

    

   self.timeLab =[[UILabelalloc]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

{

   [superdidReceiveMemoryWarning];

   // Dispose of any resources that can berecreated.

}


@end


呼。。。若干好多代码

原创粉丝点击