Bronze, Silver & Gold Challenge


#1

RotationAppDelegate.h

[code]#import <UIKit/UIKit.h>
#import “HeavyViewController.h”

@interface RotationAppDelegate : UIResponder

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) HeavyViewController *hvc;

@end[/code]

RotationAppDelegate.m

[code]#import “RotationAppDelegate.h”

@implementation RotationAppDelegate

@synthesize hvc;

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    UIDevice *device = [UIDevice currentDevice];

    [device beginGeneratingDeviceOrientationNotifications];
    [device setProximityMonitoringEnabled:TRUE];

    NSNotificationCenter *ns = [NSNotificationCenter defaultCenter];
    [ns addObserver:self
    selector:@selector(orientationChanged:)
    name:UIDeviceOrientationDidChangeNotification
    object:device];
    [ns addObserver:self
    selector:@selector(proximityChanged:)
    name:UIDeviceProximityStateDidChangeNotification
    object:device];

    hvc = [[HeavyViewController alloc] init];
    [[self window] setRootViewController:hvc];

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
    }

  • (void)orientationChanged:(NSNotification *)note
    {
    //NSLog(@“orientationChanged: %d”, [[note object] orientation]);
    }

  • (void)proximityChanged:(NSNotification *)note
    {
    if ([[note object] proximityState]) {
    [[hvc view] setBackgroundColor:[UIColor darkGrayColor]];
    }
    }

@end[/code]

HeavyViewController.m

[code]#import “HeavyViewController.h”

@implementation HeavyViewController

  • (id)init
    {
    self = [super init];

    if (self)
    {
    [[[[self view] subviews] objectAtIndex:0] setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin)];
    [[[[self view] subviews] objectAtIndex:1] setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
    [[[[self view] subviews] objectAtIndex:2] setAutoresizingMask:(UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin)];
    [[[[self view] subviews] objectAtIndex:3] setAutoresizingMask:(UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin)];
    [[[[self view] subviews] objectAtIndex:4] setAutoresizingMask:(UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin)];
    }

    return self;
    }

  • (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
    {
    return ((toInterfaceOrientation == UIDeviceOrientationPortrait) || UIInterfaceOrientationIsLandscape(toInterfaceOrientation));
    }

  • (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
    CGRect bounds = [[self view] bounds];

    if (UIDeviceOrientationIsPortrait(toInterfaceOrientation))
    {
    [[[[self view] subviews] objectAtIndex:4] setCenter:CGPointMake(36 + 20, bounds.size.height / 2)];
    } else
    {
    [[[[self view] subviews] objectAtIndex:4] setCenter:CGPointMake(bounds.size.width - 36 - 20, bounds.size.height / 2)];
    }

}

@end[/code]


#2

This might be a silly question but did you have to throw away your xib file and create your views programmatically for this to work?


#3

Nope. I created the Gold button using the XIB file and override the autorotation method to move the buttons how I needed them moved.