From 06f38e0b538095e1aa3f648a8feec9e4da2d7311 Mon Sep 17 00:00:00 2001 From: Julian Finkler Date: Mon, 16 Dec 2019 20:59:07 +0100 Subject: [PATCH 1/3] Fix for Issue #61 While the iOS device is rotating the bounds of the previewView will be recalculated and the scanRect will be reinitialized Changes in the WorkspaceSettings are necessary to run the example app --- .../xcshareddata/WorkspaceSettings.xcsettings | 10 +++++ ios/Classes/BarcodeScannerViewController.m | 44 ++++++++++++------- 2 files changed, 39 insertions(+), 15 deletions(-) create mode 100644 example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings diff --git a/example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings b/example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings new file mode 100644 index 0000000..6b30c74 --- /dev/null +++ b/example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings @@ -0,0 +1,10 @@ + + + + + BuildSystemType + Original + PreviewsEnabled + + + diff --git a/ios/Classes/BarcodeScannerViewController.m b/ios/Classes/BarcodeScannerViewController.m index da44ca0..0366f0d 100644 --- a/ios/Classes/BarcodeScannerViewController.m +++ b/ios/Classes/BarcodeScannerViewController.m @@ -10,6 +10,34 @@ @implementation BarcodeScannerViewController { } +- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coordinator +{ + CGRect bounds = [UIScreen mainScreen].bounds; + CGRect reversedBounds = CGRectMake(bounds.origin.x, bounds.origin.y, bounds.size.height, bounds.size.width); + self.previewView.bounds = reversedBounds; + self.previewView.frame = reversedBounds; + [self.scanRect removeFromSuperview]; + [self setupScanRect:reversedBounds]; + [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; +} + +- (void)setupScanRect:(CGRect)bounds { + self.scanRect = [[ScannerOverlay alloc] initWithFrame:bounds]; + self.scanRect.translatesAutoresizingMaskIntoConstraints = NO; + self.scanRect.backgroundColor = UIColor.clearColor; + [self.view addSubview:_scanRect]; + [self.view addConstraints:[NSLayoutConstraint + constraintsWithVisualFormat:@"V:[scanRect]" + options:NSLayoutFormatAlignAllBottom + metrics:nil + views:@{@"scanRect": _scanRect}]]; + [self.view addConstraints:[NSLayoutConstraint + constraintsWithVisualFormat:@"H:[scanRect]" + options:NSLayoutFormatAlignAllBottom + metrics:nil + views:@{@"scanRect": _scanRect}]]; + [_scanRect startAnimating]; +} - (void)viewDidLoad { [super viewDidLoad]; @@ -26,21 +54,7 @@ options:NSLayoutFormatAlignAllBottom metrics:nil views:@{@"previewView": _previewView}]]; - self.scanRect = [[ScannerOverlay alloc] initWithFrame:self.view.bounds]; - self.scanRect.translatesAutoresizingMaskIntoConstraints = NO; - self.scanRect.backgroundColor = UIColor.clearColor; - [self.view addSubview:_scanRect]; - [self.view addConstraints:[NSLayoutConstraint - constraintsWithVisualFormat:@"V:[scanRect]" - options:NSLayoutFormatAlignAllBottom - metrics:nil - views:@{@"scanRect": _scanRect}]]; - [self.view addConstraints:[NSLayoutConstraint - constraintsWithVisualFormat:@"H:[scanRect]" - options:NSLayoutFormatAlignAllBottom - metrics:nil - views:@{@"scanRect": _scanRect}]]; - [_scanRect startAnimating]; + [self setupScanRect:self.view.bounds]; self.scanner = [[MTBBarcodeScanner alloc] initWithPreviewView:_previewView]; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel)]; [self updateFlashButton]; From e0cc5c3bc2d2aa1f3777280ef85082f42222d30a Mon Sep 17 00:00:00 2001 From: Julian Finkler Date: Sun, 12 Jan 2020 12:18:48 +0100 Subject: [PATCH 2/3] Fixed the size of the preview overlay --- ios/Classes/BarcodeScannerViewController.m | 1 + ios/Classes/ScannerOverlay.m | 26 ++++++++++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/ios/Classes/BarcodeScannerViewController.m b/ios/Classes/BarcodeScannerViewController.m index 0366f0d..662c381 100644 --- a/ios/Classes/BarcodeScannerViewController.m +++ b/ios/Classes/BarcodeScannerViewController.m @@ -16,6 +16,7 @@ CGRect reversedBounds = CGRectMake(bounds.origin.x, bounds.origin.y, bounds.size.height, bounds.size.width); self.previewView.bounds = reversedBounds; self.previewView.frame = reversedBounds; + [self.scanRect stopAnimating]; [self.scanRect removeFromSuperview]; [self setupScanRect:reversedBounds]; [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; diff --git a/ios/Classes/ScannerOverlay.m b/ios/Classes/ScannerOverlay.m index ab4c121..eb8ca6f 100644 --- a/ios/Classes/ScannerOverlay.m +++ b/ios/Classes/ScannerOverlay.m @@ -83,13 +83,25 @@ } - (CGRect)scanRect { - CGRect rect = self.frame; - CGFloat heightMultiplier = 3.0/4.0; // 4:3 aspect ratio - CGFloat scanRectWidth = rect.size.width * 0.8f; - CGFloat scanRectHeight = scanRectWidth * heightMultiplier; - CGFloat scanRectOriginX = (rect.size.width / 2) - (scanRectWidth / 2); - CGFloat scanRectOriginY = (rect.size.height / 2) - (scanRectHeight / 2); - return CGRectMake(scanRectOriginX, scanRectOriginY, scanRectWidth, scanRectHeight); + CGRect rect = self.frame; + + CGFloat frameWidth = rect.size.width; + CGFloat frameHeight = rect.size.height; + + BOOL isLandscape = frameWidth > frameHeight; + CGFloat widthOnPotrait = isLandscape ? frameHeight : frameWidth; + CGFloat heightMultiplier = 3.0/4.0; // 4:3 aspect ratio + CGFloat scanRectWidth = widthOnPotrait * 0.8f; + CGFloat scanRectHeight = scanRectWidth * heightMultiplier; + + if(isLandscape) { + CGFloat navbarHeight = 32; + frameHeight += navbarHeight; + } + + CGFloat scanRectOriginX = (frameWidth - scanRectWidth) / 2; + CGFloat scanRectOriginY = (frameHeight - scanRectHeight) / 2; + return CGRectMake(scanRectOriginX, scanRectOriginY, scanRectWidth, scanRectHeight); } - (CGRect)scanLineRect { From 770bb47d6c7df86ae0adf69ba38e61d6b05fc95e Mon Sep 17 00:00:00 2001 From: Julian Finkler Date: Sun, 12 Jan 2020 12:34:25 +0100 Subject: [PATCH 3/3] Fixed the position of the scan line --- ios/Classes/ScannerOverlay.m | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ios/Classes/ScannerOverlay.m b/ios/Classes/ScannerOverlay.m index eb8ca6f..7c415b9 100644 --- a/ios/Classes/ScannerOverlay.m +++ b/ios/Classes/ScannerOverlay.m @@ -89,10 +89,10 @@ CGFloat frameHeight = rect.size.height; BOOL isLandscape = frameWidth > frameHeight; - CGFloat widthOnPotrait = isLandscape ? frameHeight : frameWidth; - CGFloat heightMultiplier = 3.0/4.0; // 4:3 aspect ratio - CGFloat scanRectWidth = widthOnPotrait * 0.8f; - CGFloat scanRectHeight = scanRectWidth * heightMultiplier; + CGFloat widthOnPortrait = isLandscape ? frameHeight : frameWidth; + CGFloat scanRectWidth = widthOnPortrait * 0.8f; + CGFloat aspectRatio = 3.0/4.0; + CGFloat scanRectHeight = scanRectWidth * aspectRatio; if(isLandscape) { CGFloat navbarHeight = 32; @@ -106,8 +106,8 @@ - (CGRect)scanLineRect { CGRect scanRect = [self scanRect]; - CGRect rect = self.frame; - return CGRectMake(scanRect.origin.x, rect.size.height / 2, scanRect.size.width, 1); + CGFloat positionY = scanRect.origin.y + (scanRect.size.height / 2); + return CGRectMake(scanRect.origin.x, positionY, scanRect.size.width, 1); } @end