react-native-navigation的迁移库

MMDrawerController.m 64KB

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