实现原理
addChildViewController
从使用开始
下载源码下来之后,运行demo
发现Appdelegate的代码如下:
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:[[DEMOFirstViewController alloc] init]];
DEMOLeftMenuViewController *leftMenuViewController = [[DEMOLeftMenuViewController alloc] init];
DEMORightMenuViewController *rightMenuViewController = [[DEMORightMenuViewController alloc] init];
RESideMenu *sideMenuViewController = [[RESideMenu alloc] initWithContentViewController:navigationController
leftMenuViewController:leftMenuViewController
rightMenuViewController:rightMenuViewController];
sideMenuViewController.backgroundImage = [UIImage imageNamed:@"Stars"];
sideMenuViewController.menuPreferredStatusBarStyle = 1; // UIStatusBarStyleLightContent
sideMenuViewController.delegate = self;
sideMenuViewController.contentViewShadowColor = [UIColor blackColor];
sideMenuViewController.contentViewShadowOffset = CGSizeMake(0, 0);
sideMenuViewController.contentViewShadowOpacity = 0.6;
sideMenuViewController.contentViewShadowRadius = 12;
sideMenuViewController.contentViewShadowEnabled = YES;
self.window.rootViewController = sideMenuViewController;
包含一个内容控制器navigationController及左视图控制器leftMenuViewController右视图控制器rightMenuViewController
走进初始化方法
- (id)initWithContentViewController:(UIViewController *)contentViewController leftMenuViewController:(UIViewController *)leftMenuViewController rightMenuViewController:(UIViewController *)rightMenuViewController
实现如下:
self = [self init];
if (self) {
_contentViewController = contentViewController;
_leftMenuViewController = leftMenuViewController;
_rightMenuViewController = rightMenuViewController;
}
return self;
在init中调用commonInit初始化了一堆默认属性。
重写了
@property (strong, readwrite, nonatomic) UIViewController *contentViewController;
@property (strong, readwrite, nonatomic) UIViewController *leftMenuViewController;
@property (strong, readwrite, nonatomic) UIViewController *rightMenuViewController;
这3个属性的set方法。
拿setContentController举例
- (void)setContentViewController:(UIViewController *)contentViewController
{
//判断如果没有就赋值
if (!_contentViewController) {
_contentViewController = contentViewController;
return;
}
//赋值之后将页面的所有值清空
[self hideViewController:_contentViewController];
_contentViewController = contentViewController;
//核心代码
//调用addChildViewController将contentView添加为ResideMenu的子视图
[self addChildViewController:self.contentViewController];
self.contentViewController.view.frame = self.view.bounds;
[self.contentViewContainer addSubview:self.contentViewController.view];
//添加子视图之后调用,证明已经添加完成
[self.contentViewController didMoveToParentViewController:self];
[self updateContentViewShadow];
if (self.visible) {
[self addContentViewControllerMotionEffects];
}
}
手势添加
if (self.panGestureEnabled) {
self.view.multipleTouchEnabled = NO;
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognized:)];
panGestureRecognizer.delegate = self;
[self.view addGestureRecognizer:panGestureRecognizer];
}
监控每一次手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
手势滑动触发的回调
//各种操作。。。
- (void)panGestureRecognized:(UIPanGestureRecognizer *)recognizer
//滑动或点击到左右视图后 content的区域为一个button,使用这个button来回去
self.contentButton = ({
UIButton *button = [[UIButton alloc] initWithFrame:CGRectNull];
[button addTarget:self action:@selector(hideMenuViewController) forControlEvents:UIControlEventTouchUpInside];
button;
});
实现效果和相关的逻辑判断等有时间再慢慢看。