码迷,mamicode.com
首页 > 其他好文 > 详细

抽屉效果重写

时间:2015-02-10 18:49:53      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:抽屉效果重写

//  这是主界面的内容,可以拖动主界面实现效果
//  MainViewController.m
//  WangYiDrawer
//
//  Created by xiaoyao on 15/2/9.
//  Copyright (c) 2015年 lijien. All rights reserved.
//

#import "MainViewController.h"
#import "LeftViewController.h"
#import "RightViewController.h"
#import <QuartzCore/QuartzCore.h>

#define kEndX frame.origin.x // 手势结束的x
#define kLeftViewWidth _leftView.frame.size.width // 左部view的宽度
#define kRightViewWidth _rightView.frame.size.width // 右部view的宽度

@interface MainViewController ()<LeftViewControllerDelegate> {
  CGFloat _startX;                             // 手势刚开始触发时的起点
  LeftViewController *_leftViewController;     // 左部视图控制器
  RightViewController *_rightViewController;   // 右边视图控制器
  NSMutableDictionary *_columnViewControllers; // 记录下所有已经实例化的控制器对象防止重复创建
  UIView *_mainView;
}
@end

@implementation MainViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  
  _mainView.frame = self.view.bounds;
  [self.view addSubview:_mainView];
  
  _titleButton.backgroundColor = [UIColor clearColor];
  _columnViewControllers = [[NSMutableDictionary alloc] init];
  
  // 将左侧视图控制器的视图添加到左侧view上
  _leftViewController = [[LeftViewController alloc] init];
  _leftViewController.view.frame = _leftView.frame;
  _leftViewController.delegate = self;
  [_leftView addSubview:_leftViewController.view];
  
  // 将右侧视图控制器的视图添加到左侧view上
  _rightViewController = [[RightViewController alloc] init];
  _rightViewController.view.frame = _rightView.frame;
  [_rightView addSubview:_rightViewController.view];
  
  UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
  [self.view addGestureRecognizer:pan];
  
 // 首次加载的时候就应该显示新闻的内容
  [self firstLoading];
}

// 隐藏状态栏
- (BOOL)prefersStatusBarHidden {
  return YES;
}

- (void)firstLoading {
  Column *column = [Column columnName:@"新闻" imgName:@"news.png" className:@"NewsViewController"];
  [self leftTableViewRowClicked:column];
}

- (void)panGesture:(UIPanGestureRecognizer *)pan {
  // 如果状态是刚刚开始拽就记录下当前的起始x
  if(UIGestureRecognizerStateBegan == pan.state) {
    _startX = self.view.frame.origin.x;
  }
  
  // 计算出平移拖动的距离
  CGPoint delta = [pan translationInView:self.view];
  CGRect frame = _mainView.frame;
  
  kEndX = _startX + delta.x;
  // 限制拖动范围
  if (kEndX >= kLeftViewWidth) {
    kEndX = kLeftViewWidth;
  }
  
  if (kEndX <= - kRightViewWidth) {
    kEndX = - kRightViewWidth;
  }
  
  // 由于leftView 和 rightView 是重合的所以必须要隐藏一个
  if (kEndX > 0) {
    _rightView.hidden = YES;
    _leftView.hidden = NO;
  } else {
    _rightView.hidden = NO;
    _leftView.hidden = YES;
  }
  
  if (UIGestureRecognizerStateEnded == pan.state) {
    if (_startX == 0 && delta.x > 0) {
      kEndX = kLeftViewWidth;
    } else if (_startX == 0 && delta.x < 0) {
      kEndX = - kRightViewWidth;
    } else if (_startX == kLeftViewWidth && delta.x < 0) {
      kEndX = 0;
    } else if (_startX == -kRightViewWidth && delta.x > 0) {
      kEndX = 0;
    }
  }
  
  // 最后重新设置mainView frame
  [UIView animateWithDuration:0.2 animations:^{
    _mainView.frame = frame;
  }];
  
  // 给mianView添加阴影图层
  [self addShadowForMianViewWithEndX:kEndX];
}

- (void)addShadowForMianViewWithEndX:(CGFloat)endX {
  _mainView.layer.shadowColor = [UIColor blackColor].CGColor;
  _mainView.layer.shadowOpacity = 0.5;
  if (endX > 0) {
    _mainView.layer.shadowOffset = CGSizeMake(-5, 0);
  } else {
    _mainView.layer.shadowOffset = CGSizeMake(5, 0);
  }
}

