react-native-navigation的迁移库

MMDrawerController.m 64KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555
  1. // Copyright (c) 2013 Mutual Mobile (http://mutualmobile.com/)
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. #import "MMDrawerController.h"
  21. #import "UIViewController+MMDrawerController.h"
  22. #import "RNNNavigationOptions.h"
  23. #import <QuartzCore/QuartzCore.h>
  24. CGFloat const MMDrawerDefaultWidth = 280.0f;
  25. CGFloat const MMDrawerDefaultAnimationVelocity = 840.0f;
  26. NSTimeInterval const MMDrawerDefaultFullAnimationDelay = 0.10f;
  27. CGFloat const MMDrawerDefaultBounceDistance = 50.0f;
  28. NSTimeInterval const MMDrawerDefaultBounceAnimationDuration = 0.2f;
  29. CGFloat const MMDrawerDefaultSecondBounceDistancePercentage = .25f;
  30. CGFloat const MMDrawerDefaultShadowRadius = 10.0f;
  31. CGFloat const MMDrawerDefaultShadowOpacity = 0.8;
  32. NSTimeInterval const MMDrawerMinimumAnimationDuration = 0.15f;
  33. CGFloat const MMDrawerBezelRange = 20.0f;
  34. CGFloat const MMDrawerPanVelocityXAnimationThreshold = 200.0f;
  35. /** The amount of overshoot that is panned linearly. The remaining percentage nonlinearly asymptotes to the max percentage. */
  36. CGFloat const MMDrawerOvershootLinearRangePercentage = 0.75f;
  37. /** The percent of the possible overshoot width to use as the actual overshoot percentage. */
  38. CGFloat const MMDrawerOvershootPercentage = 0.1f;
  39. typedef void (^MMDrawerGestureStartedBlock)(MMDrawerController * drawerController, UIGestureRecognizer * gesture);
  40. typedef BOOL (^MMDrawerGestureShouldRecognizeTouchBlock)(MMDrawerController * drawerController, UIGestureRecognizer * gesture, UITouch * touch);
  41. typedef void (^MMDrawerGestureCompletionBlock)(MMDrawerController * drawerController, UIGestureRecognizer * gesture);
  42. static CAKeyframeAnimation * bounceKeyFrameAnimationForDistanceOnView(CGFloat distance, UIView * view) {
  43. CGFloat factors[32] = {0, 32, 60, 83, 100, 114, 124, 128, 128, 124, 114, 100, 83, 60, 32,
  44. 0, 24, 42, 54, 62, 64, 62, 54, 42, 24, 0, 18, 28, 32, 28, 18, 0};
  45. NSMutableArray *values = [NSMutableArray array];
  46. for (int i=0; i<32; i++)
  47. {
  48. CGFloat positionOffset = factors[i]/128.0f * distance + CGRectGetMidX(view.bounds);
  49. [values addObject:@(positionOffset)];
  50. }
  51. CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position.x"];
  52. animation.repeatCount = 1;
  53. animation.duration = .8;
  54. animation.fillMode = kCAFillModeForwards;
  55. animation.values = values;
  56. animation.removedOnCompletion = YES;
  57. animation.autoreverses = NO;
  58. return animation;
  59. }
  60. static NSString *MMDrawerLeftDrawerKey = @"MMDrawerLeftDrawer";
  61. static NSString *MMDrawerRightDrawerKey = @"MMDrawerRightDrawer";
  62. static NSString *MMDrawerCenterKey = @"MMDrawerCenter";
  63. static NSString *MMDrawerOpenSideKey = @"MMDrawerOpenSide";
  64. @interface MMDrawerCenterContainerView : UIView
  65. @property (nonatomic,assign) MMDrawerOpenCenterInteractionMode centerInteractionMode;
  66. @property (nonatomic,assign) MMDrawerSide openSide;
  67. @property (nonatomic, strong) UIView *overlayView;
  68. @end
  69. @implementation MMDrawerCenterContainerView
  70. -(UIView *)overlayView {
  71. if (!_overlayView) {
  72. _overlayView = [[UIView alloc] initWithFrame:self.bounds];
  73. _overlayView.userInteractionEnabled = NO;
  74. _overlayView.alpha = 0.0;
  75. }
  76. return _overlayView;
  77. }
  78. -(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
  79. UIView *hitView = [super hitTest:point withEvent:event];
  80. if(hitView &&
  81. self.openSide != MMDrawerSideNone){
  82. UINavigationBar * navBar = [self navigationBarContainedWithinSubviewsOfView:self];
  83. CGRect navBarFrame = [navBar convertRect:navBar.bounds toView:self];
  84. if((self.centerInteractionMode == MMDrawerOpenCenterInteractionModeNavigationBarOnly &&
  85. CGRectContainsPoint(navBarFrame, point) == NO) ||
  86. self.centerInteractionMode == MMDrawerOpenCenterInteractionModeNone){
  87. hitView = nil;
  88. }
  89. }
  90. return hitView;
  91. }
  92. -(UINavigationBar*)navigationBarContainedWithinSubviewsOfView:(UIView*)view{
  93. UINavigationBar * navBar = nil;
  94. for(UIView * subview in [view subviews]){
  95. if([view isKindOfClass:[UINavigationBar class]]){
  96. navBar = (UINavigationBar*)view;
  97. break;
  98. }
  99. else {
  100. navBar = [self navigationBarContainedWithinSubviewsOfView:subview];
  101. if (navBar != nil) {
  102. break;
  103. }
  104. }
  105. }
  106. return navBar;
  107. }
  108. -(void)setFrame:(CGRect)frame withLayoutAlpha:(CGFloat)layoutAlpha {
  109. [super setFrame:frame];
  110. self.overlayView.alpha = layoutAlpha;
  111. if (![self.overlayView isDescendantOfView:self]) {
  112. [self addSubview:self.overlayView];
  113. }
  114. else {
  115. [self bringSubviewToFront:self.overlayView];
  116. }
  117. }
  118. @end
  119. @interface MMDrawerController () <UIGestureRecognizerDelegate>{
  120. CGFloat _maximumRightDrawerWidth;
  121. CGFloat _maximumLeftDrawerWidth;
  122. UIColor * _statusBarViewBackgroundColor;
  123. }
  124. @property (nonatomic, assign, readwrite) MMDrawerSide openSide;
  125. @property (nonatomic, strong) UIView * childControllerContainerView;
  126. @property (nonatomic, strong) MMDrawerCenterContainerView * centerContainerView;
  127. @property (nonatomic, strong) UIView * dummyStatusBarView;
  128. @property (nonatomic, assign) CGRect startingPanRect;
  129. @property (nonatomic, copy) MMDrawerControllerDrawerVisualStateBlock drawerVisualState;
  130. @property (nonatomic, copy) MMDrawerGestureShouldRecognizeTouchBlock gestureShouldRecognizeTouch;
  131. @property (nonatomic, copy) MMDrawerGestureCompletionBlock gestureStart;
  132. @property (nonatomic, copy) MMDrawerGestureCompletionBlock gestureCompletion;
  133. @property (nonatomic, assign, getter = isAnimatingDrawer) BOOL animatingDrawer;
  134. @end
  135. @implementation MMDrawerController
  136. #pragma mark - Init
  137. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
  138. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  139. if (self) {
  140. [self commonSetup];
  141. }
  142. return self;
  143. }
  144. - (id)initWithCoder:(NSCoder *)aDecoder{
  145. self = [super initWithCoder:aDecoder];
  146. if (self) {
  147. [self commonSetup];
  148. }
  149. return self;
  150. }
  151. -(instancetype)initWithCenterViewController:(UIViewController *)centerViewController leftDrawerViewController:(UIViewController *)leftDrawerViewController rightDrawerViewController:(UIViewController *)rightDrawerViewController{
  152. NSParameterAssert(centerViewController);
  153. self = [super init];
  154. if(self){
  155. [self setCenterViewController:centerViewController];
  156. [self setLeftDrawerViewController:leftDrawerViewController];
  157. [self setRightDrawerViewController:rightDrawerViewController];
  158. }
  159. return self;
  160. }
  161. -(instancetype)initWithCenterViewController:(UIViewController *)centerViewController leftDrawerViewController:(UIViewController *)leftDrawerViewController{
  162. return [self initWithCenterViewController:centerViewController leftDrawerViewController:leftDrawerViewController rightDrawerViewController:nil];
  163. }
  164. -(instancetype)initWithCenterViewController:(UIViewController *)centerViewController rightDrawerViewController:(UIViewController *)rightDrawerViewController{
  165. return [self initWithCenterViewController:centerViewController leftDrawerViewController:nil rightDrawerViewController:rightDrawerViewController];
  166. }
  167. -(void)commonSetup{
  168. [self setMaximumLeftDrawerWidth:MMDrawerDefaultWidth];
  169. [self setMaximumRightDrawerWidth:MMDrawerDefaultWidth];
  170. [self setAnimationVelocity:MMDrawerDefaultAnimationVelocity];
  171. [self setShowsShadow:YES];
  172. [self setShouldStretchDrawer:YES];
  173. [self setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeNone];
  174. [self setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeNone];
  175. [self setCenterHiddenInteractionMode:MMDrawerOpenCenterInteractionModeNavigationBarOnly];
  176. // set shadow related default values
  177. [self setShadowOpacity:MMDrawerDefaultShadowOpacity];
  178. [self setShadowRadius:MMDrawerDefaultShadowRadius];
  179. [self setShadowOffset:CGSizeMake(0, -3)];
  180. [self setShadowColor:[UIColor blackColor]];
  181. // set default bezel range for panGestureReconizer
  182. [self setBezelPanningCenterViewRange:MMDrawerBezelRange];
  183. // set defualt panVelocityXAnimationThreshold
  184. [self setPanVelocityXAnimationThreshold:MMDrawerPanVelocityXAnimationThreshold];
  185. _rightSideEnabled = _leftSideEnabled = YES;
  186. }
  187. #pragma mark - State Restoration
  188. - (void)encodeRestorableStateWithCoder:(NSCoder *)coder{
  189. [super encodeRestorableStateWithCoder:coder];
  190. if (self.leftDrawerViewController){
  191. [coder encodeObject:self.leftDrawerViewController forKey:MMDrawerLeftDrawerKey];
  192. }
  193. if (self.rightDrawerViewController){
  194. [coder encodeObject:self.rightDrawerViewController forKey:MMDrawerRightDrawerKey];
  195. }
  196. if (self.centerViewController){
  197. [coder encodeObject:self.centerViewController forKey:MMDrawerCenterKey];
  198. }
  199. [coder encodeInteger:self.openSide forKey:MMDrawerOpenSideKey];
  200. }
  201. - (void)decodeRestorableStateWithCoder:(NSCoder *)coder{
  202. UIViewController *controller;
  203. MMDrawerSide openside;
  204. [super decodeRestorableStateWithCoder:coder];
  205. if ((controller = [coder decodeObjectForKey:MMDrawerLeftDrawerKey])){
  206. self.leftDrawerViewController = controller;
  207. }
  208. if ((controller = [coder decodeObjectForKey:MMDrawerRightDrawerKey])){
  209. self.rightDrawerViewController = controller;
  210. }
  211. if ((controller = [coder decodeObjectForKey:MMDrawerCenterKey])){
  212. self.centerViewController = controller;
  213. }
  214. if ((openside = [coder decodeIntegerForKey:MMDrawerOpenSideKey])){
  215. [self openDrawerSide:openside animated:false completion:nil];
  216. }
  217. }
  218. #pragma mark - Open/Close methods
  219. -(void)toggleDrawerSide:(MMDrawerSide)drawerSide animated:(BOOL)animated completion:(void (^)(BOOL finished))completion{
  220. NSParameterAssert(drawerSide!=MMDrawerSideNone);
  221. if(self.openSide == MMDrawerSideNone){
  222. [self openDrawerSide:drawerSide animated:animated completion:completion];
  223. }
  224. else {
  225. if((drawerSide == MMDrawerSideLeft &&
  226. self.openSide == MMDrawerSideLeft) ||
  227. (drawerSide == MMDrawerSideRight &&
  228. self.openSide == MMDrawerSideRight)){
  229. [self closeDrawerAnimated:animated completion:completion];
  230. }
  231. else if(completion){
  232. completion(NO);
  233. }
  234. }
  235. }
  236. -(void)closeDrawerAnimated:(BOOL)animated completion:(void (^)(BOOL finished))completion{
  237. [self closeDrawerAnimated:animated velocity:self.animationVelocity animationOptions:UIViewAnimationOptionCurveEaseInOut completion:completion];
  238. }
  239. -(void)closeDrawerAnimated:(BOOL)animated velocity:(CGFloat)velocity animationOptions:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion{
  240. if(self.isAnimatingDrawer){
  241. if(completion){
  242. completion(NO);
  243. }
  244. }
  245. else {
  246. [self setAnimatingDrawer:animated];
  247. CGRect newFrame = self.childControllerContainerView.bounds;
  248. CGFloat distance = ABS(CGRectGetMinX(self.centerContainerView.frame));
  249. NSTimeInterval duration = MAX(distance/ABS(velocity),MMDrawerMinimumAnimationDuration);
  250. BOOL leftDrawerVisible = CGRectGetMinX(self.centerContainerView.frame) > 0;
  251. BOOL rightDrawerVisible = CGRectGetMinX(self.centerContainerView.frame) < 0;
  252. MMDrawerSide visibleSide = MMDrawerSideNone;
  253. CGFloat percentVisble = 0.0;
  254. if(leftDrawerVisible){
  255. CGFloat visibleDrawerPoints = CGRectGetMinX(self.centerContainerView.frame);
  256. percentVisble = MAX(0.0,visibleDrawerPoints/self.maximumLeftDrawerWidth);
  257. visibleSide = MMDrawerSideLeft;
  258. }
  259. else if(rightDrawerVisible){
  260. CGFloat visibleDrawerPoints = CGRectGetWidth(self.centerContainerView.frame)-CGRectGetMaxX(self.centerContainerView.frame);
  261. percentVisble = MAX(0.0,visibleDrawerPoints/self.maximumRightDrawerWidth);
  262. visibleSide = MMDrawerSideRight;
  263. }
  264. UIViewController * sideDrawerViewController = [self sideDrawerViewControllerForSide:visibleSide];
  265. [self updateDrawerVisualStateForDrawerSide:visibleSide percentVisible:percentVisble];
  266. [sideDrawerViewController beginAppearanceTransition:NO animated:animated];
  267. [UIView
  268. animateWithDuration:(animated?duration:0.0)
  269. delay:0.0
  270. options:options
  271. animations:^{
  272. [self setNeedsStatusBarAppearanceUpdateIfSupported];
  273. [self.centerContainerView setFrame:newFrame withLayoutAlpha:0.0];
  274. [self updateDrawerVisualStateForDrawerSide:visibleSide percentVisible:0.0];
  275. }
  276. completion:^(BOOL finished) {
  277. [sideDrawerViewController endAppearanceTransition];
  278. [self setOpenSide:MMDrawerSideNone];
  279. [self resetDrawerVisualStateForDrawerSide:visibleSide];
  280. [self setAnimatingDrawer:NO];
  281. if(completion){
  282. completion(finished);
  283. }
  284. }];
  285. }
  286. }
  287. -(void)openDrawerSide:(MMDrawerSide)drawerSide animated:(BOOL)animated completion:(void (^)(BOOL finished))completion{
  288. NSParameterAssert(drawerSide != MMDrawerSideNone);
  289. [self openDrawerSide:drawerSide animated:animated velocity:self.animationVelocity animationOptions:UIViewAnimationOptionCurveEaseInOut completion:completion];
  290. }
  291. -(void)openDrawerSide:(MMDrawerSide)drawerSide animated:(BOOL)animated velocity:(CGFloat)velocity animationOptions:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion{
  292. NSParameterAssert(drawerSide != MMDrawerSideNone);
  293. if (self.isAnimatingDrawer) {
  294. if(completion){
  295. completion(NO);
  296. }
  297. }
  298. else {
  299. [self setAnimatingDrawer:animated];
  300. UIViewController * sideDrawerViewController = [self sideDrawerViewControllerForSide:drawerSide];
  301. if (self.openSide != drawerSide) {
  302. [self prepareToPresentDrawer:drawerSide animated:animated];
  303. }
  304. if(sideDrawerViewController){
  305. CGRect newFrame;
  306. CGRect oldFrame = self.centerContainerView.frame;
  307. if(drawerSide == MMDrawerSideLeft){
  308. newFrame = self.centerContainerView.frame;
  309. newFrame.origin.x = self.maximumLeftDrawerWidth;
  310. }
  311. else {
  312. newFrame = self.centerContainerView.frame;
  313. newFrame.origin.x = 0-self.maximumRightDrawerWidth;
  314. }
  315. CGFloat distance = ABS(CGRectGetMinX(oldFrame)-newFrame.origin.x);
  316. NSTimeInterval duration = MAX(distance/ABS(velocity),MMDrawerMinimumAnimationDuration);
  317. [UIView
  318. animateWithDuration:(animated?duration:0.0)
  319. delay:0.0
  320. options:options
  321. animations:^{
  322. [self setNeedsStatusBarAppearanceUpdateIfSupported];
  323. [self.centerContainerView setFrame:newFrame withLayoutAlpha:1.0];
  324. [self updateDrawerVisualStateForDrawerSide:drawerSide percentVisible:1.0];
  325. }
  326. completion:^(BOOL finished) {
  327. //End the appearance transition if it already wasn't open.
  328. if(drawerSide != self.openSide){
  329. [sideDrawerViewController endAppearanceTransition];
  330. }
  331. [self setOpenSide:drawerSide];
  332. [self resetDrawerVisualStateForDrawerSide:drawerSide];
  333. [self setAnimatingDrawer:NO];
  334. if(completion){
  335. completion(finished);
  336. }
  337. }];
  338. }
  339. }
  340. }
  341. #pragma mark - Updating the Center View Controller
  342. //If animated is NO, then we need to handle all the appearance calls within this method. Otherwise,
  343. //let the method calling this one handle proper appearance methods since they will have more context
  344. -(void)setCenterViewController:(UIViewController *)centerViewController animated:(BOOL)animated{
  345. if ([self.centerViewController isEqual:centerViewController]) {
  346. return;
  347. }
  348. if (_centerContainerView == nil) {
  349. //This is related to Issue #152 (https://github.com/mutualmobile/MMDrawerController/issues/152)
  350. // also fixed below in the getter for `childControllerContainerView`. Turns out we have
  351. // two center container views getting added to the view during init,
  352. // because the first request self.centerContainerView.bounds was kicking off a
  353. // viewDidLoad, which caused us to be able to fall through this check twice.
  354. //
  355. //The fix is to grab the bounds, and then check again that the child container view has
  356. //not been created.
  357. CGRect centerFrame = self.childControllerContainerView.bounds;
  358. if(_centerContainerView == nil){
  359. _centerContainerView = [[MMDrawerCenterContainerView alloc] initWithFrame:centerFrame];
  360. [self.centerContainerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
  361. [self.centerContainerView setBackgroundColor:[UIColor clearColor]];
  362. [self.centerContainerView setOpenSide:self.openSide];
  363. [self.centerContainerView setCenterInteractionMode:self.centerHiddenInteractionMode];
  364. [self.childControllerContainerView addSubview:self.centerContainerView];
  365. }
  366. }
  367. UIViewController * oldCenterViewController = self.centerViewController;
  368. if(oldCenterViewController){
  369. [oldCenterViewController willMoveToParentViewController:nil];
  370. if(animated == NO){
  371. [oldCenterViewController beginAppearanceTransition:NO animated:NO];
  372. }
  373. [oldCenterViewController.view removeFromSuperview];
  374. if(animated == NO){
  375. [oldCenterViewController endAppearanceTransition];
  376. }
  377. [oldCenterViewController removeFromParentViewController];
  378. }
  379. _centerViewController = centerViewController;
  380. [self addChildViewController:self.centerViewController];
  381. [self.centerViewController.view setFrame:self.childControllerContainerView.bounds];
  382. [self.centerContainerView addSubview:self.centerViewController.view];
  383. [self.childControllerContainerView bringSubviewToFront:self.centerContainerView];
  384. [self.centerViewController.view setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
  385. [self updateShadowForCenterView];
  386. if(animated == NO){
  387. // If drawer is offscreen, then viewWillAppear: will take care of this
  388. if(self.view.window) {
  389. [self.centerViewController beginAppearanceTransition:YES animated:NO];
  390. [self.centerViewController endAppearanceTransition];
  391. }
  392. [self.centerViewController didMoveToParentViewController:self];
  393. }
  394. }
  395. - (void)performOnChildWillAppear:(RNNNavigationOptions *)childOptions {
  396. if ([self.parentViewController respondsToSelector:@selector(performOnChildWillAppear:)]) {
  397. [self.parentViewController performSelector:@selector(performOnChildWillAppear:) withObject:childOptions];
  398. }
  399. }
  400. - (void)performOnChildLoad:(RNNNavigationOptions *)childOptions {
  401. if ([self.parentViewController respondsToSelector:@selector(performOnChildLoad:)]) {
  402. [self.parentViewController performSelector:@selector(performOnChildLoad:) withObject:childOptions];
  403. }
  404. }
  405. - (void)willMoveToParentViewController:(UIViewController *)parent {
  406. if ([self.parentViewController respondsToSelector:@selector(performOnChildLoad:)]) {
  407. [self.parentViewController performSelector:@selector(performOnChildLoad:) withObject:nil];
  408. }
  409. }
  410. -(void)setCenterViewController:(UIViewController *)newCenterViewController withCloseAnimation:(BOOL)animated completion:(void(^)(BOOL finished))completion{
  411. if(self.openSide == MMDrawerSideNone){
  412. //If a side drawer isn't open, there is nothing to animate...
  413. animated = NO;
  414. }
  415. BOOL forwardAppearanceMethodsToCenterViewController = ([self.centerViewController isEqual:newCenterViewController] == NO);
  416. UIViewController * oldCenterViewController = self.centerViewController;
  417. // This is related to issue 363 (https://github.com/novkostya/MMDrawerController/pull/363)
  418. // This needs to be refactored so the appearance logic is easier
  419. // to follow across the multiple close/setter methods
  420. if (animated && forwardAppearanceMethodsToCenterViewController) {
  421. [oldCenterViewController beginAppearanceTransition:NO animated:NO];
  422. }
  423. [self setCenterViewController:newCenterViewController animated:animated];
  424. // Related to note above.
  425. if (animated && forwardAppearanceMethodsToCenterViewController) {
  426. [oldCenterViewController endAppearanceTransition];
  427. }
  428. if(animated){
  429. [self updateDrawerVisualStateForDrawerSide:self.openSide percentVisible:1.0];
  430. if (forwardAppearanceMethodsToCenterViewController) {
  431. [self.centerViewController beginAppearanceTransition:YES animated:animated];
  432. }
  433. [self
  434. closeDrawerAnimated:animated
  435. completion:^(BOOL finished) {
  436. if (forwardAppearanceMethodsToCenterViewController) {
  437. [self.centerViewController endAppearanceTransition];
  438. [self.centerViewController didMoveToParentViewController:self];
  439. }
  440. if(completion){
  441. completion(finished);
  442. }
  443. }];
  444. }
  445. else {
  446. if(completion) {
  447. completion(YES);
  448. }
  449. }
  450. }
  451. -(void)setCenterViewController:(UIViewController *)newCenterViewController withFullCloseAnimation:(BOOL)animated completion:(void(^)(BOOL finished))completion{
  452. if(self.openSide != MMDrawerSideNone &&
  453. animated){
  454. BOOL forwardAppearanceMethodsToCenterViewController = ([self.centerViewController isEqual:newCenterViewController] == NO);
  455. UIViewController * sideDrawerViewController = [self sideDrawerViewControllerForSide:self.openSide];
  456. CGFloat targetClosePoint = 0.0f;
  457. if(self.openSide == MMDrawerSideRight){
  458. targetClosePoint = -CGRectGetWidth(self.childControllerContainerView.bounds);
  459. }
  460. else if(self.openSide == MMDrawerSideLeft) {
  461. targetClosePoint = CGRectGetWidth(self.childControllerContainerView.bounds);
  462. }
  463. CGFloat distance = ABS(self.centerContainerView.frame.origin.x-targetClosePoint);
  464. NSTimeInterval firstDuration = [self animationDurationForAnimationDistance:distance];
  465. CGRect newCenterRect = self.centerContainerView.frame;
  466. [self setAnimatingDrawer:animated];
  467. UIViewController * oldCenterViewController = self.centerViewController;
  468. if(forwardAppearanceMethodsToCenterViewController ){
  469. [oldCenterViewController beginAppearanceTransition:NO animated:animated];
  470. }
  471. newCenterRect.origin.x = targetClosePoint;
  472. [UIView
  473. animateWithDuration:firstDuration
  474. delay:0.0
  475. options:UIViewAnimationOptionCurveEaseInOut
  476. animations:^{
  477. [self.centerContainerView setFrame:newCenterRect withLayoutAlpha:1.0];
  478. [sideDrawerViewController.view setFrame:self.childControllerContainerView.bounds];
  479. }
  480. completion:^(BOOL finished) {
  481. CGRect oldCenterRect = self.centerContainerView.frame;
  482. [self setCenterViewController:newCenterViewController animated:animated];
  483. [self.centerContainerView setFrame:oldCenterRect withLayoutAlpha:1.0];
  484. [self updateDrawerVisualStateForDrawerSide:self.openSide percentVisible:1.0];
  485. if(forwardAppearanceMethodsToCenterViewController) {
  486. [oldCenterViewController endAppearanceTransition];
  487. [self.centerViewController beginAppearanceTransition:YES animated:animated];
  488. }
  489. [sideDrawerViewController beginAppearanceTransition:NO animated:animated];
  490. [UIView
  491. animateWithDuration:[self animationDurationForAnimationDistance:CGRectGetWidth(self.childControllerContainerView.bounds)]
  492. delay:MMDrawerDefaultFullAnimationDelay
  493. options:UIViewAnimationOptionCurveEaseInOut
  494. animations:^{
  495. [self.centerContainerView setFrame:self.childControllerContainerView.bounds withLayoutAlpha:1.0];
  496. [self updateDrawerVisualStateForDrawerSide:self.openSide percentVisible:0.0];
  497. }
  498. completion:^(BOOL finished) {
  499. if (forwardAppearanceMethodsToCenterViewController) {
  500. [self.centerViewController endAppearanceTransition];
  501. [self.centerViewController didMoveToParentViewController:self];
  502. }
  503. [sideDrawerViewController endAppearanceTransition];
  504. [self resetDrawerVisualStateForDrawerSide:self.openSide];
  505. [sideDrawerViewController.view setFrame:sideDrawerViewController.mm_visibleDrawerFrame];
  506. [self setOpenSide:MMDrawerSideNone];
  507. [self setAnimatingDrawer:NO];
  508. if(completion){
  509. completion(finished);
  510. }
  511. }];
  512. }];
  513. }
  514. else {
  515. [self setCenterViewController:newCenterViewController animated:animated];
  516. if(self.openSide != MMDrawerSideNone){
  517. [self closeDrawerAnimated:animated completion:completion];
  518. }
  519. else if(completion){
  520. completion(YES);
  521. }
  522. }
  523. }
  524. -(void)setCenterOverlayColor:(UIColor*)color {
  525. if (color) {
  526. self.centerContainerView.overlayView.backgroundColor = color;
  527. }
  528. }
  529. #pragma mark - Size Methods
  530. -(void)setMaximumLeftDrawerWidth:(CGFloat)width animated:(BOOL)animated completion:(void(^)(BOOL finished))completion{
  531. [self setMaximumDrawerWidth:width forSide:MMDrawerSideLeft animated:animated completion:completion];
  532. }
  533. -(void)setMaximumRightDrawerWidth:(CGFloat)width animated:(BOOL)animated completion:(void(^)(BOOL finished))completion{
  534. [self setMaximumDrawerWidth:width forSide:MMDrawerSideRight animated:animated completion:completion];
  535. }
  536. - (void)setMaximumDrawerWidth:(CGFloat)width forSide:(MMDrawerSide)drawerSide animated:(BOOL)animated completion:(void(^)(BOOL finished))completion{
  537. NSParameterAssert(width > 0);
  538. NSParameterAssert(drawerSide != MMDrawerSideNone);
  539. UIViewController *sideDrawerViewController = [self sideDrawerViewControllerForSide:drawerSide];
  540. CGFloat oldWidth = 0.f;
  541. NSInteger drawerSideOriginCorrection = 1;
  542. if (drawerSide == MMDrawerSideLeft) {
  543. oldWidth = _maximumLeftDrawerWidth;
  544. _maximumLeftDrawerWidth = width;
  545. }
  546. else if(drawerSide == MMDrawerSideRight){
  547. oldWidth = _maximumRightDrawerWidth;
  548. _maximumRightDrawerWidth = width;
  549. drawerSideOriginCorrection = -1;
  550. }
  551. CGFloat distance = ABS(width-oldWidth);
  552. NSTimeInterval duration = [self animationDurationForAnimationDistance:distance];
  553. if(self.openSide == drawerSide){
  554. CGRect newCenterRect = self.centerContainerView.frame;
  555. newCenterRect.origin.x = drawerSideOriginCorrection*width;
  556. [UIView
  557. animateWithDuration:(animated?duration:0)
  558. delay:0.0
  559. options:UIViewAnimationOptionCurveEaseInOut
  560. animations:^{
  561. [self.centerContainerView setFrame:newCenterRect withLayoutAlpha:1.0];
  562. [sideDrawerViewController.view setFrame:sideDrawerViewController.mm_visibleDrawerFrame];
  563. }
  564. completion:^(BOOL finished) {
  565. if(completion != nil){
  566. completion(finished);
  567. }
  568. }];
  569. }
  570. else{
  571. [sideDrawerViewController.view setFrame:sideDrawerViewController.mm_visibleDrawerFrame];
  572. if(completion != nil){
  573. completion(YES);
  574. }
  575. }
  576. }
  577. #pragma mark - Bounce Methods
  578. -(void)bouncePreviewForDrawerSide:(MMDrawerSide)drawerSide completion:(void(^)(BOOL finished))completion{
  579. NSParameterAssert(drawerSide!=MMDrawerSideNone);
  580. [self bouncePreviewForDrawerSide:drawerSide distance:MMDrawerDefaultBounceDistance completion:completion];
  581. }
  582. -(void)bouncePreviewForDrawerSide:(MMDrawerSide)drawerSide distance:(CGFloat)distance completion:(void(^)(BOOL finished))completion{
  583. NSParameterAssert(drawerSide!=MMDrawerSideNone);
  584. UIViewController * sideDrawerViewController = [self sideDrawerViewControllerForSide:drawerSide];
  585. if(sideDrawerViewController == nil ||
  586. self.openSide != MMDrawerSideNone){
  587. if(completion){
  588. completion(NO);
  589. }
  590. return;
  591. }
  592. else {
  593. [self prepareToPresentDrawer:drawerSide animated:YES];
  594. [self updateDrawerVisualStateForDrawerSide:drawerSide percentVisible:1.0];
  595. [CATransaction begin];
  596. [CATransaction
  597. setCompletionBlock:^{
  598. [sideDrawerViewController endAppearanceTransition];
  599. [sideDrawerViewController beginAppearanceTransition:NO animated:NO];
  600. [sideDrawerViewController endAppearanceTransition];
  601. if(completion){
  602. completion(YES);
  603. }
  604. }];
  605. CGFloat modifier = ((drawerSide == MMDrawerSideLeft)?1.0:-1.0);
  606. CAKeyframeAnimation *animation = bounceKeyFrameAnimationForDistanceOnView(distance*modifier,self.centerContainerView);
  607. [self.centerContainerView.layer addAnimation:animation forKey:@"bouncing"];
  608. [CATransaction commit];
  609. }
  610. }
  611. #pragma mark - Setting Drawer Visual State
  612. -(void)setDrawerVisualStateBlock:(void (^)(MMDrawerController *, MMDrawerSide, CGFloat))drawerVisualStateBlock{
  613. [self setDrawerVisualState:drawerVisualStateBlock];
  614. }
  615. #pragma mark - Setting Custom Gesture Handler Block
  616. -(void)setGestureShouldRecognizeTouchBlock:(BOOL (^)(MMDrawerController *, UIGestureRecognizer *, UITouch *))gestureShouldRecognizeTouchBlock{
  617. [self setGestureShouldRecognizeTouch:gestureShouldRecognizeTouchBlock];
  618. }
  619. #pragma mark - Setting the Gesture Completion Block
  620. -(void)setGestureCompletionBlock:(void (^)(MMDrawerController *, UIGestureRecognizer *))gestureCompletionBlock{
  621. [self setGestureCompletion:gestureCompletionBlock];
  622. }
  623. #pragma mark - Setting the Gesture Start Block
  624. -(void)setGestureStartBlock:(void (^)(MMDrawerController *, UIGestureRecognizer *))gestureStartBlock{
  625. [self setGestureStart:gestureStartBlock];
  626. }
  627. #pragma mark - Subclass Methods
  628. -(BOOL)shouldAutomaticallyForwardAppearanceMethods{
  629. return NO;
  630. }
  631. -(BOOL)shouldAutomaticallyForwardRotationMethods{
  632. return NO;
  633. }
  634. -(BOOL)automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers{
  635. return NO;
  636. }
  637. #pragma mark - View Lifecycle
  638. - (void)viewDidLoad {
  639. [super viewDidLoad];
  640. [self.view setBackgroundColor:[UIColor blackColor]];
  641. [self setupGestureRecognizers];
  642. }
  643. -(void)viewWillAppear:(BOOL)animated{
  644. [super viewWillAppear:animated];
  645. [self.centerViewController beginAppearanceTransition:YES animated:animated];
  646. if(self.openSide == MMDrawerSideLeft) {
  647. [self.leftDrawerViewController beginAppearanceTransition:YES animated:animated];
  648. }
  649. else if(self.openSide == MMDrawerSideRight) {
  650. [self.rightDrawerViewController beginAppearanceTransition:YES animated:animated];
  651. }
  652. }
  653. -(void)viewDidAppear:(BOOL)animated{
  654. [super viewDidAppear:animated];
  655. [self updateShadowForCenterView];
  656. [self.centerViewController endAppearanceTransition];
  657. if(self.openSide == MMDrawerSideLeft) {
  658. [self.leftDrawerViewController endAppearanceTransition];
  659. }
  660. else if(self.openSide == MMDrawerSideRight) {
  661. [self.rightDrawerViewController endAppearanceTransition];
  662. }
  663. }
  664. -(void)viewWillDisappear:(BOOL)animated{
  665. [super viewWillDisappear:animated];
  666. [self.centerViewController beginAppearanceTransition:NO animated:animated];
  667. if(self.openSide == MMDrawerSideLeft) {
  668. [self.leftDrawerViewController beginAppearanceTransition:NO animated:animated];
  669. }
  670. else if (self.openSide == MMDrawerSideRight) {
  671. [self.rightDrawerViewController beginAppearanceTransition:NO animated:animated];
  672. }
  673. }
  674. -(void)viewDidDisappear:(BOOL)animated{
  675. [super viewDidDisappear:animated];
  676. [self.centerViewController endAppearanceTransition];
  677. if(self.openSide == MMDrawerSideLeft) {
  678. [self.leftDrawerViewController endAppearanceTransition];
  679. }
  680. else if (self.openSide == MMDrawerSideRight) {
  681. [self.rightDrawerViewController endAppearanceTransition];
  682. }
  683. }
  684. #pragma mark Rotation
  685. -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
  686. [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
  687. //If a rotation begins, we are going to cancel the current gesture and reset transform and anchor points so everything works correctly
  688. BOOL gestureInProgress = NO;
  689. for(UIGestureRecognizer * gesture in self.view.gestureRecognizers){
  690. if(gesture.state == UIGestureRecognizerStateChanged){
  691. [gesture setEnabled:NO];
  692. [gesture setEnabled:YES];
  693. gestureInProgress = YES;
  694. }
  695. if (gestureInProgress) {
  696. [self resetDrawerVisualStateForDrawerSide:self.openSide];
  697. }
  698. }
  699. if ([self needsManualForwardingOfRotationEvents]){
  700. for(UIViewController * childViewController in self.childViewControllers){
  701. [childViewController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
  702. }
  703. }
  704. }
  705. -(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
  706. [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
  707. //We need to support the shadow path rotation animation
  708. //Inspired from here: http://blog.radi.ws/post/8348898129/calayers-shadowpath-and-uiview-autoresizing
  709. if(self.showsShadow){
  710. CGPathRef oldShadowPath = self.centerContainerView.layer.shadowPath;
  711. if(oldShadowPath){
  712. CFRetain(oldShadowPath);
  713. }
  714. [self updateShadowForCenterView];
  715. if (oldShadowPath) {
  716. [self.centerContainerView.layer addAnimation:((^ {
  717. CABasicAnimation *transition = [CABasicAnimation animationWithKeyPath:@"shadowPath"];
  718. transition.fromValue = (__bridge id)oldShadowPath;
  719. transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  720. transition.duration = duration;
  721. return transition;
  722. })()) forKey:@"transition"];
  723. CFRelease(oldShadowPath);
  724. }
  725. }
  726. if ([self needsManualForwardingOfRotationEvents]){
  727. for(UIViewController * childViewController in self.childViewControllers){
  728. [childViewController willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
  729. }
  730. }
  731. }
  732. -(BOOL)shouldAutorotate{
  733. return YES;
  734. }
  735. -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
  736. [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
  737. if ([self needsManualForwardingOfRotationEvents]){
  738. for(UIViewController * childViewController in self.childViewControllers){
  739. [childViewController didRotateFromInterfaceOrientation:fromInterfaceOrientation];
  740. }
  741. }
  742. }
  743. #pragma mark - Setters
  744. -(void)setRightDrawerViewController:(UIViewController *)rightDrawerViewController{
  745. [self setDrawerViewController:rightDrawerViewController forSide:MMDrawerSideRight];
  746. }
  747. -(void)setLeftDrawerViewController:(UIViewController *)leftDrawerViewController{
  748. [self setDrawerViewController:leftDrawerViewController forSide:MMDrawerSideLeft];
  749. }
  750. - (void)setDrawerViewController:(UIViewController *)viewController forSide:(MMDrawerSide)drawerSide{
  751. NSParameterAssert(drawerSide != MMDrawerSideNone);
  752. UIViewController *currentSideViewController = [self sideDrawerViewControllerForSide:drawerSide];
  753. if (currentSideViewController == viewController) {
  754. return;
  755. }
  756. if (currentSideViewController != nil) {
  757. [currentSideViewController beginAppearanceTransition:NO animated:NO];
  758. [currentSideViewController.view removeFromSuperview];
  759. [currentSideViewController endAppearanceTransition];
  760. [currentSideViewController willMoveToParentViewController:nil];
  761. [currentSideViewController removeFromParentViewController];
  762. }
  763. UIViewAutoresizing autoResizingMask = 0;
  764. if (drawerSide == MMDrawerSideLeft) {
  765. _leftDrawerViewController = viewController;
  766. autoResizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
  767. }
  768. else if(drawerSide == MMDrawerSideRight){
  769. _rightDrawerViewController = viewController;
  770. autoResizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
  771. }
  772. if(viewController){
  773. [self addChildViewController:viewController];
  774. if((self.openSide == drawerSide) &&
  775. [self.childControllerContainerView.subviews containsObject:self.centerContainerView]){
  776. [self.childControllerContainerView insertSubview:viewController.view belowSubview:self.centerContainerView];
  777. [viewController beginAppearanceTransition:YES animated:NO];
  778. [viewController endAppearanceTransition];
  779. }
  780. else{
  781. [self.childControllerContainerView addSubview:viewController.view];
  782. [self.childControllerContainerView sendSubviewToBack:viewController.view];
  783. [viewController.view setHidden:YES];
  784. }
  785. [viewController didMoveToParentViewController:self];
  786. [viewController.view setAutoresizingMask:autoResizingMask];
  787. }
  788. }
  789. -(void)setCenterViewController:(UIViewController *)centerViewController{
  790. [self setCenterViewController:centerViewController animated:NO];
  791. }
  792. -(void)setShowsShadow:(BOOL)showsShadow{
  793. _showsShadow = showsShadow;
  794. [self updateShadowForCenterView];
  795. }
  796. - (void)setShadowRadius:(CGFloat)shadowRadius{
  797. _shadowRadius = shadowRadius;
  798. [self updateShadowForCenterView];
  799. }
  800. - (void)setShadowOpacity:(CGFloat)shadowOpacity{
  801. _shadowOpacity = shadowOpacity;
  802. [self updateShadowForCenterView];
  803. }
  804. - (void)setShadowOffset:(CGSize)shadowOffset{
  805. _shadowOffset = shadowOffset;
  806. [self updateShadowForCenterView];
  807. }
  808. - (void)setShadowColor:(UIColor *)shadowColor{
  809. _shadowColor = shadowColor;
  810. [self updateShadowForCenterView];
  811. }
  812. -(void)setOpenSide:(MMDrawerSide)openSide{
  813. if(_openSide != openSide){
  814. _openSide = openSide;
  815. [self.centerContainerView setOpenSide:openSide];
  816. if(openSide == MMDrawerSideNone){
  817. [self.leftDrawerViewController.view setHidden:YES];
  818. [self.rightDrawerViewController.view setHidden:YES];
  819. }
  820. [self setNeedsStatusBarAppearanceUpdateIfSupported];
  821. }
  822. }
  823. -(void)setCenterHiddenInteractionMode:(MMDrawerOpenCenterInteractionMode)centerHiddenInteractionMode{
  824. if(_centerHiddenInteractionMode!=centerHiddenInteractionMode){
  825. _centerHiddenInteractionMode = centerHiddenInteractionMode;
  826. [self.centerContainerView setCenterInteractionMode:centerHiddenInteractionMode];
  827. }
  828. }
  829. -(void)setMaximumLeftDrawerWidth:(CGFloat)maximumLeftDrawerWidth{
  830. [self setMaximumLeftDrawerWidth:maximumLeftDrawerWidth animated:NO completion:nil];
  831. }
  832. -(void)setMaximumRightDrawerWidth:(CGFloat)maximumRightDrawerWidth{
  833. [self setMaximumRightDrawerWidth:maximumRightDrawerWidth animated:NO completion:nil];
  834. }
  835. -(void)setShowsStatusBarBackgroundView:(BOOL)showsDummyStatusBar{
  836. if(showsDummyStatusBar!=_showsStatusBarBackgroundView){
  837. _showsStatusBarBackgroundView = showsDummyStatusBar;
  838. CGRect frame = self.childControllerContainerView.frame;
  839. if(_showsStatusBarBackgroundView){
  840. frame.origin.y = 20;
  841. frame.size.height = CGRectGetHeight(self.view.bounds)-20;
  842. }
  843. else {
  844. frame.origin.y = 0;
  845. frame.size.height = CGRectGetHeight(self.view.bounds);
  846. }
  847. [self.childControllerContainerView setFrame:frame];
  848. [self.dummyStatusBarView setHidden:!showsDummyStatusBar];
  849. }
  850. }
  851. -(void)setStatusBarViewBackgroundColor:(UIColor *)dummyStatusBarColor{
  852. _statusBarViewBackgroundColor = dummyStatusBarColor;
  853. [self.dummyStatusBarView setBackgroundColor:_statusBarViewBackgroundColor];
  854. }
  855. -(void)setAnimatingDrawer:(BOOL)animatingDrawer{
  856. _animatingDrawer = animatingDrawer;
  857. [self.view setUserInteractionEnabled:!animatingDrawer];
  858. }
  859. #pragma mark - Getters
  860. -(CGFloat)maximumLeftDrawerWidth{
  861. if(self.leftDrawerViewController){
  862. return _maximumLeftDrawerWidth;
  863. }
  864. else{
  865. return 0;
  866. }
  867. }
  868. -(CGFloat)maximumRightDrawerWidth{
  869. if(self.rightDrawerViewController){
  870. return _maximumRightDrawerWidth;
  871. }
  872. else {
  873. return 0;
  874. }
  875. }
  876. -(CGFloat)visibleLeftDrawerWidth{
  877. return MAX(0.0,CGRectGetMinX(self.centerContainerView.frame));
  878. }
  879. -(CGFloat)visibleRightDrawerWidth{
  880. if(CGRectGetMinX(self.centerContainerView.frame)<0){
  881. return CGRectGetWidth(self.childControllerContainerView.bounds)-CGRectGetMaxX(self.centerContainerView.frame);
  882. }
  883. else {
  884. return 0.0f;
  885. }
  886. }
  887. -(UIView*)childControllerContainerView{
  888. if(_childControllerContainerView == nil){
  889. //Issue #152 (https://github.com/mutualmobile/MMDrawerController/issues/152)
  890. //Turns out we have two child container views getting added to the view during init,
  891. //because the first request self.view.bounds was kicking off a viewDidLoad, which
  892. //caused us to be able to fall through this check twice.
  893. //
  894. //The fix is to grab the bounds, and then check again that the child container view has
  895. //not been created.
  896. CGRect childContainerViewFrame = self.view.bounds;
  897. if(_childControllerContainerView == nil){
  898. _childControllerContainerView = [[UIView alloc] initWithFrame:childContainerViewFrame];
  899. [_childControllerContainerView setBackgroundColor:[UIColor clearColor]];
  900. [_childControllerContainerView setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
  901. [self.view addSubview:_childControllerContainerView];
  902. }
  903. }
  904. return _childControllerContainerView;
  905. }
  906. -(UIView*)dummyStatusBarView{
  907. if(_dummyStatusBarView==nil){
  908. _dummyStatusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 20)];
  909. [_dummyStatusBarView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
  910. [_dummyStatusBarView setBackgroundColor:self.statusBarViewBackgroundColor];
  911. [_dummyStatusBarView setHidden:!_showsStatusBarBackgroundView];
  912. [self.view addSubview:_dummyStatusBarView];
  913. }
  914. return _dummyStatusBarView;
  915. }
  916. -(UIColor*)statusBarViewBackgroundColor{
  917. if(_statusBarViewBackgroundColor == nil){
  918. _statusBarViewBackgroundColor = [UIColor blackColor];
  919. }
  920. return _statusBarViewBackgroundColor;
  921. }
  922. #pragma mark - Gesture Handlers
  923. -(void)tapGestureCallback:(UITapGestureRecognizer *)tapGesture{
  924. if(self.openSide != MMDrawerSideNone &&
  925. self.isAnimatingDrawer == NO){
  926. [self closeDrawerAnimated:YES completion:^(BOOL finished) {
  927. if(self.gestureCompletion){
  928. self.gestureCompletion(self, tapGesture);
  929. }
  930. }];
  931. }
  932. }
  933. -(void)panGestureCallback:(UIPanGestureRecognizer *)panGesture{
  934. switch (panGesture.state) {
  935. case UIGestureRecognizerStateBegan:{
  936. if(self.gestureStart){
  937. self.gestureStart(self, panGesture);
  938. }
  939. if(self.animatingDrawer){
  940. [panGesture setEnabled:NO];
  941. break;
  942. }
  943. else {
  944. self.startingPanRect = self.centerContainerView.frame;
  945. }
  946. }
  947. case UIGestureRecognizerStateChanged:{
  948. self.view.userInteractionEnabled = NO;
  949. CGRect newFrame = self.startingPanRect;
  950. CGPoint translatedPoint = [panGesture translationInView:self.centerContainerView];
  951. newFrame.origin.x = [self roundedOriginXForDrawerConstriants:CGRectGetMinX(self.startingPanRect)+translatedPoint.x];
  952. newFrame = CGRectIntegral(newFrame);
  953. CGFloat xOffset = newFrame.origin.x;
  954. MMDrawerSide visibleSide = MMDrawerSideNone;
  955. CGFloat percentVisible = 0.0;
  956. if(xOffset > 0){
  957. visibleSide = MMDrawerSideLeft;
  958. percentVisible = xOffset/self.maximumLeftDrawerWidth;
  959. }
  960. else if(xOffset < 0){
  961. visibleSide = MMDrawerSideRight;
  962. percentVisible = ABS(xOffset)/self.maximumRightDrawerWidth;
  963. }
  964. if ((!_leftSideEnabled && visibleSide == MMDrawerSideLeft) || (!_rightSideEnabled && visibleSide == MMDrawerSideRight)) {
  965. return;
  966. }
  967. UIViewController * visibleSideDrawerViewController = [self sideDrawerViewControllerForSide:visibleSide];
  968. if(self.openSide != visibleSide){
  969. //Handle disappearing the visible drawer
  970. UIViewController * sideDrawerViewController = [self sideDrawerViewControllerForSide:self.openSide];
  971. [sideDrawerViewController beginAppearanceTransition:NO animated:NO];
  972. [sideDrawerViewController endAppearanceTransition];
  973. //Drawer is about to become visible
  974. [self prepareToPresentDrawer:visibleSide animated:NO];
  975. [visibleSideDrawerViewController endAppearanceTransition];
  976. [self setOpenSide:visibleSide];
  977. }
  978. else if(visibleSide == MMDrawerSideNone){
  979. [self setOpenSide:MMDrawerSideNone];
  980. }
  981. [self updateDrawerVisualStateForDrawerSide:visibleSide percentVisible:percentVisible];
  982. [self.centerContainerView setCenter:CGPointMake(CGRectGetMidX(newFrame), CGRectGetMidY(newFrame))];
  983. newFrame = self.centerContainerView.frame;
  984. newFrame.origin.x = floor(newFrame.origin.x);
  985. newFrame.origin.y = floor(newFrame.origin.y);
  986. self.centerContainerView.frame = newFrame;
  987. [self.centerContainerView addSubview:self.centerContainerView.overlayView];
  988. [self.centerContainerView bringSubviewToFront:self.centerContainerView.overlayView];
  989. self.centerContainerView.overlayView.alpha = percentVisible;
  990. break;
  991. }
  992. case UIGestureRecognizerStateEnded:
  993. case UIGestureRecognizerStateCancelled: {
  994. self.startingPanRect = CGRectNull;
  995. CGPoint velocity = [panGesture velocityInView:self.childControllerContainerView];
  996. [self finishAnimationForPanGestureWithXVelocity:velocity.x completion:^(BOOL finished) {
  997. if(self.gestureCompletion){
  998. self.gestureCompletion(self, panGesture);
  999. }
  1000. }];
  1001. self.view.userInteractionEnabled = YES;
  1002. break;
  1003. }
  1004. default:
  1005. break;
  1006. }
  1007. }
  1008. #pragma mark - iOS 7 Status Bar Helpers
  1009. -(UIViewController*)childViewControllerForStatusBarStyle{
  1010. return [self childViewControllerForSide:self.openSide];
  1011. }
  1012. -(UIViewController*)childViewControllerForStatusBarHidden{
  1013. return [self childViewControllerForSide:self.openSide];
  1014. }
  1015. -(void)setNeedsStatusBarAppearanceUpdateIfSupported{
  1016. if([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]){
  1017. [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
  1018. }
  1019. }
  1020. #pragma mark - iOS 8 Rotation Helpers
  1021. - (BOOL)needsManualForwardingOfRotationEvents{
  1022. BOOL isIOS8 = (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_7_1);
  1023. return !isIOS8;
  1024. }
  1025. #pragma mark - Animation helpers
  1026. -(void)finishAnimationForPanGestureWithXVelocity:(CGFloat)xVelocity completion:(void(^)(BOOL finished))completion{
  1027. CGFloat currentOriginX = CGRectGetMinX(self.centerContainerView.frame);
  1028. CGFloat animationVelocity = MAX(ABS(xVelocity),self.panVelocityXAnimationThreshold*2);
  1029. if(self.openSide == MMDrawerSideLeft) {
  1030. CGFloat midPoint = self.maximumLeftDrawerWidth / 2.0;
  1031. if(xVelocity > self.panVelocityXAnimationThreshold){
  1032. [self openDrawerSide:MMDrawerSideLeft animated:YES velocity:animationVelocity animationOptions:UIViewAnimationOptionCurveEaseOut completion:completion];
  1033. }
  1034. else if(xVelocity < -self.panVelocityXAnimationThreshold){
  1035. [self closeDrawerAnimated:YES velocity:animationVelocity animationOptions:UIViewAnimationOptionCurveEaseOut completion:completion];
  1036. }
  1037. else if(currentOriginX < midPoint){
  1038. [self closeDrawerAnimated:YES completion:completion];
  1039. }
  1040. else {
  1041. [self openDrawerSide:MMDrawerSideLeft animated:YES completion:completion];
  1042. }
  1043. }
  1044. else if(self.openSide == MMDrawerSideRight){
  1045. currentOriginX = CGRectGetMaxX(self.centerContainerView.frame);
  1046. CGFloat midPoint = (CGRectGetWidth(self.childControllerContainerView.bounds)-self.maximumRightDrawerWidth) + (self.maximumRightDrawerWidth / 2.0);
  1047. if(xVelocity > self.panVelocityXAnimationThreshold){
  1048. [self closeDrawerAnimated:YES velocity:animationVelocity animationOptions:UIViewAnimationOptionCurveEaseOut completion:completion];
  1049. }
  1050. else if (xVelocity < -self.panVelocityXAnimationThreshold){
  1051. [self openDrawerSide:MMDrawerSideRight animated:YES velocity:animationVelocity animationOptions:UIViewAnimationOptionCurveEaseOut completion:completion];
  1052. }
  1053. else if(currentOriginX > midPoint){
  1054. [self closeDrawerAnimated:YES completion:completion];
  1055. }
  1056. else {
  1057. [self openDrawerSide:MMDrawerSideRight animated:YES completion:completion];
  1058. }
  1059. }
  1060. else {
  1061. if(completion){
  1062. completion(NO);
  1063. }
  1064. }
  1065. }
  1066. -(void)updateDrawerVisualStateForDrawerSide:(MMDrawerSide)drawerSide percentVisible:(CGFloat)percentVisible{
  1067. if(self.drawerVisualState){
  1068. self.drawerVisualState(self,drawerSide,percentVisible);
  1069. }
  1070. else if(self.shouldStretchDrawer){
  1071. [self applyOvershootScaleTransformForDrawerSide:drawerSide percentVisible:percentVisible];
  1072. }
  1073. }
  1074. - (void)applyOvershootScaleTransformForDrawerSide:(MMDrawerSide)drawerSide percentVisible:(CGFloat)percentVisible{
  1075. if (percentVisible >= 1.f) {
  1076. CATransform3D transform = CATransform3DIdentity;
  1077. UIViewController * sideDrawerViewController = [self sideDrawerViewControllerForSide:drawerSide];
  1078. if(drawerSide == MMDrawerSideLeft) {
  1079. transform = CATransform3DMakeScale(percentVisible, 1.f, 1.f);
  1080. transform = CATransform3DTranslate(transform, self.maximumLeftDrawerWidth*(percentVisible-1.f)/2, 0.f, 0.f);
  1081. }
  1082. else if(drawerSide == MMDrawerSideRight){
  1083. transform = CATransform3DMakeScale(percentVisible, 1.f, 1.f);
  1084. transform = CATransform3DTranslate(transform, -self.maximumRightDrawerWidth*(percentVisible-1.f)/2, 0.f, 0.f);
  1085. }
  1086. sideDrawerViewController.view.layer.transform = transform;
  1087. }
  1088. }
  1089. -(void)resetDrawerVisualStateForDrawerSide:(MMDrawerSide)drawerSide{
  1090. UIViewController * sideDrawerViewController = [self sideDrawerViewControllerForSide:drawerSide];
  1091. [sideDrawerViewController.view.layer setAnchorPoint:CGPointMake(0.5f, 0.5f)];
  1092. [sideDrawerViewController.view.layer setTransform:CATransform3DIdentity];
  1093. [sideDrawerViewController.view setAlpha:1.0];
  1094. }
  1095. -(CGFloat)roundedOriginXForDrawerConstriants:(CGFloat)originX{
  1096. if (originX < -self.maximumRightDrawerWidth) {
  1097. if (self.shouldStretchDrawer &&
  1098. self.rightDrawerViewController) {
  1099. CGFloat maxOvershoot = (CGRectGetWidth(self.centerContainerView.frame)-self.maximumRightDrawerWidth)*MMDrawerOvershootPercentage;
  1100. return originXForDrawerOriginAndTargetOriginOffset(originX, -self.maximumRightDrawerWidth, maxOvershoot);
  1101. }
  1102. else{
  1103. return -self.maximumRightDrawerWidth;
  1104. }
  1105. }
  1106. else if(originX > self.maximumLeftDrawerWidth){
  1107. if (self.shouldStretchDrawer &&
  1108. self.leftDrawerViewController) {
  1109. CGFloat maxOvershoot = (CGRectGetWidth(self.centerContainerView.frame)-self.maximumLeftDrawerWidth)*MMDrawerOvershootPercentage;
  1110. return originXForDrawerOriginAndTargetOriginOffset(originX, self.maximumLeftDrawerWidth, maxOvershoot);
  1111. }
  1112. else{
  1113. return self.maximumLeftDrawerWidth;
  1114. }
  1115. }
  1116. return originX;
  1117. }
  1118. static inline CGFloat originXForDrawerOriginAndTargetOriginOffset(CGFloat originX, CGFloat targetOffset, CGFloat maxOvershoot){
  1119. CGFloat delta = ABS(originX - targetOffset);
  1120. CGFloat maxLinearPercentage = MMDrawerOvershootLinearRangePercentage;
  1121. CGFloat nonLinearRange = maxOvershoot * maxLinearPercentage;
  1122. CGFloat nonLinearScalingDelta = (delta - nonLinearRange);
  1123. CGFloat overshoot = nonLinearRange + nonLinearScalingDelta * nonLinearRange/sqrt(pow(nonLinearScalingDelta,2.f) + 15000);
  1124. if (delta < nonLinearRange) {
  1125. return originX;
  1126. }
  1127. else if (targetOffset < 0) {
  1128. return targetOffset - round(overshoot);
  1129. }
  1130. else{
  1131. return targetOffset + round(overshoot);
  1132. }
  1133. }
  1134. #pragma mark - Helpers
  1135. -(void)setupGestureRecognizers{
  1136. UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureCallback:)];
  1137. [pan setDelegate:self];
  1138. [self.view addGestureRecognizer:pan];
  1139. UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureCallback:)];
  1140. [tap setDelegate:self];
  1141. [self.view addGestureRecognizer:tap];
  1142. }
  1143. -(void)prepareToPresentDrawer:(MMDrawerSide)drawer animated:(BOOL)animated{
  1144. MMDrawerSide drawerToHide = MMDrawerSideNone;
  1145. if(drawer == MMDrawerSideLeft){
  1146. drawerToHide = MMDrawerSideRight;
  1147. }
  1148. else if(drawer == MMDrawerSideRight){
  1149. drawerToHide = MMDrawerSideLeft;
  1150. }
  1151. UIViewController * sideDrawerViewControllerToPresent = [self sideDrawerViewControllerForSide:drawer];
  1152. UIViewController * sideDrawerViewControllerToHide = [self sideDrawerViewControllerForSide:drawerToHide];
  1153. [self.childControllerContainerView sendSubviewToBack:sideDrawerViewControllerToHide.view];
  1154. [sideDrawerViewControllerToHide.view setHidden:YES];
  1155. [sideDrawerViewControllerToPresent.view setHidden:NO];
  1156. [self resetDrawerVisualStateForDrawerSide:drawer];
  1157. [sideDrawerViewControllerToPresent.view setFrame:sideDrawerViewControllerToPresent.mm_visibleDrawerFrame];
  1158. [self updateDrawerVisualStateForDrawerSide:drawer percentVisible:0.0];
  1159. [sideDrawerViewControllerToPresent beginAppearanceTransition:YES animated:animated];
  1160. }
  1161. -(void)updateShadowForCenterView{
  1162. UIView * centerView = self.centerContainerView;
  1163. if(self.showsShadow){
  1164. centerView.layer.masksToBounds = NO;
  1165. centerView.layer.shadowRadius = self.shadowRadius;
  1166. centerView.layer.shadowOpacity = self.shadowOpacity;
  1167. centerView.layer.shadowOffset = self.shadowOffset;
  1168. centerView.layer.shadowColor = [self.shadowColor CGColor];
  1169. /** In the event this gets called a lot, we won't update the shadowPath
  1170. unless it needs to be updated (like during rotation) */
  1171. if (centerView.layer.shadowPath == NULL) {
  1172. centerView.layer.shadowPath = [[UIBezierPath bezierPathWithRect:self.centerContainerView.bounds] CGPath];
  1173. }
  1174. else{
  1175. CGRect currentPath = CGPathGetPathBoundingBox(centerView.layer.shadowPath);
  1176. if (CGRectEqualToRect(currentPath, centerView.bounds) == NO){
  1177. centerView.layer.shadowPath = [[UIBezierPath bezierPathWithRect:self.centerContainerView.bounds] CGPath];
  1178. }
  1179. }
  1180. }
  1181. else if (centerView.layer.shadowPath != NULL) {
  1182. centerView.layer.shadowRadius = 0.f;
  1183. centerView.layer.shadowOpacity = 0.f;
  1184. centerView.layer.shadowOffset = CGSizeMake(0, -3);
  1185. centerView.layer.shadowPath = NULL;
  1186. centerView.layer.masksToBounds = YES;
  1187. }
  1188. }
  1189. -(NSTimeInterval)animationDurationForAnimationDistance:(CGFloat)distance{
  1190. NSTimeInterval duration = MAX(distance/self.animationVelocity,MMDrawerMinimumAnimationDuration);
  1191. return duration;
  1192. }
  1193. -(UIViewController*)sideDrawerViewControllerForSide:(MMDrawerSide)drawerSide{
  1194. UIViewController * sideDrawerViewController = nil;
  1195. if(drawerSide != MMDrawerSideNone){
  1196. sideDrawerViewController = [self childViewControllerForSide:drawerSide];
  1197. }
  1198. return sideDrawerViewController;
  1199. }
  1200. -(UIViewController*)childViewControllerForSide:(MMDrawerSide)drawerSide{
  1201. UIViewController * childViewController = nil;
  1202. switch (drawerSide) {
  1203. case MMDrawerSideLeft:
  1204. childViewController = self.leftDrawerViewController;
  1205. break;
  1206. case MMDrawerSideRight:
  1207. childViewController = self.rightDrawerViewController;
  1208. break;
  1209. case MMDrawerSideNone:
  1210. childViewController = self.centerViewController;
  1211. break;
  1212. }
  1213. return childViewController;
  1214. }
  1215. #pragma mark - UIGestureRecognizerDelegate
  1216. -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
  1217. if(self.openSide == MMDrawerSideNone){
  1218. MMOpenDrawerGestureMode possibleOpenGestureModes = [self possibleOpenGestureModesForGestureRecognizer:gestureRecognizer
  1219. withTouch:touch];
  1220. return ((self.openDrawerGestureModeMask & possibleOpenGestureModes)>0);
  1221. }
  1222. else{
  1223. MMCloseDrawerGestureMode possibleCloseGestureModes = [self possibleCloseGestureModesForGestureRecognizer:gestureRecognizer
  1224. withTouch:touch];
  1225. return ((self.closeDrawerGestureModeMask & possibleCloseGestureModes)>0);
  1226. }
  1227. }
  1228. #pragma mark Gesture Recogizner Delegate Helpers
  1229. -(MMCloseDrawerGestureMode)possibleCloseGestureModesForGestureRecognizer:(UIGestureRecognizer*)gestureRecognizer withTouch:(UITouch*)touch{
  1230. CGPoint point = [touch locationInView:self.childControllerContainerView];
  1231. MMCloseDrawerGestureMode possibleCloseGestureModes = MMCloseDrawerGestureModeNone;
  1232. if([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]){
  1233. if([self isPointContainedWithinNavigationRect:point]){
  1234. possibleCloseGestureModes |= MMCloseDrawerGestureModeTapNavigationBar;
  1235. }
  1236. if([self isPointContainedWithinCenterViewContentRect:point]){
  1237. possibleCloseGestureModes |= MMCloseDrawerGestureModeTapCenterView;
  1238. }
  1239. }
  1240. else if([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]){
  1241. if([self isPointContainedWithinNavigationRect:point]){
  1242. possibleCloseGestureModes |= MMCloseDrawerGestureModePanningNavigationBar;
  1243. }
  1244. if([self isPointContainedWithinCenterViewContentRect:point]){
  1245. possibleCloseGestureModes |= MMCloseDrawerGestureModePanningCenterView;
  1246. }
  1247. if([self isPointContainedWithinRightBezelRect:point] &&
  1248. self.openSide == MMDrawerSideLeft){
  1249. possibleCloseGestureModes |= MMCloseDrawerGestureModeBezelPanningCenterView;
  1250. }
  1251. if([self isPointContainedWithinLeftBezelRect:point] &&
  1252. self.openSide == MMDrawerSideRight){
  1253. possibleCloseGestureModes |= MMCloseDrawerGestureModeBezelPanningCenterView;
  1254. }
  1255. if([self isPointContainedWithinCenterViewContentRect:point] == NO &&
  1256. [self isPointContainedWithinNavigationRect:point] == NO){
  1257. possibleCloseGestureModes |= MMCloseDrawerGestureModePanningDrawerView;
  1258. }
  1259. }
  1260. if((self.closeDrawerGestureModeMask & MMCloseDrawerGestureModeCustom) > 0 &&
  1261. self.gestureShouldRecognizeTouch){
  1262. if(self.gestureShouldRecognizeTouch(self,gestureRecognizer,touch)){
  1263. possibleCloseGestureModes |= MMCloseDrawerGestureModeCustom;
  1264. }
  1265. }
  1266. return possibleCloseGestureModes;
  1267. }
  1268. -(MMOpenDrawerGestureMode)possibleOpenGestureModesForGestureRecognizer:(UIGestureRecognizer*)gestureRecognizer withTouch:(UITouch*)touch{
  1269. CGPoint point = [touch locationInView:self.childControllerContainerView];
  1270. MMOpenDrawerGestureMode possibleOpenGestureModes = MMOpenDrawerGestureModeNone;
  1271. if([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]){
  1272. if([self isPointContainedWithinNavigationRect:point]){
  1273. possibleOpenGestureModes |= MMOpenDrawerGestureModePanningNavigationBar;
  1274. }
  1275. if([self isPointContainedWithinCenterViewContentRect:point]){
  1276. possibleOpenGestureModes |= MMOpenDrawerGestureModePanningCenterView;
  1277. }
  1278. if([self isPointContainedWithinLeftBezelRect:point] &&
  1279. self.leftDrawerViewController){
  1280. possibleOpenGestureModes |= MMOpenDrawerGestureModeBezelPanningCenterView;
  1281. }
  1282. if([self isPointContainedWithinRightBezelRect:point] &&
  1283. self.rightDrawerViewController){
  1284. possibleOpenGestureModes |= MMOpenDrawerGestureModeBezelPanningCenterView;
  1285. }
  1286. }
  1287. if((self.openDrawerGestureModeMask & MMOpenDrawerGestureModeCustom) > 0 &&
  1288. self.gestureShouldRecognizeTouch){
  1289. if(self.gestureShouldRecognizeTouch(self,gestureRecognizer,touch)){
  1290. possibleOpenGestureModes |= MMOpenDrawerGestureModeCustom;
  1291. }
  1292. }
  1293. return possibleOpenGestureModes;
  1294. }
  1295. -(BOOL)isPointContainedWithinNavigationRect:(CGPoint)point{
  1296. CGRect navigationBarRect = CGRectNull;
  1297. if([self.centerViewController isKindOfClass:[UINavigationController class]]){
  1298. UINavigationBar * navBar = [(UINavigationController*)self.centerViewController navigationBar];
  1299. navigationBarRect = [navBar convertRect:navBar.bounds toView:self.childControllerContainerView];
  1300. navigationBarRect = CGRectIntersection(navigationBarRect,self.childControllerContainerView.bounds);
  1301. }
  1302. return CGRectContainsPoint(navigationBarRect,point);
  1303. }
  1304. -(BOOL)isPointContainedWithinCenterViewContentRect:(CGPoint)point{
  1305. CGRect centerViewContentRect = self.centerContainerView.frame;
  1306. centerViewContentRect = CGRectIntersection(centerViewContentRect,self.childControllerContainerView.bounds);
  1307. return (CGRectContainsPoint(centerViewContentRect, point) &&
  1308. [self isPointContainedWithinNavigationRect:point] == NO);
  1309. }
  1310. -(BOOL)isPointContainedWithinLeftBezelRect:(CGPoint)point{
  1311. CGRect leftBezelRect = CGRectNull;
  1312. CGRect tempRect;
  1313. CGRectDivide(self.childControllerContainerView.bounds, &leftBezelRect, &tempRect, self.bezelPanningCenterViewRange, CGRectMinXEdge);
  1314. return (CGRectContainsPoint(leftBezelRect, point) &&
  1315. [self isPointContainedWithinCenterViewContentRect:point]);
  1316. }
  1317. -(BOOL)isPointContainedWithinRightBezelRect:(CGPoint)point{
  1318. CGRect rightBezelRect = CGRectNull;
  1319. CGRect tempRect;
  1320. CGRectDivide(self.childControllerContainerView.bounds, &rightBezelRect, &tempRect, self.bezelPanningCenterViewRange, CGRectMaxXEdge);
  1321. return (CGRectContainsPoint(rightBezelRect, point) &&
  1322. [self isPointContainedWithinCenterViewContentRect:point]);
  1323. }
  1324. @end