侧边栏


在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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值