#pragma mark - leftViewDelegate
- (void)leftTableViewRowClicked:(Column *)rowSelected {
  Column *column = (Column *)rowSelected;
  // 1,关闭左边的控制=======================
  // 调用抽取出来的公共代码,设置mainView的x,参数是endX
  [self setmainViewX:0];
  // 2,更改标题按钮上面的文字
  _titleButton.titleLabel.text = column.columnName;
  
  // 根据栏目数据模型中的类名,实例化对应栏目的控制器,并且将其设置为导航控制器的根控制器,最后将导航控制器的view添加到mainView中,目的是方便设置导航条,以及,各控制器的跳转
  
  // 2,从缓存字典中取,如果子控制器字典有曾经创建过的子控制器,直接取出来用
  UIViewController *columnVC = _columnViewControllers[column.columnClassName];
  // 如果子控制器字典中没有保存过该栏目的控制器,才要创建子控制器
  if (columnVC == nil) {
    Class c = NSClassFromString(column.columnClassName);
    columnVC = [[c alloc]init];
    // 并且一定要将其放到 子控制器字典里面,存起来
    [_columnViewControllers setObject:columnVC forKey:column.columnClassName];
  }
  
  // 4,移除contentView中的正在显示的旧的子view
  if (_contentView.subviews.count > 0) {
    UIView *oldView = [_contentView subviews][0];
    [oldView removeFromSuperview];
  }
  
  // 5,最后将子控制器的view添加到contentView中,显示
  columnVC.view.frame = _contentView.bounds;
  [self.contentView addSubview:columnVC.view];
  NSLog(@"%@",self.contentView);
  // 在添加到mainView之前 ,先得到mainView导航控制器的子控制器,并将其移除(如果有的话)
}

// 抽取出来的公共代码,设置mainView的x,参数是endX
- (void)setmainViewX:(CGFloat)endX
{
  CGRect frame = _mainView.frame;
  frame.origin.x = endX;
  [UIView animateWithDuration:0.2 animations:^{
    _mainView.frame = frame;
  }];
  
}
@end
/**************** 左侧表格设定代理 *******/
// 
//  LeftViewController.m
//  WangYiDrawer
//
//  Created by xiaoyao on 15/2/9.
//  Copyright (c) 2015年 lijien. All rights reserved.
//

#import "LeftViewController.h"

@interface LeftViewController () {
  NSArray *_classNameArray;
  UITableView *_tableView;
}
@end

@implementation LeftViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  
  _tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
  [self.view addSubview:_tableView];
  
  NSArray *titleArray = @[@"新闻", @"图片", @"跟帖"];
  NSArray *picArray = @[@"news.png", @"pic.png", @"comment.png"];
  
  Column *newsColumn = [Column columnName:titleArray[0] imgName:picArray[0] className:@"NewsViewController"];
  Column *picColumn = [Column columnName:titleArray[1] imgName:picArray[1] className:@"PicViewController"];
  Column *commentColumn = [Column columnName:titleArray[2] imgName:picArray[2] className:@"CommentViewController"];
  
  // 将创建的控制器对象添加到数组中管理
 //_className = [NSArray arrayWithObjects:newsColumn, picColumn, commentColumn, nil];
  _classNameArray = @[newsColumn, picColumn, commentColumn];
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return _classNameArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  return 40;
}

#pragma mark - UITableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *cellIde = @"cellIde";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIde];
  
  if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIde];
  }
  
  if (indexPath.row < _classNameArray.count) {
    Column *column = [_classNameArray objectAtIndexedSubscript:indexPath.row];
    cell.textLabel.text = column.columnName;
    cell.imageView.image = [UIImage imageNamed:column.columnImgName];
  }
  return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  // 取消每一行的高亮状态
  [tableView deselectRowAtIndexPath:indexPath animated:YES];
  
  Column *column = nil;
  if (indexPath.row < _classNameArray.count) {
    column = [_classNameArray objectAtIndexedSubscript:indexPath.row];
  }
  if ([self.delegate respondsToSelector:@selector(leftTableViewRowClicked:)]) {
    [self.delegate leftTableViewRowClicked:column];
  }
}
@end

抽屉效果重写

标签:抽屉效果重写

原文地址:http://blog.csdn.net/u010606986/article/details/43705055

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!