Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
[ios] Integrated shazron's iOS6 orientation updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Willoughby committed Nov 8, 2012
1 parent f1fb499 commit e0f7b9a
Showing 1 changed file with 72 additions and 10 deletions.
82 changes: 72 additions & 10 deletions src/ios/CDVBarcodeScanner.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,20 @@
//------------------------------------------------------------------------------
#import "zxing-all-in-one.h"

#import <CORDOVA/CDVPlugin.h>
#import <Cordova/CDVPlugin.h>


//------------------------------------------------------------------------------
// Delegate to handle orientation functions
//
//------------------------------------------------------------------------------
@protocol CDVBarcodeScannerOrientationDelegate <NSObject>

- (NSUInteger)supportedInterfaceOrientations;
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
- (BOOL)shouldAutorotate;

@end

//------------------------------------------------------------------------------
// Adds a shutter button to the UI, and changes the scan from continuous to
Expand Down Expand Up @@ -72,11 +85,13 @@ - (void)dumpImage:(UIImage*)image;
//------------------------------------------------------------------------------
// view controller for the ui
//------------------------------------------------------------------------------
@interface CDVbcsViewController : UIViewController {}
@interface CDVbcsViewController : UIViewController <CDVBarcodeScannerOrientationDelegate> {}
@property (nonatomic, retain) CDVbcsProcessor* processor;
@property (nonatomic, retain) NSString* alternateXib;
@property (nonatomic) BOOL shutterPressed;
@property (nonatomic, retain) IBOutlet UIView* overlayView;
// unsafe_unretained is equivalent to assign - used to prevent retain cycles in the property below
@property (nonatomic, unsafe_unretained) id orientationDelegate;

- (id)initWithProcessor:(CDVbcsProcessor*)processor alternateOverlay:(NSString *)alternateXib;
- (void)startCapturing;
Expand Down Expand Up @@ -234,6 +249,8 @@ - (void)scanBarcode {
}

self.viewController = [[[CDVbcsViewController alloc] initWithProcessor: self alternateOverlay:self.alternateXib] autorelease];
// here we set the orientation delegate to the MainViewController of the app (orientation controlled in the Project Settings)
self.viewController.orientationDelegate = self.plugin.viewController;

// delayed [self openDialog];
[self performSelector:@selector(openDialog) withObject:nil afterDelay:1];
Expand Down Expand Up @@ -629,6 +646,17 @@ - (void)loadView {
[self.view addSubview:[self buildOverlayView]];
}

//--------------------------------------------------------------------------
- (void)viewWillAppear:(BOOL)animated {

// set video orientation to what the camera sees
self.processor.previewLayer.orientation = [[UIApplication sharedApplication] statusBarOrientation];

// this fixes the bug when the statusbar is landscape, and the preview layer
// starts up in portrait (not filling the whole view)
self.processor.previewLayer.frame = self.view.bounds;
}

//--------------------------------------------------------------------------
- (void)viewDidAppear:(BOOL)animated {
[self startCapturing];
Expand All @@ -641,13 +669,6 @@ - (void)startCapturing {
self.processor.capturing = YES;
}

//--------------------------------------------------------------------------
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// rotation currently not supported
if (interfaceOrientation == UIInterfaceOrientationPortrait) return YES;
return NO;
}

//--------------------------------------------------------------------------
- (void)shutterButtonPressed {
self.shutterPressed = YES;
Expand Down Expand Up @@ -796,4 +817,45 @@ - (UIImage*)buildReticleImage {
return result;
}

@end
#pragma mark CDVBarcodeScannerOrientationDelegate

- (BOOL)shouldAutorotate
{
if ((self.orientationDelegate != nil) && [self.orientationDelegate respondsToSelector:@selector(shouldAutorotate)]) {
return [self.orientationDelegate shouldAutorotate];
}

return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
if ((self.orientationDelegate != nil) && [self.orientationDelegate respondsToSelector:@selector(supportedInterfaceOrientations)]) {
return [self.orientationDelegate supportedInterfaceOrientations];
}

return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ((self.orientationDelegate != nil) && [self.orientationDelegate respondsToSelector:@selector(shouldAutorotateToInterfaceOrientation:)]) {
return [self.orientationDelegate shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

return YES;
}

- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration
{
[CATransaction begin];

self.processor.previewLayer.orientation = orientation;
[self.processor.previewLayer layoutSublayers];
self.processor.previewLayer.frame = self.view.bounds;

[CATransaction commit];
[super willAnimateRotationToInterfaceOrientation:orientation duration:duration];
}

@end

0 comments on commit e0f7b9a

Please sign in to comment.