SSASideMenu 是知名侧滑第三方。
定义三个属性,左菜单,内容,右菜单
@property (strong, readwrite, nonatomic) UIViewController *contentViewController;
@property (strong, readwrite, nonatomic) UIViewController *leftMenuViewController;
@property (strong, readwrite, nonatomic) UIViewController *rightMenuViewController;
- (id)initWithContentViewController:(UIViewController *)contentViewController leftMenuViewController:(UIViewController *)leftMenuViewController rightMenuViewController:(UIViewController *)rightMenuViewController
{
self = [self init];
if (self) {
// 三个ViewController分别是 内容,左菜单,右菜单
_contentViewController = contentViewController;
_leftMenuViewController = leftMenuViewController;
_rightMenuViewController = rightMenuViewController;
}
return self;
}
设置属性controller的view 的autoresizingMask和frame;
menuViewContainer 是当前视图控制器的属性,用来添加 菜单view controller 的view
对应的也有一个 contentViewContainer 属性来做 self.contentViewController.view 的父视图
至于 didMoveToParentViewController 方法 是将子VC从父VC 移除,移除后会调用
if (self.leftMenuViewController) {
[self addChildViewController:self.leftMenuViewController];
self.leftMenuViewController.view.frame = self.view.bounds;
self.leftMenuViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.menuViewContainer addSubview:self.leftMenuViewController.view];
[self.leftMenuViewController didMoveToParentViewController:self];
}
[self addMenuViewControllerMotionEffects];
- (void)addMenuViewControllerMotionEffects
{
if (self.parallaxEnabled) {
IF_IOS7_OR_GREATER(
for (UIMotionEffect *effect in self.menuViewContainer.motionEffects) {
[self.menuViewContainer removeMotionEffect:effect];
}
UIInterpolatingMotionEffect *interpolationHorizontal = [[UIInterpolatingMotionEffect alloc]initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
interpolationHorizontal.minimumRelativeValue = @(self.parallaxMenuMinimumRelativeValue);
interpolationHorizontal.maximumRelativeValue = @(self.parallaxMenuMaximumRelativeValue);
UIInterpolatingMotionEffect *interpolationVertical = [[UIInterpolatingMotionEffect alloc]initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
interpolationVertical.minimumRelativeValue = @(self.parallaxMenuMinimumRelativeValue);
interpolationVertical.maximumRelativeValue = @(self.parallaxMenuMaximumRelativeValue);
[self.menuViewContainer addMotionEffect:interpolationHorizontal];
[self.menuViewContainer addMotionEffect:interpolationVertical];
);
}
}
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognized:)];
panGestureRecognizer.delegate = self;
[self.view addGestureRecognizer:panGestureRecognizer];
接下来是点击外部按钮,响应公开方法 :presentLeftMenuViewController
第一步是内容VC缩放,第二是center 改变,这样就实现抽屉效果了。
- (void)presentLeftMenuViewController
{
[self presentMenuViewContainerWithMenuViewController:self.leftMenuViewController];
[self showLeftMenuViewController];
}