Files
phonegap-plugins/iOS/ScreenOrientation/ScreenOrientation.m
Simon Cruise a799d552c8 Screen Orientation Plugin to rotate screen to portrait and landscape mode
The status bar and screen will be rotated with animation to the desired
orientation.

To use the AppDelegate must have the main view controller that extends
CDVViewController as a member variable called viewController.

This main view controller must also have the following modifications.

Header file should have the below allowed orientations variable
introduced.

@interface MainViewController : CDVViewController {
    NSMutableArray *allowedOrientations;
}

@property (nonatomic, retain) NSMutableArray *allowedOrientations;

Source file should have the allowedOrientations synthesized.

@synthesize allowedOrientations;

Then perform setup in viewDidLoad method. UIDeviceOrientationPortrait
can be replaced with UIDeviceOrientationLandscapeRight depending on
desired start up orientation.

- (void) viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.allowedOrientations =  [NSMutableArray array];
    [self.allowedOrientations addObject:[NSNumber
numberWithInt:UIDeviceOrientationPortrait]];
}

Finally implement shouldAutorotateToInterfaceOrientation method with
the following.

- (BOOL)
shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface
Orientation
{
    // Return YES for supported orientations
    return [allowedOrientations containsObject:[NSNumber
numberWithInt:interfaceOrientation]];
}
2012-08-31 10:55:31 +01:00

45 lines
2.1 KiB
Objective-C

//
// ScreenOrientation.m
//
// Created by Simon Cruise on 30/08/2012.
//
#import "ScreenOrientation.h"
@implementation ScreenOrientation
- (void)set:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
AppDelegate* appDelegate = (AppDelegate*) [UIApplication sharedApplication].delegate;
NSMutableArray *allowed = [NSMutableArray array];
NSString *targetOrientation = [options objectForKey:@"key"];
int statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
int statusBarWidth = [[UIApplication sharedApplication] statusBarFrame].size.width;
if([targetOrientation isEqualToString:@"landscape"]) {
[allowed addObject:[NSNumber numberWithInt:UIDeviceOrientationLandscapeRight]];
appDelegate.viewController.allowedOrientations = allowed;
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeRight animated:YES];
[appDelegate.viewController.view setTransform: CGAffineTransformMakeRotation(M_PI * 1.5)];
[appDelegate.viewController.view setFrame:CGRectMake(statusBarHeight, 0, appDelegate.viewController.view.frame.size.height-statusBarHeight, appDelegate.viewController.view.frame.size.width+statusBarHeight)];
[UIView commitAnimations];
}
if([targetOrientation isEqualToString:@"portrait"]) {
if (![appDelegate.viewController.allowedOrientations containsObject:[NSNumber numberWithInt:UIDeviceOrientationPortrait]]) {
[allowed addObject:[NSNumber numberWithInt:UIDeviceOrientationPortrait]];
appDelegate.viewController.allowedOrientations = allowed;
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationPortrait animated:YES];
[appDelegate.viewController.view setTransform: CGAffineTransformMakeRotation(0)];
[appDelegate.viewController.view setFrame:CGRectMake(0, statusBarWidth, appDelegate.viewController.view.frame.size.height+statusBarWidth, appDelegate.viewController.view.frame.size.width-statusBarWidth)];
[UIView commitAnimations];
}
}
}
@end