RESideMenu是一个抽屉效果的第三方,实现原理为RESideMenu作为一个视图控制器,依次添加子视图左侧菜单栏控制器和内容页控制,通过pan手势识别,来缩放内容控制页,从而来显示左侧菜单栏。
1.使用
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];
2.结构
1)调用方法
- (id)initWithContentViewController:(UIViewController *)contentViewController leftMenuViewController:(UIViewController *)leftMenuViewController rightMenuViewController:(UIViewController *)rightMenuViewController;
===>2)调用 init 进行初始化
=====>3) - (void)viewDidLoad;(核心代码)
//加背景图
//在vc中加入view menuViewContainer
//在vc中加入view contentViewContaine
//如果有lefMenuVC
//把leftMenuVC设置成本VC的子控制器
[self addChildViewController:self.leftMenuViewController];
//把lefmenuVC的view加入到self.menuViewContainer
[self.menuViewContainer addSubview:self.leftMenuViewController.view];
//再把contentViewController设置成本VC的子控制器
[self addChildViewController:self.contentViewController];
//把contentVC的view加入到contentviewContainer中
[self.contentViewContainer addSubview:self.contentViewController.view];
//设置menuviewContainer的透明度
self.menuViewContainer.alpha = !self.fadeMenuView ?: 0;
//设置动画效果
//设置手势 panGesture
[self updateContentViewShadow];
3.展示抽屉效果
使用CGAffineTransform来进行缩放
分别对backgroundImageView menuViewContainer contentViewContainer 进行缩放
self.contentViewContainer.transform = CGAffineTransformMakeScale(self.contentViewScaleValue, self.contentViewScaleValue);
另外: [self.leftMenuViewController beginAppearanceTransition:YES animated:YES];
Tells a child controller its appearance is about to change.If you are implementing a custom container controller, use this method to tell the child that its views are about to appear or disappear. Do not invoke viewWillAppear:, viewWillDisappear:, viewDidAppear:, or viewDidDisappear: directly.
beginAppearanceTransition :告诉这个子vc,它将要现实显示或者消失,将自动调用viewWillAppear,viewWillDisappear,viewDidAppear,viewDidDisappear
第二点:在contentViewContainer加入btn,为了点击contentViewContationer隐藏左侧菜单栏
4.panGesture手势
调用uigestureRecognizer的代理
1)
首先 根据- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;判断对这个手势该不该做进一步相应(左侧菜单栏是否隐藏&&pan手势&&point的x值在屏幕两侧20px以内)
2)Pan gesture recognizer
recognizer.state == UIGestureRecognizerStateBegan
此时内容页的frame没有改变,contentbutton添加到根视图的最上层
3)recognizer.state == UIGestureRecognizerStateChanged
根据手势的point来更改视图的大小
self.backgroundImageView.transform = CGAffineTransformMakeScale(backgroundViewScale, backgroundViewScale);
参考:http://www.cnblogs.com/ios-wmm/p/4276746.html
CoreGraphics框架中的CGAffineTransform类可用于设定UIView的transform属性,控制视图的缩放、旋转和平移操作:
另称放射变换矩阵,
总得来说,这个类中包含3张不同类型,分别使用如下3个方法创建数值;
1.CGAffineTransformMakeTranslation(CGFloattx,CGFloatty)(平移:设置平移量)
2.CGAffineTransformMakeScale(CGFloatsx,CGFloatsy)(缩放:设置缩放比例)仅通过设置缩放比例就可实现视图扑面而来和缩进频幕的效果。
3.CGAffineTransformMakeRotation(CGFloatangle)(旋转:设置旋转角度)
以上3个都是针对视图的原定最初位置的中心点为起始参照进行相应操作的,在操作结束之后可对设置量进行还原:
view.transform=CGAffineTransformIdentity;
另外还可以通过CGAffineTransformTranslate等方法对现有的transform进行进一步处理;
更多了解core animation =====》》》 https://github.com/yixiangboy/IOSAnimationDemo