react-native-navigation的迁移库

MMDrawerController.m 65KB

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