第三方庫
RESideMenu
自定義側滑半屏顯示
-(id)initWithLeftVC:(UIViewController )leftVC mainVC:(UIViewController )mainVC rightVC:(UIViewController *)rightVC
{
self = [super init];
if(self)
{
[self.view addSubview:leftVC.view];
[self.view addSubview:rightVC.view];
[self.view addSubview:mainVC.view];
//viewController 想執行vc生命週期方法 必須把當前的VC加入到管理VC裏
//事件響應鏈
[self addChildViewController:leftVC];
[self addChildViewController:mainVC];
[self addChildViewController:rightVC];
self.leftVC = leftVC;
self.rightVC = rightVC;
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panHandle:)];
[mainVC.view addGestureRecognizer:pan];
}
return self;
}
-(void)panHandle:(UIPanGestureRecognizer *)p
{
//[p locationInView:self.view];
CGPoint point = [p translationInView:self.view];
p.view.center = CGPointMake(p.view.center.x + point.x, p.view.center.y);
[p setTranslation:CGPointZero inView:self.view];
if(p.view.frame.origin.x > 0)
{
self.leftVC.view.hidden = NO;
self.rightVC.view.hidden = YES;
}
else
{
self.leftVC.view.hidden = YES;
self.rightVC.view.hidden = NO;
}
//120 ---> 240
if(p.state == UIGestureRecognizerStateEnded)
{
NSInteger x_offset = 0;
if(p.view.frame.origin.x >= 150)
{
x_offset = [UIScreen mainScreen].bounds.size.width / 2.0f + 240;
}
else if(p.view.frame.origin.x <= -150)
{
x_offset = [UIScreen mainScreen].bounds.size.width / 2.0f - 240;
}
else
{
x_offset = [UIScreen mainScreen].bounds.size.width / 2.0f;
}
[UIView animateWithDuration:0.25 animations:^{
p.view.center = CGPointMake(x_offset, p.view.center.y);
}];
}
}