Skip to content

Commit

Permalink
Fix orientation handling and categories displaying - #13
Browse files Browse the repository at this point in the history
  • Loading branch information
akervern committed Nov 8, 2011
1 parent bb8bcd3 commit 4b9f6a4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 16 deletions.
4 changes: 2 additions & 2 deletions iOS/sosmessage/sosmessage/NSString+SOSMessage.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ -(float)sizeForBlocksForView:(UIView*)view {

-(float)blocksCount:(UIView*)view {
float widthWithFont = [self sizeWithFont:SOSFONT].width;
NSLog(@"Width with font for %@ : %.2f", self, widthWithFont);
//NSLog(@"Width with font for %@ : %.2f", self, widthWithFont);
float blockSize = view.bounds.size.width / NB_BLOCKS;
NSLog(@"Frame width: %.2f and a block: %.2f", view.frame.size.width, blockSize);
//NSLog(@"Frame width: %.2f and a block: %.2f", view.frame.size.width, blockSize);
return ceilf(widthWithFont / blockSize);
}

Expand Down
1 change: 1 addition & 0 deletions iOS/sosmessage/sosmessage/ViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
- (void)addSOSCategory:(NSString*)label inPosX:(int)posX andPosY:(int)posY;
- (void)fillEmptyBlocks:(int)nb fromPosX:(int)posX andPosY:(int)posY;

- (void)refreshCategoriesWithNotification:(NSNotification*)notification;
- (void)refreshCategories;
- (void)removeCategoriesLabel;
- (void)handleCategoryTapping:(UIGestureRecognizer *)sender;
Expand Down
25 changes: 11 additions & 14 deletions iOS/sosmessage/sosmessage/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ - (void)viewWillAppear:(BOOL)animated
labels = [[NSMutableArray alloc] initWithObjects:@"Remerciements", @"Calques", nil];
[super viewWillAppear:animated];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshCategories) name:UIDeviceOrientationDidChangeNotification object:nil];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshCategoriesWithNotification:) name:UIDeviceOrientationDidChangeNotification object:nil];
[self refreshCategories];
}

Expand All @@ -57,6 +58,7 @@ - (void)viewDidAppear:(BOOL)animated

- (void)viewWillDisappear:(BOOL)animated
{
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] removeObserver:self];

[labels release];
Expand All @@ -73,14 +75,6 @@ - (void)viewDidDisappear:(BOOL)animated
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;

/*
// Return YES for supported orientations
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}*/
}

-(BOOL)canBecomeFirstResponder {
Expand Down Expand Up @@ -130,7 +124,7 @@ - (void)addSOSCategory:(NSString*)label inPosX:(int)posX andPosY:(int)posY {
float rectWidth = ceilf([label sizeForBlocksForView:self.view]);
float rectHeight = 1; //arbitrary set to 1

NSLog(@"Place label (%@) at (%.2f;%.2f) with size (%.2f;%.2f)", label, rectX, rectY, rectWidth, rectHeight);
//NSLog(@"Place label (%@) at (%.2f;%.2f) with size (%.2f;%.2f)", label, rectX, rectY, rectWidth, rectHeight);

UILabel* uiLabel = [[[UILabel alloc] initWithFrame:CGRectMake(rectX, posY, rectWidth, rectHeight)] autorelease];
uiLabel.backgroundColor = [UIColor colorWithHue:label.hue saturation:0.4 brightness:0.9 alpha:1.0];
Expand All @@ -148,13 +142,14 @@ - (void)addSOSCategory:(NSString*)label inPosX:(int)posX andPosY:(int)posY {

- (void)fillEmptyBlocks:(int)nb fromPosX:(int)posX andPosY:(int)posY {
float blockSize = self.view.bounds.size.width / NB_BLOCKS;
NSLog(@"Bounds width: %.2f and Frame width: %.2f", self.view.bounds.size.width, self.view.frame.size.width);

float rectX = floorf(blockSize * posX);
float rectY = posY; //origin y will be re-calculate after views are generated
float rectWidth = blockSize * nb;
float rectHeight = 1; //arbitrary set to 1

NSLog(@"Fill %d blocks at (%.2f;%.2f) with size (%.2f;%.2f)", nb, rectX, rectY, rectWidth, rectHeight);
//NSLog(@"Fill %d blocks at (%.2f;%.2f) with size (%.2f;%.2f)", nb, rectX, rectY, rectWidth, rectHeight);
UILabel* emptyBlocks = [[[UILabel alloc] initWithFrame:CGRectMake(rectX, posY, rectWidth, rectHeight)] autorelease];

float hue = (rand()%24) / 24.0;
Expand All @@ -163,6 +158,10 @@ - (void)fillEmptyBlocks:(int)nb fromPosX:(int)posX andPosY:(int)posY {
[self.view addSubview:emptyBlocks];
}

- (void)refreshCategoriesWithNotification:(NSNotification*)notification {
[self refreshCategories];
}

- (void)refreshCategories {
[self removeCategoriesLabel];

Expand Down Expand Up @@ -198,7 +197,7 @@ - (void)refreshCategories {
if (x == 0) {
y -= 1;
}
float fitHeight = self.view.bounds.size.height / y;
float fitHeight = self.view.bounds.size.height / (y + 1);
for (UIView* subView in self.view.subviews) {
if ([subView isKindOfClass:[UILabel class]] && subView.tag == 0) {
subView.frame = CGRectMake(subView.frame.origin.x, subView.frame.origin.y * fitHeight, subView.frame.size.width, fitHeight);
Expand All @@ -207,13 +206,11 @@ - (void)refreshCategories {
}

-(void)removeCategoriesLabel {
NSLog(@"Nb of subViews: %d", self.view.subviews.count);
for (UIView* subView in self.view.subviews) {
if ([subView isKindOfClass:[UILabel class]] && subView.tag == 0) {
[subView removeFromSuperview];
}
}
NSLog(@"Nb of subViews after remove: %d", self.view.subviews.count);
}

- (void)handleCategoryTapping:(UIGestureRecognizer *)sender {
Expand Down
1 change: 1 addition & 0 deletions iOS/sosmessage/sosmessage/sosmessage-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
Expand Down

0 comments on commit 4b9f6a4

Please sign in to comment.