Skip to content

Commit 2a29ea3

Browse files
author
Francesco
committed
- Added device orientation observer
- Added orientation changed method to handle menu layout - Added isCircularMenu or isGridMenu properties - Separated in different methods the layout set up for circular and grid menu
1 parent a62783f commit 2a29ea3

File tree

2 files changed

+107
-55
lines changed

2 files changed

+107
-55
lines changed

IGCMenu/IGCMenu.m

Lines changed: 105 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ @implementation IGCMenu{
1818
NSMutableArray *menuButtonArray; //array of menu buttons
1919
NSMutableArray *menuNameLabelArray; //array of menu name label
2020
UIView *pMenuButtonSuperView;
21+
22+
int maxRow;
23+
__block CGFloat topMenuCenterY;
24+
CGFloat eachMenuWidth;
25+
CGFloat eachMenuVerticalSpace;
26+
BOOL isCircularMenu;
27+
BOOL isGridMenu;
2128
}
2229

2330
- (instancetype)init{
@@ -31,6 +38,13 @@ - (instancetype)init{
3138
self.menuRadius = 120;
3239
self.maxColumn = 3;
3340
self.backgroundType = BlurEffectDark;
41+
42+
//observe orientation changes
43+
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
44+
[[NSNotificationCenter defaultCenter]
45+
addObserver:self selector:@selector(orientationChanged:)
46+
name:UIDeviceOrientationDidChangeNotification
47+
object:[UIDevice currentDevice]];
3448
}
3549
return self;
3650
}
@@ -93,15 +107,15 @@ - (void)createMenuButtons{
93107
}
94108

