- (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:)]];
}
// 用x来判断,用transform来控制
- (void)dragCenterView:(UIPanGestureRecognizer *)pan
{
//取得平移手势在view上移动的点
CGPoint point = [pan translationInView:pan.view];
// 1.结束拖拽
if (pan.state == UIGestureRecognizerStateCancelled || pan.state == UIGestureRecognizerStateEnded) {
//(1)结束拖拽时,如果手势视图的x大于等于菜单栏的一半,就把手势视图的x设置为菜单栏的宽度
if (pan.view.x >= MJMenuWidth / 2.0) { // 往右边至少走动了75
[UIView animateWithDuration:0.5 animations:^{
//设置手势视图的x值为菜单栏的宽
pan.view.transform = CGAffineTransformMakeTranslation(MJMenuWidth, 0);
}];
//(2)结束拖拽时,如果手势视图的x值没有大于菜单栏的一半,就还让手势视图回到原位
} else {
[UIView animateWithDuration:0.5 animations:^{
pan.view.transform = CGAffineTransformIdentity;
}];
}
// 2.正在拖拽中
} else {
//(1)手势视图的x值跟随跟随手势的x值改变,实现手势视图向右移动
pan.view.transform = CGAffineTransformTranslate(pan.view.transform, point.x, 0);
//(2)x值每次增加前都清除之前的值
[pan setTranslation:CGPointZero inView:pan.view];
//在拖拽时,手势视图的x如果大于等于菜单栏的宽度,将其设为菜单栏的宽度
if (pan.view.x >= MJMenuWidth) {
pan.view.transform = CGAffineTransformMakeTranslation(MJMenuWidth, 0);
}else if (pan.view.x <= 0) { //如果手势视图的x小于等于0了,让其回到原位
pan.view.transform = CGAffineTransformIdentity;
}
}
}
抽屉效果
最新推荐文章于 2024-07-28 23:50:12 发布