From f32edb7041648d4d21cd45eae0f68e7eaf770ce0 Mon Sep 17 00:00:00 2001 From: Aryan Ghassemi Date: Sun, 5 Jan 2014 15:18:41 -0800 Subject: [PATCH] - Code cleanup - Simplified logic by avoiding orientation check in multiple placers - Fix ome animation glitches --- SlideMenu/AppDelegate.m | 6 +-- SlideMenu/Source/SlideNavigationController.m | 51 +++++++++----------- 2 files changed, 27 insertions(+), 30 deletions(-) diff --git a/SlideMenu/AppDelegate.m b/SlideMenu/AppDelegate.m index 90d6a86..be29f9a 100644 --- a/SlideMenu/AppDelegate.m +++ b/SlideMenu/AppDelegate.m @@ -17,15 +17,15 @@ MenuViewController *rightMenu = (MenuViewController*)[mainStoryboard instantiateViewControllerWithIdentifier: @"MenuViewController"]; - rightMenu.view.backgroundColor = [UIColor yellowColor]; + rightMenu.view.backgroundColor = [UIColor whiteColor]; rightMenu.cellIdentifier = @"rightMenuCell"; MenuViewController *leftMenu = (MenuViewController*)[mainStoryboard instantiateViewControllerWithIdentifier: @"MenuViewController"]; - leftMenu.view.backgroundColor = [UIColor lightGrayColor]; + leftMenu.view.backgroundColor = [UIColor whiteColor]; leftMenu.cellIdentifier = @"leftMenuCell"; - [SlideNavigationController sharedInstance].righMenu = rightMenu; + [SlideNavigationController sharedInstance].rightMenu = rightMenu; [SlideNavigationController sharedInstance].leftMenu = leftMenu; // Override point for customization after application launch. diff --git a/SlideMenu/Source/SlideNavigationController.m b/SlideMenu/Source/SlideNavigationController.m index 6194a83..9cbec28 100644 --- a/SlideMenu/Source/SlideNavigationController.m +++ b/SlideMenu/Source/SlideNavigationController.m @@ -41,6 +41,7 @@ #define MENU_SHADOW_RADIUS 10 #define MENU_SHADOW_OPACITY 1 #define MENU_DEFAULT_SLIDE_OFFSET 60 +#define MENU_FAST_VELOCITY_FOR_SWIPE_FOLLOW_DIRECTION 1000 static SlideNavigationController *singletonInstance; @@ -112,12 +113,6 @@ static SlideNavigationController *singletonInstance; self.rightMenu.view.frame = rect; } -- (void)viewDidLayoutSubviews -{ - [super viewDidLayoutSubviews]; - -} - - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration]; @@ -142,16 +137,15 @@ static SlideNavigationController *singletonInstance; return; } - __block CGRect rect = self.view.frame; - if ([self isMenuOpen]) { [UIView animateWithDuration:MENU_SLIDE_ANIMATION_DURATION delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ - [self moveHorizontallyToLocation:(rect.origin.x > 0) ? rect.size.width : -1*rect.size.width]; - self.view.frame = rect; + CGFloat width = self.horizontalSize; + CGFloat moveLocation = (self.horizontalLocation> 0) ? width : -1*width; + [self moveHorizontallyToLocation:moveLocation]; } completion:^(BOOL finished) { [super popToRootViewControllerAnimated:NO]; @@ -241,14 +235,7 @@ static SlideNavigationController *singletonInstance; - (BOOL)isMenuOpen { - if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) - { - return (self.view.frame.origin.y == 0) ? NO : YES; - } - else - { - return (self.view.frame.origin.x == 0) ? NO : YES; - } + return (self.horizontalLocation == 0) ? NO : YES; } - (BOOL)shouldDisplayMenu:(Menu)menu forViewController:(UIViewController *)vc @@ -293,10 +280,9 @@ static SlideNavigationController *singletonInstance; options:UIViewAnimationOptionCurveEaseOut animations:^{ CGRect rect = self.view.frame; - CGFloat width = (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) ? rect.size.height : rect.size.width; + CGFloat width = self.horizontalSize; rect.origin.x = (menu == MenuLeft) ? (width - self.slideOffset) : ((width - self.slideOffset )* -1); [self moveHorizontallyToLocation:rect.origin.x]; - //self.view.frame = rect; } completion:^(BOOL finished) { if (completion) @@ -320,7 +306,6 @@ static SlideNavigationController *singletonInstance; CGRect rect = self.view.frame; rect.origin.x = 0; [self moveHorizontallyToLocation:rect.origin.x]; - //self.view.frame = rect; } completion:^(BOOL finished) { if (completion) @@ -371,6 +356,21 @@ static SlideNavigationController *singletonInstance; } } +- (CGFloat)horizontalSize +{ + CGRect rect = self.view.frame; + UIInterfaceOrientation orientation = self.interfaceOrientation; + + if (UIInterfaceOrientationIsLandscape(orientation)) + { + return rect.size.height; + } + else + { + return rect.size.width; + } +} + #pragma mark - UINavigationControllerDelegate Methods - - (void)navigationController:(UINavigationController *)navigationController @@ -419,8 +419,6 @@ static SlideNavigationController *singletonInstance; - (void)panDetected:(UIPanGestureRecognizer *)aPanRecognizer { - static NSInteger velocityForFollowingDirection = 1000; - CGPoint translation = [aPanRecognizer translationInView:aPanRecognizer.view]; CGPoint velocity = [aPanRecognizer velocityInView:aPanRecognizer.view]; @@ -436,7 +434,6 @@ static SlideNavigationController *singletonInstance; if (newHorizontalLocation >= self.minXForDragging && newHorizontalLocation <= self.maxXForDragging) [self moveHorizontallyToLocation:newHorizontalLocation]; - //self.view.frame = rect; self.draggingPoint = translation; @@ -458,7 +455,7 @@ static SlideNavigationController *singletonInstance; NSInteger positiveVelocity = (velocity.x > 0) ? velocity.x : velocity.x * -1; // If the speed is high enough follow direction - if (positiveVelocity >= velocityForFollowingDirection) + if (positiveVelocity >= MENU_FAST_VELOCITY_FOR_SWIPE_FOLLOW_DIRECTION) { // Moving Right if (velocity.x > 0) @@ -502,7 +499,7 @@ static SlideNavigationController *singletonInstance; { if ([self shouldDisplayMenu:MenuRight forViewController:self.topViewController]) { - return (self.view.frame.size.width - self.slideOffset) * -1; + return (self.horizontalSize - self.slideOffset) * -1; } return 0; @@ -512,7 +509,7 @@ static SlideNavigationController *singletonInstance; { if ([self shouldDisplayMenu:MenuLeft forViewController:self.topViewController]) { - return self.view.frame.size.width - self.slideOffset; + return self.horizontalSize - self.slideOffset; } return 0;