当前位置: 首页 > 工具软件 > SSASideMenu > 使用案例 >

SSASideMenu解析

裴焱
2023-12-01

SSASideMenu 是知名侧滑第三方。


定义三个属性,左菜单,内容,右菜单

@property (strong, readwrite, nonatomic) UIViewController *contentViewController;
@property (strong, readwrite, nonatomic) UIViewController *leftMenuViewController;
@property (strong, readwrite, nonatomic) UIViewController *rightMenuViewController;

初始化赋值三个viewcontroller属性

- (id)initWithContentViewController:(UIViewController *)contentViewController leftMenuViewController:(UIViewController *)leftMenuViewController rightMenuViewController:(UIViewController *)rightMenuViewController
{
    self = [self init];
    if (self) {
//        三个ViewController分别是 内容,左菜单,右菜单
        _contentViewController = contentViewController;
        _leftMenuViewController = leftMenuViewController;
        _rightMenuViewController = rightMenuViewController;
    }
    return self;
}

viewDidLoad 添加子视图控制器,当然前提是判断左右菜单是否存在

设置属性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];

        );

    }

}



给self.view添加拖拽手势

   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];
}





 类似资料: