react-native-navigation的迁移库

MMDrawerController.m 65KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595
  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. if (self.rightDrawerViewController != nil) {
  732. for (UIView * subview in self.rightDrawerViewController.view.subviews) {
  733. subview.frame = self.view.bounds;
  734. }
  735. }
  736. if (self.leftDrawerViewController != nil) {
  737. for (UIView * subview in self.leftDrawerViewController.view.subviews) {
  738. subview.frame = self.view.bounds;
  739. }
  740. }
  741. }
  742. -(bool)hasPan
  743. {
  744. for (UIGestureRecognizer *recognizer in self.view.gestureRecognizers) {
  745. if(recognizer == _pan) { return YES; }
  746. }
  747. return NO;
  748. }
  749. #pragma mark - Setters
  750. -(void)setRightDrawerViewController:(UIViewController *)rightDrawerViewController{
  751. [self setDrawerViewController:rightDrawerViewController forSide:MMDrawerSideRight];
  752. }
  753. -(void)setLeftDrawerViewController:(UIViewController *)leftDrawerViewController{
  754. [self setDrawerViewController:leftDrawerViewController forSide:MMDrawerSideLeft];
  755. }
  756. - (void)setDrawerViewController:(UIViewController *)viewController forSide:(MMDrawerSide)drawerSide{
  757. NSParameterAssert(drawerSide != MMDrawerSideNone);
  758. UIViewController *currentSideViewController = [self sideDrawerViewControllerForSide:drawerSide];
  759. if (currentSideViewController == viewController) {
  760. return;
  761. }
  762. if (currentSideViewController != nil) {
  763. [currentSideViewController beginAppearanceTransition:NO animated:NO];
  764. [currentSideViewController.view removeFromSuperview];
  765. [currentSideViewController endAppearanceTransition];
  766. [currentSideViewController willMoveToParentViewController:nil];
  767. [currentSideViewController removeFromParentViewController];
  768. }
  769. UIViewAutoresizing autoResizingMask = 0;
  770. if (drawerSide == MMDrawerSideLeft) {
  771. _leftDrawerViewController = viewController;
  772. autoResizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
  773. }
  774. else if(drawerSide == MMDrawerSideRight){
  775. _rightDrawerViewController = viewController;
  776. autoResizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
  777. }
  778. if(viewController){
  779. [self addChildViewController:viewController];
  780. if((self.openSide == drawerSide) &&
  781. [self.childControllerContainerView.subviews containsObject:self.centerContainerView]){
  782. [self.childControllerContainerView insertSubview:viewController.view belowSubview:self.centerContainerView];
  783. [viewController beginAppearanceTransition:YES animated:NO];
  784. [viewController endAppearanceTransition];
  785. }
  786. else{
  787. [self.childControllerContainerView addSubview:viewController.view];
  788. [self.childControllerContainerView sendSubviewToBack:viewController.view];
  789. [viewController.view setHidden:YES];
  790. }
  791. [viewController didMoveToParentViewController:self];
  792. [viewController.view setAutoresizingMask:autoResizingMask];
  793. }
  794. }
  795. -(void)setCenterViewController:(UIViewController *)centerViewController{
  796. [self setCenterViewController:centerViewController animated:NO];
  797. }
  798. -(void)setShowsShadow:(BOOL)showsShadow{
  799. _showsShadow = showsShadow;
  800. [self updateShadowForCenterView];
  801. }
  802. - (void)setShadowRadius:(CGFloat)shadowRadius{
  803. _shadowRadius = shadowRadius;
  804. [self updateShadowForCenterView];
  805. }
  806. - (void)setShadowOpacity:(CGFloat)shadowOpacity{
  807. _shadowOpacity = shadowOpacity;
  808. [self updateShadowForCenterView];
  809. }
  810. - (void)setShadowOffset:(CGSize)shadowOffset{
  811. _shadowOffset = shadowOffset;
  812. [self updateShadowForCenterView];
  813. }
  814. - (void)setShadowColor:(UIColor *)shadowColor{
  815. _shadowColor = shadowColor;
  816. [self updateShadowForCenterView];
  817. }
  818. -(void)setOpenSide:(MMDrawerSide)openSide{
  819. if(_openSide != openSide){
  820. _openSide = openSide;
  821. [self.centerContainerView setOpenSide:openSide];
  822. if(openSide == MMDrawerSideNone){
  823. [self.leftDrawerViewController.view setHidden:YES];
  824. [self.rightDrawerViewController.view setHidden:YES];
  825. }
  826. [self setNeedsStatusBarAppearanceUpdateIfSupported];
  827. }
  828. }
  829. -(void)setCenterHiddenInteractionMode:(MMDrawerOpenCenterInteractionMode)centerHiddenInteractionMode{
  830. if(_centerHiddenInteractionMode!=centerHiddenInteractionMode){
  831. _centerHiddenInteractionMode = centerHiddenInteractionMode;
  832. [self.centerContainerView setCenterInteractionMode:centerHiddenInteractionMode];
  833. }
  834. }
  835. -(void)setMaximumLeftDrawerWidth:(CGFloat)maximumLeftDrawerWidth{
  836. [self setMaximumLeftDrawerWidth:maximumLeftDrawerWidth animated:NO completion:nil];
  837. }
  838. -(void)setMaximumRightDrawerWidth:(CGFloat)maximumRightDrawerWidth{
  839. [self setMaximumRightDrawerWidth:maximumRightDrawerWidth animated:NO completion:nil];
  840. }
  841. -(void)setShowsStatusBarBackgroundView:(BOOL)showsDummyStatusBar{
  842. if(showsDummyStatusBar!=_showsStatusBarBackgroundView){
  843. _showsStatusBarBackgroundView = showsDummyStatusBar;
  844. CGRect frame = self.childControllerContainerView.frame;
  845. if(_showsStatusBarBackgroundView){
  846. frame.origin.y = 20;
  847. frame.size.height = CGRectGetHeight(self.view.bounds)-20;
  848. }
  849. else {
  850. frame.origin.y = 0;
  851. frame.size.height = CGRectGetHeight(self.view.bounds);
  852. }
  853. [self.childControllerContainerView setFrame:frame];
  854. [self.dummyStatusBarView setHidden:!showsDummyStatusBar];
  855. }
  856. }
  857. -(void)setStatusBarViewBackgroundColor:(UIColor *)dummyStatusBarColor{
  858. _statusBarViewBackgroundColor = dummyStatusBarColor;
  859. [self.dummyStatusBarView setBackgroundColor:_statusBarViewBackgroundColor];
  860. }
  861. -(void)setAnimatingDrawer:(BOOL)animatingDrawer{
  862. _animatingDrawer = animatingDrawer;
  863. [self.view setUserInteractionEnabled:!animatingDrawer];
  864. }
  865. - (void)setLeftSideEnabled:(BOOL)leftSideEnabled
  866. {
  867. _leftSideEnabled = leftSideEnabled;
  868. [self updatePanHandlersState];
  869. }
  870. - (void)setRightSideEnabled:(BOOL)rightSideEnabled
  871. {
  872. _rightSideEnabled = rightSideEnabled;
  873. [self updatePanHandlersState];
  874. }
  875. #pragma mark - Getters
  876. -(CGFloat)maximumLeftDrawerWidth{
  877. if(self.leftDrawerViewController){
  878. return _maximumLeftDrawerWidth;
  879. }
  880. else{
  881. return 0;
  882. }
  883. }
  884. -(CGFloat)maximumRightDrawerWidth{
  885. if(self.rightDrawerViewController){
  886. return _maximumRightDrawerWidth;
  887. }
  888. else {
  889. return 0;
  890. }
  891. }
  892. -(CGFloat)visibleLeftDrawerWidth{
  893. return MAX(0.0,CGRectGetMinX(self.centerContainerView.frame));
  894. }
  895. -(CGFloat)visibleRightDrawerWidth{
  896. if(CGRectGetMinX(self.centerContainerView.frame)<0){
  897. return CGRectGetWidth(self.childControllerContainerView.bounds)-CGRectGetMaxX(self.centerContainerView.frame);
  898. }
  899. else {
  900. return 0.0f;
  901. }
  902. }
  903. -(UIView*)childControllerContainerView{
  904. if(_childControllerContainerView == nil){
  905. //Issue #152 (https://github.com/mutualmobile/MMDrawerController/issues/152)
  906. //Turns out we have two child container views getting added to the view during init,
  907. //because the first request self.view.bounds was kicking off a viewDidLoad, which
  908. //caused us to be able to fall through this check twice.
  909. //
  910. //The fix is to grab the bounds, and then check again that the child container view has
  911. //not been created.
  912. CGRect childContainerViewFrame = self.view.bounds;
  913. if(_childControllerContainerView == nil){
  914. _childControllerContainerView = [[UIView alloc] initWithFrame:childContainerViewFrame];
  915. [_childControllerContainerView setBackgroundColor:[UIColor clearColor]];
  916. [_childControllerContainerView setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
  917. [self.view addSubview:_childControllerContainerView];
  918. }
  919. }
  920. return _childControllerContainerView;
  921. }
  922. -(UIView*)dummyStatusBarView{
  923. if(_dummyStatusBarView==nil){
  924. _dummyStatusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 20)];
  925. [_dummyStatusBarView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
  926. [_dummyStatusBarView setBackgroundColor:self.statusBarViewBackgroundColor];
  927. [_dummyStatusBarView setHidden:!_showsStatusBarBackgroundView];
  928. [self.view addSubview:_dummyStatusBarView];
  929. }
  930. return _dummyStatusBarView;
  931. }
  932. -(UIColor*)statusBarViewBackgroundColor{
  933. if(_statusBarViewBackgroundColor == nil){
  934. _statusBarViewBackgroundColor = [UIColor blackColor];
  935. }
  936. return _statusBarViewBackgroundColor;
  937. }
  938. #pragma mark - Gesture Handlers
  939. -(void)tapGestureCallback:(UITapGestureRecognizer *)tapGesture{
  940. if(self.openSide != MMDrawerSideNone &&
  941. self.isAnimatingDrawer == NO){
  942. [self closeDrawerAnimated:YES completion:^(BOOL finished) {
  943. if(self.gestureCompletion){
  944. self.gestureCompletion(self, tapGesture);
  945. }
  946. }];
  947. }
  948. }
  949. -(void)panGestureCallback:(UIPanGestureRecognizer *)panGesture{
  950. switch (panGesture.state) {
  951. case UIGestureRecognizerStateBegan:{
  952. if(self.gestureStart){
  953. self.gestureStart(self, panGesture);
  954. }
  955. if(self.animatingDrawer){
  956. [panGesture setEnabled:NO];
  957. break;
  958. }
  959. else {
  960. self.startingPanRect = self.centerContainerView.frame;
  961. }
  962. }
  963. case UIGestureRecognizerStateChanged:{
  964. self.view.userInteractionEnabled = NO;
  965. CGRect newFrame = self.startingPanRect;
  966. CGPoint translatedPoint = [panGesture translationInView:self.centerContainerView];
  967. newFrame.origin.x = [self roundedOriginXForDrawerConstriants:CGRectGetMinX(self.startingPanRect)+translatedPoint.x];
  968. newFrame = CGRectIntegral(newFrame);
  969. CGFloat xOffset = newFrame.origin.x;
  970. MMDrawerSide visibleSide = MMDrawerSideNone;
  971. CGFloat percentVisible = 0.0;
  972. if(xOffset > 0){
  973. visibleSide = MMDrawerSideLeft;
  974. percentVisible = xOffset/self.maximumLeftDrawerWidth;
  975. }
  976. else if(xOffset < 0){
  977. visibleSide = MMDrawerSideRight;
  978. percentVisible = ABS(xOffset)/self.maximumRightDrawerWidth;
  979. }
  980. if ((!_leftSideEnabled && visibleSide == MMDrawerSideLeft) || (!_rightSideEnabled && visibleSide == MMDrawerSideRight)) {
  981. return;
  982. }
  983. UIViewController * visibleSideDrawerViewController = [self sideDrawerViewControllerForSide:visibleSide];
  984. if(self.openSide != visibleSide){
  985. //Handle disappearing the visible drawer
  986. UIViewController * sideDrawerViewController = [self sideDrawerViewControllerForSide:self.openSide];
  987. [sideDrawerViewController beginAppearanceTransition:NO animated:NO];
  988. [sideDrawerViewController endAppearanceTransition];
  989. //Drawer is about to become visible
  990. [self prepareToPresentDrawer:visibleSide animated:NO];
  991. [visibleSideDrawerViewController endAppearanceTransition];
  992. [self setOpenSide:visibleSide];
  993. }
  994. else if(visibleSide == MMDrawerSideNone){
  995. [self setOpenSide:MMDrawerSideNone];
  996. }
  997. [self updateDrawerVisualStateForDrawerSide:visibleSide percentVisible:percentVisible];
  998. [self.centerContainerView setCenter:CGPointMake(CGRectGetMidX(newFrame), CGRectGetMidY(newFrame))];
  999. newFrame = self.centerContainerView.frame;
  1000. newFrame.origin.x = floor(newFrame.origin.x);
  1001. newFrame.origin.y = floor(newFrame.origin.y);
  1002. self.centerContainerView.frame = newFrame;
  1003. [self.centerContainerView addSubview:self.centerContainerView.overlayView];
  1004. [self.centerContainerView bringSubviewToFront:self.centerContainerView.overlayView];
  1005. self.centerContainerView.overlayView.alpha = percentVisible;
  1006. break;
  1007. }
  1008. case UIGestureRecognizerStateEnded:
  1009. case UIGestureRecognizerStateCancelled: {
  1010. self.startingPanRect = CGRectNull;
  1011. CGPoint velocity = [panGesture velocityInView:self.childControllerContainerView];
  1012. [self finishAnimationForPanGestureWithXVelocity:velocity.x completion:^(BOOL finished) {
  1013. if(self.gestureCompletion){
  1014. self.gestureCompletion(self, panGesture);
  1015. }
  1016. }];
  1017. self.view.userInteractionEnabled = YES;
  1018. break;
  1019. }
  1020. default:
  1021. break;
  1022. }
  1023. }
  1024. - (void)updatePanHandlersState
  1025. {
  1026. if(_leftSideEnabled == NO && _rightSideEnabled == NO) {
  1027. if([self hasPan]) { [self.view removeGestureRecognizer:_pan]; }
  1028. } else {
  1029. if(![self hasPan]) { [self.view addGestureRecognizer:_pan]; }
  1030. }
  1031. }
  1032. #pragma mark - iOS 7 Status Bar Helpers
  1033. -(UIViewController*)childViewControllerForStatusBarStyle{
  1034. return [self childViewControllerForSide:self.openSide];
  1035. }
  1036. -(UIViewController*)childViewControllerForStatusBarHidden{
  1037. return [self childViewControllerForSide:self.openSide];
  1038. }
  1039. -(void)setNeedsStatusBarAppearanceUpdateIfSupported{
  1040. if([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]){
  1041. [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
  1042. }
  1043. }
  1044. #pragma mark - iOS 8 Rotation Helpers
  1045. - (BOOL)needsManualForwardingOfRotationEvents{
  1046. BOOL isIOS8 = (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_7_1);
  1047. return !isIOS8;
  1048. }
  1049. #pragma mark - Animation helpers
  1050. -(void)finishAnimationForPanGestureWithXVelocity:(CGFloat)xVelocity completion:(void(^)(BOOL finished))completion{
  1051. CGFloat currentOriginX = CGRectGetMinX(self.centerContainerView.frame);
  1052. CGFloat animationVelocity = MAX(ABS(xVelocity),self.panVelocityXAnimationThreshold*2);
  1053. if(self.openSide == MMDrawerSideLeft) {
  1054. CGFloat midPoint = self.maximumLeftDrawerWidth / 2.0;
  1055. if(xVelocity > self.panVelocityXAnimationThreshold){
  1056. [self openDrawerSide:MMDrawerSideLeft animated:YES velocity:animationVelocity animationOptions:UIViewAnimationOptionCurveEaseOut completion:completion];
  1057. }
  1058. else if(xVelocity < -self.panVelocityXAnimationThreshold){
  1059. [self closeDrawerAnimated:YES velocity:animationVelocity animationOptions:UIViewAnimationOptionCurveEaseOut completion:completion];
  1060. }
  1061. else if(currentOriginX < midPoint){
  1062. [self closeDrawerAnimated:YES completion:completion];
  1063. }
  1064. else {
  1065. [self openDrawerSide:MMDrawerSideLeft animated:YES completion:completion];
  1066. }
  1067. }
  1068. else if(self.openSide == MMDrawerSideRight){
  1069. currentOriginX = CGRectGetMaxX(self.centerContainerView.frame);
  1070. CGFloat midPoint = (CGRectGetWidth(self.childControllerContainerView.bounds)-self.maximumRightDrawerWidth) + (self.maximumRightDrawerWidth / 2.0);
  1071. if(xVelocity > self.panVelocityXAnimationThreshold){
  1072. [self closeDrawerAnimated:YES velocity:animationVelocity animationOptions:UIViewAnimationOptionCurveEaseOut completion:completion];
  1073. }
  1074. else if (xVelocity < -self.panVelocityXAnimationThreshold){
  1075. [self openDrawerSide:MMDrawerSideRight animated:YES velocity:animationVelocity animationOptions:UIViewAnimationOptionCurveEaseOut completion:completion];
  1076. }
  1077. else if(currentOriginX > midPoint){
  1078. [self closeDrawerAnimated:YES completion:completion];
  1079. }
  1080. else {
  1081. [self openDrawerSide:MMDrawerSideRight animated:YES completion:completion];
  1082. }
  1083. }
  1084. else {
  1085. if(completion){
  1086. completion(NO);
  1087. }
  1088. }
  1089. }
  1090. -(void)updateDrawerVisualStateForDrawerSide:(MMDrawerSide)drawerSide percentVisible:(CGFloat)percentVisible{
  1091. if(self.drawerVisualState){
  1092. self.drawerVisualState(self,drawerSide,percentVisible);
  1093. }
  1094. else if([self shouldStretchForSide:drawerSide]){
  1095. [self applyOvershootScaleTransformForDrawerSide:drawerSide percentVisible:percentVisible];
  1096. }
  1097. }
  1098. - (BOOL)shouldStretchForSide:(MMDrawerSide)drawerSide {
  1099. switch (drawerSide) {
  1100. case MMDrawerSideLeft:
  1101. return self.shouldStretchLeftDrawer;
  1102. break;
  1103. case MMDrawerSideRight:
  1104. return self.shouldStretchRightDrawer;
  1105. break;
  1106. default:
  1107. return YES;
  1108. break;
  1109. }
  1110. }
  1111. - (void)applyOvershootScaleTransformForDrawerSide:(MMDrawerSide)drawerSide percentVisible:(CGFloat)percentVisible{
  1112. if (percentVisible >= 1.f) {
  1113. CATransform3D transform = CATransform3DIdentity;
  1114. UIViewController * sideDrawerViewController = [self sideDrawerViewControllerForSide:drawerSide];
  1115. if(drawerSide == MMDrawerSideLeft) {
  1116. transform = CATransform3DMakeScale(percentVisible, 1.f, 1.f);
  1117. transform = CATransform3DTranslate(transform, self.maximumLeftDrawerWidth*(percentVisible-1.f)/2, 0.f, 0.f);
  1118. }
  1119. else if(drawerSide == MMDrawerSideRight){
  1120. transform = CATransform3DMakeScale(percentVisible, 1.f, 1.f);
  1121. transform = CATransform3DTranslate(transform, -self.maximumRightDrawerWidth*(percentVisible-1.f)/2, 0.f, 0.f);
  1122. }
  1123. sideDrawerViewController.view.layer.transform = transform;
  1124. }
  1125. }
  1126. -(void)resetDrawerVisualStateForDrawerSide:(MMDrawerSide)drawerSide{
  1127. UIViewController * sideDrawerViewController = [self sideDrawerViewControllerForSide:drawerSide];
  1128. [sideDrawerViewController.view.layer setAnchorPoint:CGPointMake(0.5f, 0.5f)];
  1129. [sideDrawerViewController.view.layer setTransform:CATransform3DIdentity];
  1130. [sideDrawerViewController.view setAlpha:1.0];
  1131. }
  1132. -(CGFloat)roundedOriginXForDrawerConstriants:(CGFloat)originX{
  1133. if (originX < -self.maximumRightDrawerWidth) {
  1134. if (self.shouldStretchRightDrawer &&
  1135. self.rightDrawerViewController) {
  1136. CGFloat maxOvershoot = (CGRectGetWidth(self.centerContainerView.frame)-self.maximumRightDrawerWidth)*MMDrawerOvershootPercentage;
  1137. return originXForDrawerOriginAndTargetOriginOffset(originX, -self.maximumRightDrawerWidth, maxOvershoot);
  1138. }
  1139. else{
  1140. return -self.maximumRightDrawerWidth;
  1141. }
  1142. }
  1143. else if(originX > self.maximumLeftDrawerWidth){
  1144. if (self.shouldStretchLeftDrawer &&
  1145. self.leftDrawerViewController) {
  1146. CGFloat maxOvershoot = (CGRectGetWidth(self.centerContainerView.frame)-self.maximumLeftDrawerWidth)*MMDrawerOvershootPercentage;
  1147. return originXForDrawerOriginAndTargetOriginOffset(originX, self.maximumLeftDrawerWidth, maxOvershoot);
  1148. }
  1149. else{
  1150. return self.maximumLeftDrawerWidth;
  1151. }
  1152. }
  1153. return originX;
  1154. }
  1155. static inline CGFloat originXForDrawerOriginAndTargetOriginOffset(CGFloat originX, CGFloat targetOffset, CGFloat maxOvershoot){
  1156. CGFloat delta = ABS(originX - targetOffset);
  1157. CGFloat maxLinearPercentage = MMDrawerOvershootLinearRangePercentage;
  1158. CGFloat nonLinearRange = maxOvershoot * maxLinearPercentage;
  1159. CGFloat nonLinearScalingDelta = (delta - nonLinearRange);
  1160. CGFloat overshoot = nonLinearRange + nonLinearScalingDelta * nonLinearRange/sqrt(pow(nonLinearScalingDelta,2.f) + 15000);
  1161. if (delta < nonLinearRange) {
  1162. return originX;
  1163. }
  1164. else if (targetOffset < 0) {
  1165. return targetOffset - round(overshoot);
  1166. }
  1167. else{
  1168. return targetOffset + round(overshoot);
  1169. }
  1170. }
  1171. #pragma mark - Helpers
  1172. -(void)setupGestureRecognizers{
  1173. _pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureCallback:)];
  1174. [_pan setDelegate:self];
  1175. [self.view addGestureRecognizer:_pan];
  1176. UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureCallback:)];
  1177. [tap setDelegate:self];
  1178. [self.view addGestureRecognizer:tap];
  1179. }
  1180. -(void)prepareToPresentDrawer:(MMDrawerSide)drawer animated:(BOOL)animated{
  1181. MMDrawerSide drawerToHide = MMDrawerSideNone;
  1182. if(drawer == MMDrawerSideLeft){
  1183. drawerToHide = MMDrawerSideRight;
  1184. }
  1185. else if(drawer == MMDrawerSideRight){
  1186. drawerToHide = MMDrawerSideLeft;
  1187. }
  1188. UIViewController * sideDrawerViewControllerToPresent = [self sideDrawerViewControllerForSide:drawer];
  1189. UIViewController * sideDrawerViewControllerToHide = [self sideDrawerViewControllerForSide:drawerToHide];
  1190. [self.childControllerContainerView sendSubviewToBack:sideDrawerViewControllerToHide.view];
  1191. [sideDrawerViewControllerToHide.view setHidden:YES];
  1192. [sideDrawerViewControllerToPresent.view setHidden:NO];
  1193. [self resetDrawerVisualStateForDrawerSide:drawer];
  1194. [sideDrawerViewControllerToPresent.view setFrame:sideDrawerViewControllerToPresent.mm_visibleDrawerFrame];
  1195. [self updateDrawerVisualStateForDrawerSide:drawer percentVisible:0.0];
  1196. [sideDrawerViewControllerToPresent beginAppearanceTransition:YES animated:animated];
  1197. }
  1198. -(void)updateShadowForCenterView{
  1199. UIView * centerView = self.centerContainerView;
  1200. if(self.showsShadow){
  1201. centerView.layer.masksToBounds = NO;
  1202. centerView.layer.shadowRadius = self.shadowRadius;
  1203. centerView.layer.shadowOpacity = self.shadowOpacity;
  1204. centerView.layer.shadowOffset = self.shadowOffset;
  1205. centerView.layer.shadowColor = [self.shadowColor CGColor];
  1206. /** In the event this gets called a lot, we won't update the shadowPath
  1207. unless it needs to be updated (like during rotation) */
  1208. if (centerView.layer.shadowPath == NULL) {
  1209. centerView.layer.shadowPath = [[UIBezierPath bezierPathWithRect:self.centerContainerView.bounds] CGPath];
  1210. }
  1211. else{
  1212. CGRect currentPath = CGPathGetPathBoundingBox(centerView.layer.shadowPath);
  1213. if (CGRectEqualToRect(currentPath, centerView.bounds) == NO){
  1214. centerView.layer.shadowPath = [[UIBezierPath bezierPathWithRect:self.centerContainerView.bounds] CGPath];
  1215. }
  1216. }
  1217. }
  1218. else if (centerView.layer.shadowPath != NULL) {
  1219. centerView.layer.shadowRadius = 0.f;
  1220. centerView.layer.shadowOpacity = 0.f;
  1221. centerView.layer.shadowOffset = CGSizeMake(0, -3);
  1222. centerView.layer.shadowPath = NULL;
  1223. centerView.layer.masksToBounds = YES;
  1224. }
  1225. }
  1226. -(NSTimeInterval)animationDurationForAnimationDistance:(CGFloat)distance{
  1227. CGFloat velocity = self.openSide == MMDrawerSideLeft ? self.animationVelocityLeft : self.animationVelocityRight;
  1228. NSTimeInterval duration = MAX(distance/velocity,MMDrawerMinimumAnimationDuration);
  1229. return duration;
  1230. }
  1231. -(UIViewController*)sideDrawerViewControllerForSide:(MMDrawerSide)drawerSide{
  1232. UIViewController * sideDrawerViewController = nil;
  1233. if(drawerSide != MMDrawerSideNone){
  1234. sideDrawerViewController = [self childViewControllerForSide:drawerSide];
  1235. }
  1236. return sideDrawerViewController;
  1237. }
  1238. -(UIViewController*)childViewControllerForSide:(MMDrawerSide)drawerSide{
  1239. UIViewController * childViewController = nil;
  1240. switch (drawerSide) {
  1241. case MMDrawerSideLeft:
  1242. childViewController = self.leftDrawerViewController;
  1243. break;
  1244. case MMDrawerSideRight:
  1245. childViewController = self.rightDrawerViewController;
  1246. break;
  1247. case MMDrawerSideNone:
  1248. childViewController = self.centerViewController;
  1249. break;
  1250. }
  1251. return childViewController;
  1252. }
  1253. #pragma mark - UIGestureRecognizerDelegate
  1254. -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
  1255. if(self.openSide == MMDrawerSideNone){
  1256. MMOpenDrawerGestureMode possibleOpenGestureModes = [self possibleOpenGestureModesForGestureRecognizer:gestureRecognizer
  1257. withTouch:touch];
  1258. return ((self.openDrawerGestureModeMask & possibleOpenGestureModes)>0);
  1259. }
  1260. else{
  1261. MMCloseDrawerGestureMode possibleCloseGestureModes = [self possibleCloseGestureModesForGestureRecognizer:gestureRecognizer
  1262. withTouch:touch];
  1263. return ((self.closeDrawerGestureModeMask & possibleCloseGestureModes)>0);
  1264. }
  1265. }
  1266. #pragma mark Gesture Recogizner Delegate Helpers
  1267. -(MMCloseDrawerGestureMode)possibleCloseGestureModesForGestureRecognizer:(UIGestureRecognizer*)gestureRecognizer withTouch:(UITouch*)touch{
  1268. CGPoint point = [touch locationInView:self.childControllerContainerView];
  1269. MMCloseDrawerGestureMode possibleCloseGestureModes = MMCloseDrawerGestureModeNone;
  1270. if([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]){
  1271. if([self isPointContainedWithinNavigationRect:point]){
  1272. possibleCloseGestureModes |= MMCloseDrawerGestureModeTapNavigationBar;
  1273. }
  1274. if([self isPointContainedWithinCenterViewContentRect:point]){
  1275. possibleCloseGestureModes |= MMCloseDrawerGestureModeTapCenterView;
  1276. }
  1277. }
  1278. else if([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]){
  1279. if([self isPointContainedWithinNavigationRect:point]){
  1280. possibleCloseGestureModes |= MMCloseDrawerGestureModePanningNavigationBar;
  1281. }
  1282. if([self isPointContainedWithinCenterViewContentRect:point]){
  1283. possibleCloseGestureModes |= MMCloseDrawerGestureModePanningCenterView;
  1284. }
  1285. if([self isPointContainedWithinRightBezelRect:point] &&
  1286. self.openSide == MMDrawerSideLeft){
  1287. possibleCloseGestureModes |= MMCloseDrawerGestureModeBezelPanningCenterView;
  1288. }
  1289. if([self isPointContainedWithinLeftBezelRect:point] &&
  1290. self.openSide == MMDrawerSideRight){
  1291. possibleCloseGestureModes |= MMCloseDrawerGestureModeBezelPanningCenterView;
  1292. }
  1293. if([self isPointContainedWithinCenterViewContentRect:point] == NO &&
  1294. [self isPointContainedWithinNavigationRect:point] == NO){
  1295. possibleCloseGestureModes |= MMCloseDrawerGestureModePanningDrawerView;
  1296. }
  1297. }
  1298. if((self.closeDrawerGestureModeMask & MMCloseDrawerGestureModeCustom) > 0 &&
  1299. self.gestureShouldRecognizeTouch){
  1300. if(self.gestureShouldRecognizeTouch(self,gestureRecognizer,touch)){
  1301. possibleCloseGestureModes |= MMCloseDrawerGestureModeCustom;
  1302. }
  1303. }
  1304. return possibleCloseGestureModes;
  1305. }
  1306. -(MMOpenDrawerGestureMode)possibleOpenGestureModesForGestureRecognizer:(UIGestureRecognizer*)gestureRecognizer withTouch:(UITouch*)touch{
  1307. CGPoint point = [touch locationInView:self.childControllerContainerView];
  1308. MMOpenDrawerGestureMode possibleOpenGestureModes = MMOpenDrawerGestureModeNone;
  1309. if([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]){
  1310. if([self isPointContainedWithinNavigationRect:point]){
  1311. possibleOpenGestureModes |= MMOpenDrawerGestureModePanningNavigationBar;
  1312. }
  1313. if([self isPointContainedWithinCenterViewContentRect:point]){
  1314. possibleOpenGestureModes |= MMOpenDrawerGestureModePanningCenterView;
  1315. }
  1316. if([self isPointContainedWithinLeftBezelRect:point] &&
  1317. self.leftDrawerViewController){
  1318. possibleOpenGestureModes |= MMOpenDrawerGestureModeBezelPanningCenterView;
  1319. }
  1320. if([self isPointContainedWithinRightBezelRect:point] &&
  1321. self.rightDrawerViewController){
  1322. possibleOpenGestureModes |= MMOpenDrawerGestureModeBezelPanningCenterView;
  1323. }
  1324. }
  1325. if((self.openDrawerGestureModeMask & MMOpenDrawerGestureModeCustom) > 0 &&
  1326. self.gestureShouldRecognizeTouch){
  1327. if(self.gestureShouldRecognizeTouch(self,gestureRecognizer,touch)){
  1328. possibleOpenGestureModes |= MMOpenDrawerGestureModeCustom;
  1329. }
  1330. }
  1331. return possibleOpenGestureModes;
  1332. }
  1333. -(BOOL)isPointContainedWithinNavigationRect:(CGPoint)point{
  1334. CGRect navigationBarRect = CGRectNull;
  1335. if([self.centerViewController isKindOfClass:[UINavigationController class]]){
  1336. UINavigationBar * navBar = [(UINavigationController*)self.centerViewController navigationBar];
  1337. navigationBarRect = [navBar convertRect:navBar.bounds toView:self.childControllerContainerView];
  1338. navigationBarRect = CGRectIntersection(navigationBarRect,self.childControllerContainerView.bounds);
  1339. }
  1340. return CGRectContainsPoint(navigationBarRect,point);
  1341. }
  1342. -(BOOL)isPointContainedWithinCenterViewContentRect:(CGPoint)point{
  1343. CGRect centerViewContentRect = self.centerContainerView.frame;
  1344. centerViewContentRect = CGRectIntersection(centerViewContentRect,self.childControllerContainerView.bounds);
  1345. return (CGRectContainsPoint(centerViewContentRect, point) &&
  1346. [self isPointContainedWithinNavigationRect:point] == NO);
  1347. }
  1348. -(BOOL)isPointContainedWithinLeftBezelRect:(CGPoint)point{
  1349. CGRect leftBezelRect = CGRectNull;
  1350. CGRect tempRect;
  1351. CGRectDivide(self.childControllerContainerView.bounds, &leftBezelRect, &tempRect, self.bezelPanningCenterViewRange, CGRectMinXEdge);
  1352. return (CGRectContainsPoint(leftBezelRect, point) &&
  1353. [self isPointContainedWithinCenterViewContentRect:point]);
  1354. }
  1355. -(BOOL)isPointContainedWithinRightBezelRect:(CGPoint)point{
  1356. CGRect rightBezelRect = CGRectNull;
  1357. CGRect tempRect;
  1358. CGRectDivide(self.childControllerContainerView.bounds, &rightBezelRect, &tempRect, self.bezelPanningCenterViewRange, CGRectMaxXEdge);
  1359. return (CGRectContainsPoint(rightBezelRect, point) &&
  1360. [self isPointContainedWithinCenterViewContentRect:point]);
  1361. }
  1362. @end