#import "MJViewController.h"
#import "MJMenuViewController.h"
#import "MJCenterViewController.h"
#import "UIView+MJ.h"
#define MJMenuWidth 150
@interface MJViewController ()
@end
@implementation MJViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 1.初始化子控制器
// 1.1.左侧菜单
MJMenuViewController *menuVc = [[MJMenuViewController alloc] init];
menuVc.view.width = MJMenuWidth;
menuVc.view.y = 20;
[self.view addSubview:menuVc.view];
[self addChildViewController:menuVc];
// 1.2.中间内容
MJCenterViewController *centerVc = [[MJCenterViewController alloc] init];
centerVc.view.frame = self.view.bounds;
[self.view addSubview:centerVc.view];
[self addChildViewController:centerVc];
// 2.监听手势
[centerVc.view addGestureRecognizer:[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragCenterView:)]];
}
// transform == 150
// 1
// 用x来判断,用transform来控制
- (void)dragCenterView:(UIPanGestureRecognizer *)pan
{
CGPoint point = [pan translationInView:pan.view];
// 结束拖拽
if (pan.state == UIGestureRecognizerStateCancelled || pan.state == UIGestureRecognizerStateEnded) {
if (pan.view.x >= MJMenuWidth * 0.5) { // 往右边至少走动了75
[UIView animateWithDuration:0.5 animations:^{
pan.view.transform = CGAffineTransformMakeTranslation(MJMenuWidth, 0);
}];
} else { // 走动距离的没有达到75
[UIView animateWithDuration:0.5 animations:^{
pan.view.transform = CGAffineTransformIdentity;
}];
}
} else { // 正在拖拽中
pan.view.transform = CGAffineTransformTranslate(pan.view.transform, point.x, 0);
[pan setTranslation:CGPointZero inView:pan.view];
if (pan.view.x >= MJMenuWidth) {
pan.view.transform = CGAffineTransformMakeTranslation(MJMenuWidth, 0);
} else if (pan.view.x <= 0) {
pan.view.transform = CGAffineTransformIdentity;
}
}
}
@end