在storyboard里面要拖入一个UIview放在紧邻视图之外(左边侧栏放在左边,右视图放在右边)
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
BOOL flag;
}
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (weak, nonatomic) IBOutlet UIView *leftView;
@property (weak, nonatomic) IBOutlet UILabel *label;
- (IBAction)menu:(id)sender;
@property (strong, nonatomic) NSArray *nameArray;
@property (strong, nonatomic) UISwipeGestureRecognizer *leftSwipeGesture;
@property (strong, nonatomic) UISwipeGestureRecognizer *rightSwipeGesture;
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableview.dataSource = self;
self.tableview.delegate = self;
self.nameArray = [[NSArray alloc]initWithObjects:@"菜单一",@"菜单二",@"菜单三", nil];
flag = NO;
self.leftSwipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipe:)];
self.leftSwipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
self.rightSwipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipe:)];
self.rightSwipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
[self.leftView addGestureRecognizer:self.leftSwipeGesture];
[self.leftView addGestureRecognizer:self.rightSwipeGesture];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.nameArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentify = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify];
}
cell.textLabel.text = [self.nameArray objectAtIndex:indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
self.label.text = [self.nameArray objectAtIndex:indexPath.row];
[self back];
}
- (IBAction)menu:(id)sender {
if (flag == NO) {
[self show];
}
else{
[self back];
}
}
-(void)show{
CGRect tableviewRect = self.tableview.frame;
CGRect viewRect = self.leftView.frame;
tableviewRect.origin.x = 220.0f;
viewRect.origin.x = -100.0f;
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.tableview.frame = tableviewRect;
self.leftView.frame = viewRect;
} completion:^(BOOL finished) {
flag = YES;
}];
}
-(void)back{
CGRect tableviewRect = self.tableview.frame;
CGRect viewRect = self.leftView.frame;
tableviewRect.origin.x = 320.0f;
viewRect.origin.x = 0.0f;
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.tableview.frame = tableviewRect;
self.leftView.frame = viewRect;
} completion:^(BOOL finished) {
flag = NO;
}];
}
-(void)handleSwipe:(UISwipeGestureRecognizer *)sender{
if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
[self show];
}
if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
[self back];
}
}
@end