RESIDEMENU实现原理

  在写一个项目用RESIDEMENU,这是写得很好的一个开源组件,只需要几行代码就可以使用。所以现在抽空看看代码,看看作者是怎么实现的。

- (void)awakeFromNib {

if (self.contentViewStoryboardID) {

self.contentViewController = [self.storyboard instantiateViewControllerWithIdentifier:self.contentViewStoryboardID];

}

if (self.leftMenuViewStoryboardID) {

self.leftMenuViewController = [self.storyboard instantiateViewControllerWithIdentifier:self.leftMenuViewStoryboardID];

}

if (self.rightMenuViewStoryboardID) {

self.rightMenuViewController = [self.storyboard instantiateViewControllerWithIdentifier:self.rightMenuViewStoryboardID];

}

}

  这是.m文件里的第一个方法,很好理解,这是通过storyboard设好ID,然后通过ID初始化三个vc,分别作为内容试图、左视图、右视图。

- (void)viewDidLoad {

[super viewDidLoad];

self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

self.backgroundImageView = ({

UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];

imageView.image = self.backgroundImage;

imageView.contentMode = UIViewContentModeScaleAspectFill;

imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

imageView;

});

self.contentButton = ({

UIButton *button = [[UIButton alloc] initWithFrame:CGRectNull];

[button addTarget:self action:@selector(hideMenuViewController) forControlEvents:UIControlEventTouchUpInside];

button;

});

[self.view addSubview:self.backgroundImageView];

[self.view addSubview:self.menuViewContainer];

[self.view addSubview:self.contentViewContainer];

//重点

self.menuViewContainer.frame = self.view.bounds;

self.menuViewContainer.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

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

}

if (self.rightMenuViewController) {

[self addChildViewController:self.rightMenuViewController];

self.rightMenuViewController.view.frame = self.view.bounds;

self.rightMenuViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

[self.menuViewContainer addSubview:self.rightMenuViewController.view];

[self.rightMenuViewController didMoveToParentViewController:self];

}

self.contentViewContainer.frame = self.view.bounds;

self.contentViewContainer.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

[self addChildViewController:self.contentViewController];

self.contentViewController.view.frame = self.view.bounds;

[self.contentViewContainer addSubview:self.contentViewController.view];

[self.contentViewController didMoveToParentViewController:self];

self.menuViewContainer.alpha = !self.fadeMenuView ?: 0;

if (self.scaleBackgroundImageView)

self.backgroundImageView.transform = CGAffineTransformMakeScale(1.7f, 1.7f);

[self addMenuViewControllerMotionEffects];

if (self.panGestureEnabled) {

self.view.multipleTouchEnabled = NO;

UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognized:)];

panGestureRecognizer.delegate = self;

[self.view addGestureRecognizer:panGestureRecognizer];

}

[self updateContentViewShadow];

}

  这是viewdidload里面的代码,可以看看“重点”开始的代码,如果左视图和右视图存在就添加到当前视图。内容视图是一定要有的。其实在看代码前,没想过vc添加vc的,之前都是用view添加view,用得多了,所以思维就有点固化了。RESIDEMENU原理其实很简单,但是如果要做到作者的水平,必须对vc间的关系、动画、手势有相当的积累才可以

你可能感兴趣的:(RESIDEMENU实现原理)