react-native-navigation的迁移库

MMDrawerController.m 64KB

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