From 4167885b839f4fecb32b263a2b166521c7f02859 Mon Sep 17 00:00:00 2001 From: hanchao Date: Fri, 18 Jul 2014 11:32:22 +0800 Subject: [PATCH 1/4] support orientation changes --- HMSideMenu/HMSideMenu.h | 3 ++ HMSideMenu/HMSideMenu.m | 37 +++++++++++++------ .../HMSideMenuExample/ViewController.m | 5 +++ 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/HMSideMenu/HMSideMenu.h b/HMSideMenu/HMSideMenu.h index ee79780..6c42c6d 100644 --- a/HMSideMenu/HMSideMenu.h +++ b/HMSideMenu/HMSideMenu.h @@ -54,6 +54,9 @@ typedef enum { */ - (void)close; + +- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation; + @end ///-------------------------------- diff --git a/HMSideMenu/HMSideMenu.m b/HMSideMenu/HMSideMenu.m index 5676162..f82978b 100644 --- a/HMSideMenu/HMSideMenu.m +++ b/HMSideMenu/HMSideMenu.m @@ -166,24 +166,39 @@ - (void)layoutSubviews { CGFloat y = 0; CGFloat itemInitialX = 0; + CGFloat superViewWidth = 0; + CGFloat superViewHeight = 0; + + UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation]; + if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation ==UIInterfaceOrientationPortraitUpsideDown) { + superViewWidth = self.superview.frame.size.width; + superViewHeight = self.superview.frame.size.height; + }else if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft || interfaceOrientation ==UIInterfaceOrientationLandscapeRight) { + superViewWidth = self.superview.frame.size.height; + superViewHeight = self.superview.frame.size.width; + } + + if (self.menuIsVertical) { - x = self.menuPosition == HMSideMenuPositionRight ? self.superview.frame.size.width : 0 - self.menuWidth; - y = (self.superview.frame.size.height / 2) - (self.menuHeight / 2); + x = self.menuPosition == HMSideMenuPositionRight ? superViewWidth : 0 - self.menuWidth; + y = (superViewHeight / 2) - (self.menuHeight / 2); itemInitialX = self.menuWidth / 2; } else { - x = self.superview.frame.size.width / 2 - (self.menuWidth / 2); - y = self.menuPosition == HMSideMenuPositionTop ? 0 - self.menuHeight : self.superview.frame.size.height; + x = superViewWidth / 2 - (self.menuWidth / 2); + y = self.menuPosition == HMSideMenuPositionTop ? 0 - self.menuHeight : superViewHeight; } self.frame = CGRectMake(x, y, self.menuWidth, self.menuHeight);; - // Layout the items - [self.items enumerateObjectsUsingBlock:^(UIView *item, NSUInteger idx, BOOL *stop) { - if (self.menuIsVertical) - [item setCenter:CGPointMake(itemInitialX, (idx * biggestHeight) + (idx * self.itemSpacing) + (biggestHeight / 2))]; - else - [item setCenter:CGPointMake((idx * biggestWidth) + (idx * self.itemSpacing) + (biggestWidth / 2), self.menuHeight / 2)]; - }]; + if (!_isOpen) { + // Layout the items + [self.items enumerateObjectsUsingBlock:^(UIView *item, NSUInteger idx, BOOL *stop) { + if (self.menuIsVertical) + [item setCenter:CGPointMake(itemInitialX, (idx * biggestHeight) + (idx * self.itemSpacing) + (biggestHeight / 2))]; + else + [item setCenter:CGPointMake((idx * biggestWidth) + (idx * self.itemSpacing) + (biggestWidth / 2), self.menuHeight / 2)]; + }]; + } } #pragma mark - Animation diff --git a/HMSideMenuExample/HMSideMenuExample/ViewController.m b/HMSideMenuExample/HMSideMenuExample/ViewController.m index c8bbdf0..17a81bd 100644 --- a/HMSideMenuExample/HMSideMenuExample/ViewController.m +++ b/HMSideMenuExample/HMSideMenuExample/ViewController.m @@ -69,4 +69,9 @@ - (IBAction)toggleMenu:(id)sender { [self.sideMenu open]; } +- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation +{ + [self.sideMenu layoutSubviews]; +} + @end From b026ed5b3abfcd279b04373d957d93aa68637b8c Mon Sep 17 00:00:00 2001 From: hanchao Date: Fri, 18 Jul 2014 11:39:08 +0800 Subject: [PATCH 2/4] remove didRotateFromInterfaceOrientation --- HMSideMenu/HMSideMenu.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/HMSideMenu/HMSideMenu.h b/HMSideMenu/HMSideMenu.h index 6c42c6d..ee79780 100644 --- a/HMSideMenu/HMSideMenu.h +++ b/HMSideMenu/HMSideMenu.h @@ -54,9 +54,6 @@ typedef enum { */ - (void)close; - -- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation; - @end ///-------------------------------- From 73c72689b96976b466435c0a889d517de79fe734 Mon Sep 17 00:00:00 2001 From: hanchao Date: Fri, 18 Jul 2014 12:51:04 +0800 Subject: [PATCH 3/4] just layout items once --- HMSideMenu/HMSideMenu.m | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/HMSideMenu/HMSideMenu.m b/HMSideMenu/HMSideMenu.m index f82978b..7bcc9fc 100644 --- a/HMSideMenu/HMSideMenu.m +++ b/HMSideMenu/HMSideMenu.m @@ -26,6 +26,8 @@ @interface HMSideMenu () @property (nonatomic, assign) CGFloat menuWidth; @property (nonatomic, assign) CGFloat menuHeight; +@property (nonatomic, assign) BOOL isLayoutItems; + - (BOOL)menuIsVertical; @end @@ -39,6 +41,7 @@ - (id)initWithItems:(NSArray *)items { self.items = items; _animationDuration = 1.3f; _menuPosition = HMSideMenuPositionRight; + _isLayoutItems = FALSE; } return self; @@ -190,7 +193,7 @@ - (void)layoutSubviews { self.frame = CGRectMake(x, y, self.menuWidth, self.menuHeight);; - if (!_isOpen) { + if (!_isLayoutItems) { // Layout the items [self.items enumerateObjectsUsingBlock:^(UIView *item, NSUInteger idx, BOOL *stop) { if (self.menuIsVertical) @@ -198,6 +201,7 @@ - (void)layoutSubviews { else [item setCenter:CGPointMake((idx * biggestWidth) + (idx * self.itemSpacing) + (biggestWidth / 2), self.menuHeight / 2)]; }]; + _isLayoutItems = TRUE; } } From 769b4c5006eedce0c22f61057ae02c9f54fc84f4 Mon Sep 17 00:00:00 2001 From: hanchao Date: Sun, 20 Jul 2014 11:36:35 +0800 Subject: [PATCH 4/4] layoutSubviews in willAnimateRotationToInterfaceOrientation --- HMSideMenuExample/HMSideMenuExample/ViewController.m | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/HMSideMenuExample/HMSideMenuExample/ViewController.m b/HMSideMenuExample/HMSideMenuExample/ViewController.m index 17a81bd..f7617a1 100644 --- a/HMSideMenuExample/HMSideMenuExample/ViewController.m +++ b/HMSideMenuExample/HMSideMenuExample/ViewController.m @@ -69,8 +69,7 @@ - (IBAction)toggleMenu:(id)sender { [self.sideMenu open]; } -- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation -{ +- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { [self.sideMenu layoutSubviews]; }