RESideMenu组件用法

来源:互联网 发布:java调用接口上传文件 编辑:程序博客网 时间:2024/04/28 00:12

Github上有个不错的侧边菜单控件,点击当前视图导航栏的左菜单按钮,视图会一边缩小一边移动到屏幕右方,而屏幕左半部分将出现菜单列表。点击菜单上的某个按钮,新视图将从屏幕右方一边放大一边移到中间,最后变成当前视图。


源码地址:https://github.com/romaonthego/RESideMenu


可以利用这个组件封装好的方法,快速搭建UI。

一个DEMO的结构如下:



导入RESideMenu的源文件后即可直接使用。

Controllers中分别建立了四个类,FirstViewController控制DEMO的主界面,SecondViewController控制了点击侧边栏按钮后所显示的一个视图。LeftMenuViewController与RightMenuViewController分别控制左按钮与右按钮点击后所显示的侧边栏视图。

首先来看FirstViewController

- (void)viewDidLoad{    [super viewDidLoad];self.title = @"First Controller";    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Left"                                                                             style:UIBarButtonItemStylePlain                                                                            target:self                                                                            action:@selector(presentLeftMenuViewController:)];    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Right"                                                                             style:UIBarButtonItemStylePlain                                                                            target:self                                                                            action:@selector(presentRightMenuViewController:)];        UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];    imageView.contentMode = UIViewContentModeScaleAspectFill;    imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;    imageView.image = [UIImage imageNamed:@"Balloon"];    [self.view addSubview:imageView];}
主要的viewDidLoad方法首先定义了导航栏的标题,然后分别定义左按钮和右按钮,将代理方法设定为presentXXXMenuViewController方法,这个方法是RESideMenu中写好的,负责点击按钮后显示侧边栏的效果。接着设定了背景的一系列属性。

SecondViewController

- (void)viewDidLoad{    [super viewDidLoad];self.title = @"Second Controller";    self.view.backgroundColor = [UIColor colorWithRed:255/255.0 green:202/255.0 blue:101/255.0 alpha:1.0];    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Left"                                                                             style:UIBarButtonItemStylePlain                                                                            target:self                                                                            action:@selector(presentLeftMenuViewController:)];    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Right"                                                                              style:UIBarButtonItemStylePlain                                                                             target:self                                                                             action:@selector(presentRightMenuViewController:)];        [self.view addSubview:({        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];        button.frame = CGRectMake(0, 84, self.view.frame.size.width, 44);        button.autoresizingMask = UIViewAutoresizingFlexibleWidth;        [button setTitle:@"Push View Controller" forState:UIControlStateNormal];        [button addTarget:self action:@selector(pushViewController:) forControlEvents:UIControlEventTouchUpInside];        button;    })];}- (void)pushViewController:(id)sender{    UIViewController *viewController = [[UIViewController alloc] init];    viewController.title = @"Pushed Controller";    viewController.view.backgroundColor = [UIColor whiteColor];    [self.navigationController pushViewController:viewController animated:YES];}

这个视图有一个按钮,按钮方法为pushViewController,这个方法先初始化了一个控制器,然后设定了这个控制器所控制视图的属性,点击这个按钮后,会显示这个视图。


LeftViewController与RightViewController大致差不多,LeftViewController给其中两个按钮设置了方法。

@implementation DEMOLeftMenuViewController- (void)viewDidLoad{    [super viewDidLoad];    self.tableView = ({        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, (self.view.frame.size.height - 54 * 5) / 2.0f, self.view.frame.size.width, 54 * 5) style:UITableViewStylePlain];        tableView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;        tableView.delegate = self;        tableView.dataSource = self;        tableView.opaque = NO;        tableView.backgroundColor = [UIColor clearColor];        tableView.backgroundView = nil;        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;        tableView.bounces = NO;        tableView;    });    [self.view addSubview:self.tableView];}#pragma mark -#pragma mark UITableView Delegate- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    [tableView deselectRowAtIndexPath:indexPath animated:YES];    switch (indexPath.row) {        case 0:            [self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[[DEMOFirstViewController alloc] init]]                                                         animated:YES];            [self.sideMenuViewController hideMenuViewController];            break;        case 1:            [self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[[DEMOSecondViewController alloc] init]]                                                         animated:YES];            [self.sideMenuViewController hideMenuViewController];            break;        default:            break;    }}#pragma mark -#pragma mark UITableView Datasource- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    return 54;}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex{    return 5;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *cellIdentifier = @"Cell";        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];        if (cell == nil) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];        cell.backgroundColor = [UIColor clearColor];        cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:21];        cell.textLabel.textColor = [UIColor whiteColor];        cell.textLabel.highlightedTextColor = [UIColor lightGrayColor];        cell.selectedBackgroundView = [[UIView alloc] init];    }        NSArray *titles = @[@"Home", @"Calendar", @"Profile", @"Settings", @"Log Out"];    NSArray *images = @[@"IconHome", @"IconCalendar", @"IconProfile", @"IconSettings", @"IconEmpty"];    cell.textLabel.text = titles[indexPath.row];    cell.imageView.image = [UIImage imageNamed:images[indexPath.row]];        return cell;}@end
侧边栏视图中的按钮实际上是以表格布局排列,viewDidload方法定义了这个表格视图的一系列属性。接下来didSelectRowAtIndexPath方法给第一个按钮和第二个按钮设置了代理方法,点击第一个按钮会显示FirstViewController的视图,点击第二个会显示SecondViewController的视图。最后给每个按钮设置了名称与图标。


开发者只需要对ios的表视图有些基本的了解,就可以非常熟练的利用RESideMenu方法了。




0 0
原创粉丝点击