Making the QRCodeReaderView public

This commit is contained in:
Yannick Loriot 2017-08-10 21:25:17 +02:00
parent 5850c05d7b
commit 4539cda341
5 changed files with 55 additions and 49 deletions

View File

@ -1,5 +1,11 @@
# Change log # Change log
## [Version 7.5.0](https://github.com/yannickl/QRCodeReader.swift/releases/tag/7.5.0)
Release on 2017-08-10
- [REFACTORING] Makes the `QRCodeReaderView` public
- [REFACTORING] The rotation is now managed by the `QRCodeReaderView`
## [Version 7.4.2](https://github.com/yannickl/QRCodeReader.swift/releases/tag/7.4.2) ## [Version 7.4.2](https://github.com/yannickl/QRCodeReader.swift/releases/tag/7.4.2)
Release on 2017-05-28 Release on 2017-05-28

View File

@ -310,6 +310,7 @@
TargetAttributes = { TargetAttributes = {
CE412E8819D9A1E4000F294E = { CE412E8819D9A1E4000F294E = {
CreatedOnToolsVersion = 6.0.1; CreatedOnToolsVersion = 6.0.1;
DevelopmentTeam = WNK46V33W9;
LastSwiftMigration = 0800; LastSwiftMigration = 0800;
ProvisioningStyle = Automatic; ProvisioningStyle = Automatic;
}; };
@ -554,7 +555,7 @@
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
DEVELOPMENT_TEAM = ""; DEVELOPMENT_TEAM = WNK46V33W9;
INFOPLIST_FILE = QRCodeReader.swift/Info.plist; INFOPLIST_FILE = QRCodeReader.swift/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.workandplay.qrcodereader; PRODUCT_BUNDLE_IDENTIFIER = com.workandplay.qrcodereader;
@ -569,7 +570,7 @@
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
DEVELOPMENT_TEAM = ""; DEVELOPMENT_TEAM = WNK46V33W9;
INFOPLIST_FILE = QRCodeReader.swift/Info.plist; INFOPLIST_FILE = QRCodeReader.swift/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.workandplay.qrcodereader; PRODUCT_BUNDLE_IDENTIFIER = com.workandplay.qrcodereader;

View File

@ -26,8 +26,8 @@
import UIKit import UIKit
final class QRCodeReaderView: UIView, QRCodeReaderDisplayable { final public class QRCodeReaderView: UIView, QRCodeReaderDisplayable {
lazy var overlayView: UIView? = { public lazy var overlayView: UIView? = {
let ov = ReaderOverlayView() let ov = ReaderOverlayView()
ov.backgroundColor = .clear ov.backgroundColor = .clear
@ -37,7 +37,7 @@ final class QRCodeReaderView: UIView, QRCodeReaderDisplayable {
return ov return ov
}() }()
let cameraView: UIView = { public let cameraView: UIView = {
let cv = UIView() let cv = UIView()
cv.clipsToBounds = true cv.clipsToBounds = true
@ -46,7 +46,7 @@ final class QRCodeReaderView: UIView, QRCodeReaderDisplayable {
return cv return cv
}() }()
lazy var cancelButton: UIButton? = { public lazy var cancelButton: UIButton? = {
let cb = UIButton() let cb = UIButton()
cb.translatesAutoresizingMaskIntoConstraints = false cb.translatesAutoresizingMaskIntoConstraints = false
@ -55,7 +55,7 @@ final class QRCodeReaderView: UIView, QRCodeReaderDisplayable {
return cb return cb
}() }()
lazy var switchCameraButton: UIButton? = { public lazy var switchCameraButton: UIButton? = {
let scb = SwitchCameraButton() let scb = SwitchCameraButton()
scb.translatesAutoresizingMaskIntoConstraints = false scb.translatesAutoresizingMaskIntoConstraints = false
@ -63,7 +63,7 @@ final class QRCodeReaderView: UIView, QRCodeReaderDisplayable {
return scb return scb
}() }()
lazy var toggleTorchButton: UIButton? = { public lazy var toggleTorchButton: UIButton? = {
let ttb = ToggleTorchButton() let ttb = ToggleTorchButton()
ttb.translatesAutoresizingMaskIntoConstraints = false ttb.translatesAutoresizingMaskIntoConstraints = false
@ -71,7 +71,10 @@ final class QRCodeReaderView: UIView, QRCodeReaderDisplayable {
return ttb return ttb
}() }()
func setupComponents(showCancelButton: Bool, showSwitchCameraButton: Bool, showTorchButton: Bool, showOverlayView: Bool) { private weak var reader: QRCodeReader?
public func setupComponents(showCancelButton: Bool, showSwitchCameraButton: Bool, showTorchButton: Bool, showOverlayView: Bool, reader: QRCodeReader?) {
self.reader = reader
translatesAutoresizingMaskIntoConstraints = false translatesAutoresizingMaskIntoConstraints = false
addComponents() addComponents()
@ -122,6 +125,7 @@ final class QRCodeReaderView: UIView, QRCodeReaderDisplayable {
func addRedBorder() { func addRedBorder() {
self.startTimerForBorderReset() self.startTimerForBorderReset()
if let ovl = self.overlayView as? ReaderOverlayView { if let ovl = self.overlayView as? ReaderOverlayView {
ovl.overlayColor = .red ovl.overlayColor = .red
} }
@ -129,14 +133,29 @@ final class QRCodeReaderView: UIView, QRCodeReaderDisplayable {
func addGreenBorder() { func addGreenBorder() {
self.startTimerForBorderReset() self.startTimerForBorderReset()
if let ovl = self.overlayView as? ReaderOverlayView { if let ovl = self.overlayView as? ReaderOverlayView {
ovl.overlayColor = .green ovl.overlayColor = .green
} }
} }
func orientationDidChange() {
setNeedsDisplay()
overlayView?.setNeedsDisplay()
if let connection = reader?.previewLayer.connection, connection.isVideoOrientationSupported {
let orientation = UIDevice.current.orientation
let supportedInterfaceOrientations = UIApplication.shared.supportedInterfaceOrientations(for: nil)
connection.videoOrientation = QRCodeReader.videoOrientation(deviceOrientation: orientation, withSupportedOrientations: supportedInterfaceOrientations, fallbackOrientation: connection.videoOrientation)
}
}
// MARK: - Convenience Methods // MARK: - Convenience Methods
private func addComponents() { private func addComponents() {
NotificationCenter.default.addObserver(self, selector: #selector(QRCodeReaderView.orientationDidChange), name: .UIDeviceOrientationDidChange, object: nil)
addSubview(cameraView) addSubview(cameraView)
if let ov = overlayView { if let ov = overlayView {

View File

@ -49,8 +49,10 @@ public protocol QRCodeReaderDisplayable {
- Parameter showCancelButton: Flag to know whether you should display the cancel button. - Parameter showCancelButton: Flag to know whether you should display the cancel button.
- Parameter showSwitchCameraButton: Flag to know whether you should display the switch camera button. - Parameter showSwitchCameraButton: Flag to know whether you should display the switch camera button.
- Parameter showTorchButton: Flag to know whether you should display the toggle torch button. - Parameter showTorchButton: Flag to know whether you should display the toggle torch button.
- Parameter showOverlayView: Flag to know whether you should display the overlay.
- Parameter reader: A reference to the code reader.
*/ */
func setupComponents(showCancelButton: Bool, showSwitchCameraButton: Bool, showTorchButton: Bool, showOverlayView: Bool) func setupComponents(showCancelButton: Bool, showSwitchCameraButton: Bool, showTorchButton: Bool, showOverlayView: Bool, reader: QRCodeReader?)
} }
/// The `QRCodeReaderContainer` structure embed the view displayed by the controller. The embeded view must be conform to the `QRCodeReaderDisplayable` protocol. /// The `QRCodeReaderContainer` structure embed the view displayed by the controller. The embeded view must be conform to the `QRCodeReaderDisplayable` protocol.
@ -70,7 +72,7 @@ public struct QRCodeReaderContainer {
// MARK: - Convenience Methods // MARK: - Convenience Methods
func setupComponents(showCancelButton: Bool, showSwitchCameraButton: Bool, showTorchButton: Bool, showOverlayView: Bool) { func setupComponents(showCancelButton: Bool, showSwitchCameraButton: Bool, showTorchButton: Bool, showOverlayView: Bool, reader: QRCodeReader? = nil) {
displayable.setupComponents(showCancelButton: showCancelButton, showSwitchCameraButton: showSwitchCameraButton, showTorchButton: showTorchButton, showOverlayView: showOverlayView) displayable.setupComponents(showCancelButton: showCancelButton, showSwitchCameraButton: showSwitchCameraButton, showTorchButton: showTorchButton, showOverlayView: showOverlayView, reader: reader)
} }
} }

View File

@ -38,7 +38,6 @@ public class QRCodeReaderViewController: UIViewController {
let showSwitchCameraButton: Bool let showSwitchCameraButton: Bool
let showTorchButton: Bool let showTorchButton: Bool
let showOverlayView: Bool let showOverlayView: Bool
let handleOrientationChange: Bool
// MARK: - Managing the Callback Responders // MARK: - Managing the Callback Responders
@ -69,7 +68,6 @@ public class QRCodeReaderViewController: UIViewController {
showSwitchCameraButton = builder.showSwitchCameraButton showSwitchCameraButton = builder.showSwitchCameraButton
showTorchButton = builder.showTorchButton showTorchButton = builder.showTorchButton
showOverlayView = builder.showOverlayView showOverlayView = builder.showOverlayView
handleOrientationChange = builder.handleOrientationChange
super.init(nibName: nil, bundle: nil) super.init(nibName: nil, bundle: nil)
@ -94,8 +92,6 @@ public class QRCodeReaderViewController: UIViewController {
} }
setupUIComponentsWithCancelButtonTitle(builder.cancelButtonTitle) setupUIComponentsWithCancelButtonTitle(builder.cancelButtonTitle)
NotificationCenter.default.addObserver(self, selector: #selector(orientationDidChange), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
} }
required public init?(coder aDecoder: NSCoder) { required public init?(coder aDecoder: NSCoder) {
@ -106,15 +102,11 @@ public class QRCodeReaderViewController: UIViewController {
showTorchButton = false showTorchButton = false
showSwitchCameraButton = false showSwitchCameraButton = false
showOverlayView = false showOverlayView = false
handleOrientationChange = false
super.init(coder: aDecoder) super.init(coder: aDecoder)
} }
// MARK: - Responding to View Events // MARK: - Responding to View Events
override public var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return parent?.supportedInterfaceOrientations ?? .all
}
override public func viewWillAppear(_ animated: Bool) { override public func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated) super.viewWillAppear(animated)
@ -136,20 +128,6 @@ public class QRCodeReaderViewController: UIViewController {
codeReader.previewLayer.frame = view.bounds codeReader.previewLayer.frame = view.bounds
} }
// MARK: - Managing the Orientation
func orientationDidChange(_ notification: Notification) {
readerView.view.setNeedsDisplay()
if showOverlayView, let qrv = readerView.displayable as? QRCodeReaderView {
qrv.overlayView?.setNeedsDisplay()
}
if handleOrientationChange == true, let device = notification.object as? UIDevice, let connection = codeReader.previewLayer.connection, connection.isVideoOrientationSupported {
connection.videoOrientation = QRCodeReader.videoOrientation(deviceOrientation: device.orientation, withSupportedOrientations: supportedInterfaceOrientations, fallbackOrientation: connection.videoOrientation)
}
}
// MARK: - Initializing the AV Components // MARK: - Initializing the AV Components
private func setupUIComponentsWithCancelButtonTitle(_ cancelButtonTitle: String) { private func setupUIComponentsWithCancelButtonTitle(_ cancelButtonTitle: String) {
@ -158,7 +136,7 @@ public class QRCodeReaderViewController: UIViewController {
let sscb = showSwitchCameraButton && codeReader.hasFrontDevice let sscb = showSwitchCameraButton && codeReader.hasFrontDevice
let stb = showTorchButton && codeReader.isTorchAvailable let stb = showTorchButton && codeReader.isTorchAvailable
readerView.setupComponents(showCancelButton: showCancelButton, showSwitchCameraButton: sscb, showTorchButton: stb, showOverlayView: showOverlayView) readerView.setupComponents(showCancelButton: showCancelButton, showSwitchCameraButton: sscb, showTorchButton: stb, showOverlayView: showOverlayView, reader: codeReader)
// Setup action methods // Setup action methods