主页
包含文件
- MainViewController.h, MainViewController.m
实现功能
1、设置导航模式的核心页面为MainWindows
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
LeftMenuViewController *leftMenuVC = [[LeftMenuViewController alloc] initWithNibName:@"LeftMenuViewController" bundle:[NSBundle mainBundle]];
HomeViewController *homeVC = [[HomeViewController alloc] initWithViewModel:[HomeViewModel new]];
_mainVC = [[MainViewController alloc] initWithLeftMenuViewController:leftMenuVC andHomeViewController:homeVC];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:_mainVC]
self.window.rootViewController = navigationController;
...
}
2、将appdelegate里面创建的LeftMenuViewController对象和HomeViewController的对象传给MainViewController
@property(strong,nonatomic) HomeViewController *homeViewController;
@property(strong,nonatomic) LeftMenuViewController *leftMenuViewController;
@property(assign,nonatomic) CGFloat distance;
@property(strong,nonatomic) UIView *leftMenuView;
@property(strong,nonatomic) UIView *mainView;
- (instancetype)initWithLeftMenuViewController:(LeftMenuViewController *)menuVC andHomeViewController:(HomeViewController *)homeVC {
self = [super init];
if (self) {
self.homeViewController = homeVC;
self.leftMenuViewController = menuVC;
}
return self;
}
3、在MainViewController加载完毕以后,
- 记录当前的拖动量_distance
- 设置leftMenuViewController.view的frame为全屏,位置转化为不可见,添加到MainViewController的view中
- 设置_homeViewController.view为全屏,并添加到MainViewController的view中
- 为_mainView添加pan手势
- 初始化tap手势,为后面的程序使用
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_distance = MainViewOriginXFromValue;
_leftMenuView = _leftMenuViewController.view;
_leftMenuView.frame = kScreenBounds;
=CGAffineTransformConcat(CGAffineTransformScale(CGAffineTransformIdentity, LeftViewScaleFromValue, LeftViewScaleFromValue), CGAffineTransformTranslate(CGAffineTransformIdentity, LeftViewOriginXFromValue, 0));
[self.view addSubview:_leftMenuView];
_mainView = [[UIView alloc] initWithFrame:kScreenBounds];
[self.view addSubview:_mainView];
[_mainView addSubview:_homeViewController.view];
//上面代码创建一个UIView出来好像没有用,直接下面的代码也行:
//_mainView = _homeViewController.view
//[self.view addSubview:_mainView];
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
[_mainView addGestureRecognizer:panGesture];
_tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
}
4、手势事件
- 点击手势
- 显示MainView。
- (void)tap:(UITapGestureRecognizer *)recongizer {
[self showMainView];
}
- 拖动手势
- 计算拖动量
- 拖动过程中,根据拖动量移动_leftMenuView和_mainView
- 拖动完成时,根据最终偏移量,显示_mainView或者_leftMenuView
- (void)pan:(UIPanGestureRecognizer *)recongnizer {
CGFloat moveX = [recongnizer translationInView:self.view].x;
CGFloat truedistance = _distance + moveX;
CGFloat percent = truedistance/MainViewMoveXMaxValue;
if (truedistance >= 0 && truedistance <= MainViewMoveXMaxValue) {
_mainView.transform = CGAffineTransformConcat(CGAffineTransformScale(CGAffineTransformIdentity, 1, MainViewScaleYFromValue-MainViewScaleMaxValue*percent), CGAffineTransformTranslate(CGAffineTransformIdentity, MainViewOriginXFromValue+truedistance, 0));
_leftMenuView.transform = CGAffineTransformConcat(CGAffineTransformScale(CGAffineTransformIdentity, LeftViewScaleFromValue+LeftViewScaleMaxValue*percent, LeftViewScaleFromValue+LeftViewScaleMaxValue*percent), CGAffineTransformTranslate(CGAffineTransformIdentity, LeftViewOriginXFromValue+LeftViewMoveXMaxValue*percent, 0));
}
if (recongnizer.state == UIGestureRecognizerStateEnded) {
if (truedistance <= MainViewMoveXMaxValue/2) {
[self showMainView];
}else{
[self showLeftMenuView];
}
}
}
5、显示页面函数如下
- 动画显示移动过程(_mainView, _leftMenuView)
- 动画完成后,记录偏移量,并添加(移除)偏移量
- (void)showMainView {
[UIView animateWithDuration:AnimateDuration delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
_mainView.transform = CGAffineTransformIdentity;
_leftMenuView.transform = CGAffineTransformConcat(CGAffineTransformScale(CGAffineTransformIdentity, LeftViewScaleFromValue, LeftViewScaleFromValue), CGAffineTransformTranslate(CGAffineTransformIdentity, LeftViewOriginXFromValue, 0));
} completion:^(BOOL finished) {
_distance = MainViewOriginXFromValue;
[_mainView removeGestureRecognizer:_tap];
}];
}
- (void)showLeftMenuView {
[UIView animateWithDuration:AnimateDuration delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
_mainView.transform = CGAffineTransformConcat(CGAffineTransformScale(CGAffineTransformIdentity, 1, MainViewScaleYEndValue), CGAffineTransformTranslate(CGAffineTransformIdentity, MainViewOriginXEndValue, 0));
_leftMenuView.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
_distance = MainViewOriginXEndValue;
[_mainView addGestureRecognizer:_tap];
}];
}