程序员iOS开发好文JC专题

ios左右侧滑框架汇总

2014-03-13  本文已影响2760人  Ghstart

其实这种开源框架github上已经好多好多了,但是总是没找到适合自己的,找了好久才找到。(我的要求,即可以左右滑动,也可以点击滑动,并且可拓展性好。)下面总结两种,也是我自己用的。

1.IIViewDeckController,这是我在下载ShareSDK的时候,发现他的demo里用的这种,感觉很不错,挤拿过来用了。

在AppDelegate.m中

#import "CenterViewController.h"    //中间的视图控制器
#import "LeftViewController.h"        //左边的视图控制器
#import "RightViewController.h"     //右边的视图控制器


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    CenterViewController *centerVC  = [[CenterViewController alloc] init];
    UINavigationController *centerNav = [[UINavigationController alloc] initWithRootViewController:centerVC];
    
    LeftViewController *leftVC      = [[LeftViewController alloc] init];
    RightViewController *rightVC    = [[RightViewController alloc] init];
    
    self.viewController = [[IIViewDeckController alloc] initWithCenterViewController:centerNav leftViewController:leftVC rightViewController:rightVC];//可以只添加左边,也可以只添加右边,具体看里面的代码
    
    self.window.backgroundColor = [UIColor whiteColor];
    
    self.window.rootViewController = self.viewController;
    
    [self.window makeKeyAndVisible];
    return YES;
}

在CenterViewController.m中

- (void)viewDidLoad
{
    [super viewDidLoad];
        
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [rightBtn setTitle:@"right" forState:UIControlStateNormal];
    rightBtn.frame = CGRectMake(0.0, 0.0, 53.0, 30.0);
    [rightBtn addTarget:self action:@selector(rightButtonClickHandler:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}

-(void)rightButtonClickHandler:(UIButton *)button
{
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [app.viewController toggleRightViewAnimated:YES];
}

2.叫MKDSlideViewController
在AppDelegate.m中

//MainViewController为UITabBarControllers,里面放了viewControllers

    MainViewController *main = [[MainViewController alloc] init];
    LeftViewController *left = [[LeftViewController alloc] init];
    RightViewController *right = [[RightViewController alloc] init];
    
    _slideViewController = [[MKDSlideViewController alloc] initWithMainViewController:main];
    _slideViewController.leftViewController = left;
    _slideViewController.rightViewController = right;
    
    self.window.rootViewController = self.slideViewController;

在控制器的.m文件中

- (void)viewDidLoad
{
    [super viewDidLoad];

    //
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, [UIApplication sharedApplication].statusBarFrame.size.height, 44, 44);
    [leftBtn setTitle:@"左" forState:UIControlStateNormal];
    [leftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftItemClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
}

#pragma mark - Action Methods
//这里用的是通知
-(void)leftItemClick
{
    [[NSNotificationCenter defaultCenter] postNotificationName:kShowLeftControllertNotification object:nil];
}

具体的源码github里可以自己下载。

上一篇下一篇

猜你喜欢

热点阅读