95109
- (void)menuSuperViewBackground{
96-
if (pMenuButtonSuperView == nil) {
97-
pMenuButtonSuperView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
98-
pMenuButtonSuperView.tag = MENU_BACKGROUND_VIEW_TAG;
99-
}
100-
if (!self.menuSuperView) {
110+
if (pMenuButtonSuperView == nil) {
111+
pMenuButtonSuperView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
112+
pMenuButtonSuperView.tag = MENU_BACKGROUND_VIEW_TAG;
113+
}
114+
if (!self.menuSuperView) {
101115
self.menuSuperView = [self.menuButton superview];
102-
}
103-
[self.menuSuperView bringSubviewToFront:self.menuButton];
104-
[self.menuSuperView insertSubview:pMenuButtonSuperView belowSubview:self.menuButton];
116+
}
117+
[self.menuSuperView bringSubviewToFront:self.menuButton];
118+
[self.menuSuperView insertSubview:pMenuButtonSuperView belowSubview:self.menuButton];
105119

106120
if (self.disableBackground){
107121
pMenuButtonSuperView.userInteractionEnabled = YES;
@@ -150,6 +164,7 @@ -(void)setBlurredView:(UIBlurEffectStyle) blurEffectStyle{
150164
}
151165

152166
- (void)showCircularMenu{
167+
isCircularMenu = true;
153168

154169
[self menuSuperViewBackground];
155170

@@ -159,23 +174,28 @@ - (void)showCircularMenu{
159174
//menuButton.center = CGPointMake(homeButtonCenter.x - radius * cos(angle * i), homeButtonCenter.y - radius * sin(angle * i));
160175

161176
for (int i = 1; i < menuButtonArray.count * 2; i=i+2) {
162-
163-
CGFloat angle = M_PI / (menuButtonArray.count * 2);
164177
[UIView animateWithDuration:ANIMATION_DURATION delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
165178
pMenuButtonSuperView.layer.opacity = 1.0;
166-
UIButton * menuButton = (UIButton *)[menuButtonArray objectAtIndex:i/2];
167-
menuButton.layer.opacity = 1.0;
168-
menuButton.center = CGPointMake(self.menuButton.center.x - self.menuRadius * cos(angle * i), self.menuButton.center.y - self.menuRadius * sin(angle * i));
169-
if (menuNameLabelArray.count > (i/2)) {
170-
UILabel *menuNameLabel = (UILabel *)[menuNameLabelArray objectAtIndex:i/2];
171-
menuNameLabel.layer.opacity = 1.0;
172-
menuNameLabel.center = CGPointMake(menuButton.center.x, menuButton.frame.origin.y + menuButton.frame.size.height + (menuNameLabel.frame.size.height / 2) + 5);
173-
}
179+
[self updateCircularMenuLayoutAtIndex:i];
174180
}completion:nil];
175181
}
176182
}
177183

184+
- (void)updateCircularMenuLayoutAtIndex:(int)i{
185+
UIButton * menuButton = (UIButton *)[menuButtonArray objectAtIndex:i/2];
186+
menuButton.layer.opacity = 1.0;
187+
CGFloat angle = M_PI / (menuButtonArray.count * 2);
188+
menuButton.center = CGPointMake(self.menuButton.center.x - self.menuRadius * cos(angle * i), self.menuButton.center.y - self.menuRadius * sin(angle * i));
189+
if (menuNameLabelArray.count > (i/2)) {
190+
UILabel *menuNameLabel = (UILabel *)[menuNameLabelArray objectAtIndex:i/2];
191+
menuNameLabel.layer.opacity = 1.0;
192+
menuNameLabel.center = CGPointMake(menuButton.center.x, menuButton.frame.origin.y + menuButton.frame.size.height + (menuNameLabel.frame.size.height / 2) + 5);
193+
}
194+
}
195+
178196
- (void)hideCircularMenu{
197+
isCircularMenu = false;
198+
179199
[UIView animateWithDuration:ANIMATION_DURATION delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
180200
for (int i = 0; i < menuButtonArray.count; i++) {
181201
UIButton *menuButton = (UIButton *)[menuButtonArray objectAtIndex:i];
@@ -205,15 +225,24 @@ - (void)hideCircularMenu{
205225
}
206226

207227
-(void)showGridMenu{
228+
isGridMenu = true;
208229
[self menuSuperViewBackground];
209230
if (menuButtonArray.count <= 0) {
210231
[self createMenuButtons];
211232
}
212233

213-
int maxRow = ceilf(menuButtonArray.count /(float)self.maxColumn);
214-
__block CGFloat topMenuCenterY = self.menuButton.frame.origin.y - 10;
215-
CGFloat eachMenuVerticalSpace = 0;
216-
CGFloat eachMenuWidth = 0;
234+
[self setMenuButtonLayout];
235+
[UIView animateWithDuration:ANIMATION_DURATION delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
236+
pMenuButtonSuperView.layer.opacity = 1.0;
237+
[self updateGridMenuLayout];
238+
}completion:nil];
239+
}
240+
241+
-(void)setMenuButtonLayout{
242+
maxRow = ceilf(menuButtonArray.count /(float)self.maxColumn);
243+
topMenuCenterY = self.menuButton.frame.origin.y - 10;
244+
eachMenuVerticalSpace = 0;
245+
eachMenuWidth = 0;
217246
if (menuButtonArray.count) {
218247
UIButton *menuButton = (UIButton *)menuButtonArray[0];
219248
eachMenuVerticalSpace = menuButton.frame.size.height + 20;
@@ -228,48 +257,47 @@ -(void)showGridMenu{
228257
eachMenuVerticalSpace = 100.0;
229258
topMenuCenterY = topMenuCenterY - (eachMenuVerticalSpace * maxRow) + eachMenuVerticalSpace/3;
230259
}
260+
}
261+
262+
- (void)updateGridMenuLayout{
231263

232-
__block CGFloat distanceBetweenMenu = ((pMenuButtonSuperView.frame.size.width - (self.maxColumn*eachMenuWidth))/(self.maxColumn +1));
233-
234-
[UIView animateWithDuration:ANIMATION_DURATION delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
235-
pMenuButtonSuperView.layer.opacity = 1.0;
264+
__block CGFloat distanceBetweenMenu = ((pMenuButtonSuperView.frame.size.width - (self.maxColumn*eachMenuWidth))/(self.maxColumn +1));
265+
int menuIndex = 0;
266+
//for each row
267+
for(int i = 1; i <= maxRow; i++,topMenuCenterY += eachMenuVerticalSpace) {
236268

237-
int menuIndex = 0;
238-
//for each row
239-
for(int i = 1; i <= maxRow; i++,topMenuCenterY += eachMenuVerticalSpace) {
240-
241-
int remainingMenuButton = self.maxColumn;
242-
//CGFloat menuCenterX = distanceBetweenMenu;
269+
int remainingMenuButton = self.maxColumn;
270+
//CGFloat menuCenterX = distanceBetweenMenu;
271+
272+
CGFloat menuCenterX;
273+
//for each column
274+
for (int j = 1; j <= remainingMenuButton; j++) {
275+
UIButton *menuButton = (UIButton *)[menuButtonArray objectAtIndex:menuIndex];
276+
menuButton.layer.opacity = 1.0;
243277

244-
CGFloat menuCenterX;
245-
//for each column
246-
for (int j = 1; j <= remainingMenuButton; j++) {
247-
UIButton *menuButton = (UIButton *)[menuButtonArray objectAtIndex:menuIndex];
248-
menuButton.layer.opacity = 1.0;
249-
250-
menuCenterX = (distanceBetweenMenu *j) + (2*j - 1)*(menuButton.frame.size.width/2);
251-
if (i == maxRow) {
252-
remainingMenuButton = menuButtonArray.count % self.maxColumn;
253-
if (remainingMenuButton == 0) {
254-
remainingMenuButton = self.maxColumn;
255-
}
256-
menuCenterX = menuCenterX + ((self.maxColumn - remainingMenuButton)*(distanceBetweenMenu/2)) + (self.maxColumn - remainingMenuButton)*menuButton.frame.size.width/2;
257-
}
258-
menuButton.center = CGPointMake(menuCenterX, topMenuCenterY);
259-
260-
if (menuNameLabelArray.count > menuIndex) {
261-
UILabel *menuNameLabel = (UILabel *)[menuNameLabelArray objectAtIndex:menuIndex];
262-
menuNameLabel.layer.opacity = 1.0;
263-
menuNameLabel.center = CGPointMake(menuButton.center.x, menuButton.frame.origin.y + menuButton.frame.size.height + (menuNameLabel.frame.size.height / 2) + 5);
278+
menuCenterX = (distanceBetweenMenu *j) + (2*j - 1)*(menuButton.frame.size.width/2);
279+
if (i == maxRow) {
280+
remainingMenuButton = menuButtonArray.count % self.maxColumn;
281+
if (remainingMenuButton == 0) {
282+
remainingMenuButton = self.maxColumn;
264283
}
265-
266-
menuIndex++;
284+
menuCenterX = menuCenterX + ((self.maxColumn - remainingMenuButton)*(distanceBetweenMenu/2)) + (self.maxColumn - remainingMenuButton)*menuButton.frame.size.width/2;
267285
}
286+
menuButton.center = CGPointMake(menuCenterX, topMenuCenterY);
287+
288+
if (menuNameLabelArray.count > menuIndex) {
289+
UILabel *menuNameLabel = (UILabel *)[menuNameLabelArray objectAtIndex:menuIndex];
290+
menuNameLabel.layer.opacity = 1.0;
291+
menuNameLabel.center = CGPointMake(menuButton.center.x, menuButton.frame.origin.y + menuButton.frame.size.height + (menuNameLabel.frame.size.height / 2) + 5);
292+
}
293+
294+
menuIndex++;
268295
}
269-
}completion:nil];
296+
}
270297
}
271298

272299
-(void)hideGridMenu{
300+
isGridMenu = false;
273301
[self hideCircularMenu];
274302
}
275303

@@ -293,5 +321,27 @@ - (void)menuButtonClicked:(UIButton *)sender{
293321
}
294322
}
295323

324+
//MARK: Orientation changes
325+
- (void)orientationChanged:(NSNotification *)note{
326+
UIDevice * device = note.object;
327+
switch(device.orientation){
328+
case UIDeviceOrientationPortrait:
329+
case UIDeviceOrientationLandscapeLeft:
330+
case UIDeviceOrientationLandscapeRight:
331+
[pMenuButtonSuperView setFrame: [UIScreen mainScreen].bounds];
332+
/* update menu animation */
333+
if (isCircularMenu){
334+
for (int i = 1; i < menuButtonArray.count * 2; i=i+2) {
335+
[self updateCircularMenuLayoutAtIndex:i];
336+
}
337+
} else if (isGridMenu){
338+
[self setMenuButtonLayout];
339+
[self updateGridMenuLayout];
340+
}
341+
break;
342+
default:
343+
break;
344+
};
345+
}
296346

297347
@end

IGCMenuExample/Info.plist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
<key>UISupportedInterfaceOrientations</key>
3434
<array>
3535
<string>UIInterfaceOrientationPortrait</string>
36+
<string>UIInterfaceOrientationLandscapeLeft</string>
37+
<string>UIInterfaceOrientationLandscapeRight</string>
3638
</array>
3739
<key>UISupportedInterfaceOrientations~ipad</key>
3840
<array>

0 commit comments

Comments
 (0)